3

結合を使用して 2 つのテーブルからデータを取得する必要があります。

私はこのコードを持っていますが、次のエラーで失敗しますCall to undefined method Zend\Db\ResultSet\ResultSet::from():

public function getUsers($id){
    $id  = (int) $id;
    $rowset = $this->tableGateway->select()->from(array('u' => 'user'))
        ->join(array('l' => 'levels'),
            'u.user_id = l.id_user');
    $row = $rowset->current();
    if (!$row) {
        throw new \Exception("Could not find row $id");
    }
    return $row;
}

SQL コマンドは次のようになります。

select user.*,levels.name from user left join levels on user.user_id=levels.id_user

ありがとう

更新 @Mohamad の変更を使用すると、次のようになります。

The table name of the provided select object must match that of the table

私の UsersTable.php は次のようになります。

<?php
// module/Users/src/Users/Model/UsersTable.php:
namespace Users\Model;

use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Select;

class UsersTable
{
    protected $tableGateway;

    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
    }

    public function fetchAll()
    {
        $select = new Select();
        $select->from('levels');
        $select->join('user', 'levels.id=user.user_id',Select::SQL_STAR,Select::JOIN_RIGHT);
        $rowset = $this->tableGateway->selectWith ( $select );

        $resultSet = $rowset->current();
        if (!$resultSet) {
            throw new \Exception("Could not find row $id");
        }
        return $resultSet;
    }
4

1 に答える 1