具有多个FROM表的TableGateway [英] TableGateway with multiple FROM tables

查看:71
本文介绍了具有多个FROM表的TableGateway的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

具体来说,我想在Zend2中做到这一点:

Concretely, I would like to do this in 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');
    });
  }
}

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

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

因此,我无法调整FROM语句以匹配FooTableBarTable之间的简单内部联接.

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

推荐答案

我希望这对您的旅程有帮助,因为这是我的一个工作示例:

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

这篇关于具有多个FROM表的TableGateway的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆