TableGateway with multiple FROM tables

南楼画角 提交于 2019-12-17 09:45:49

问题


I would like to do a simple INNER JOIN between two tables in Zend2.

Concretely, I would like to do this in Zend2:

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

I have a 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');
    });
  }
}

The $select->from('foo'); returns an error:

==> Since this object was created with a table and/or schema in the constructor, it is read only.

So, I can't tweak my FROM statement to match a simple inner join between FooTable and BarTable.


回答1:


I hope this will help you along your journey as this is a working example I have:

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();
    }
}


来源:https://stackoverflow.com/questions/14354802/tablegateway-with-multiple-from-tables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!