TableGateway具有多个FROM表

San*_*nda 5 php zend-framework2 tablegateway

我想INNER JOIN在Zend2中的两个表之间做一个简单的操作.

具体来说,我想在Zend2中这样做:

SELECT * FROM foo, bar WHERE foo.foreign_id = bar.id;

我有一个FooTable:

class FooTable
{
  protected $tableGateway;

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

  public function get($id)
  {
    $rowset = $this->tableGateway->select(function (Select $select) {
      $select->from('foo');
    });
  }
}
Run Code Online (Sandbox Code Playgroud)

$select->from('foo');返回一个错误:

==> 由于此对象是在构造函数中使用表和/或模式创建的,因此它是只读的.

所以,我不能调整我的FROM语句来匹配FooTable和之间的简单内连接BarTable.

Die*_*uzi 12

我希望这会对你的旅程有所帮助,因为这是我的一个有效例子:

namespace Pool\Model;

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

class IpaddressPool extends AbstractTableGateway
{
    public function __construct($adapter)
    {
        $this->table = 'ipaddress_pool';

        $this->adapter = $adapter;

        $this->initialize();
    }

    public function Leases($poolid)
    {
        $result = $this->select(function (Select $select) use ($poolid) {
            $select
                ->columns(array(
                    'ipaddress',
                    'accountid',
                    'productid',
                    'webaccountid'
                ))
                ->join('account', 'account.accountid = ipaddress_pool.accountid', array(
                    'firstname',
                    'lastname'
                ))
                ->join('product_hosting', 'product_hosting.hostingid = ipaddress_pool.hostingid', array(
                    'name'
                ))
                ->join('webaccount', 'webaccount.webaccountid = ipaddress_pool.webaccountid', array(
                    'domain'
                ))->where->equalTo('ipaddress_pool.poolid', $poolid);
        });

        return $result->toArray();
    }
}
Run Code Online (Sandbox Code Playgroud)