Join Zend 2与select连接
我必须在Zend 2中使用select进行连接,有什么方法可以做到吗?我看到连接期望first paremetr是您连接的表的字符串名称。 我用类似这样的东西,用另一个select做了一个selectJoin Zend 2与select连接,join,zend-framework2,tablegateway,Join,Zend Framework2,Tablegateway,我必须在Zend 2中使用select进行连接,有什么方法可以做到吗?我看到连接期望first paremetr是您连接的表的字符串名称。 我用类似这样的东西,用另一个select做了一个select $this->select(function (Select $select) use ($params) 但是joinfunction Select$Select同样不起作用。联接有两个必需的参数,表名和联接条件。您还可以选择指定要返回的列和联接类型(默认为内部联接)。下面是一个快速的例
$this->select(function (Select $select) use ($params)
但是joinfunction Select$Select同样不起作用。联接有两个必需的参数,表名和联接条件。您还可以选择指定要返回的列和联接类型(默认为内部联接)。下面是一个快速的例子来获取作者的帖子
// First you need to create the select object
$select = $sql->select();
$select->from('posts');
// then join to the people table
$select->join('people', 'posts.author_id = people.id', array('first_name','last_name'));
我知道所有这些,我做过简单的连接,我的问题是如何用select进行连接。类似这样的东西:select*from YTable y internal join select minprice,根据y.id=x.idI,我认为唯一的方法是创建自己的select对象并重构join方法。也许这会有帮助->下面的示例将指导您:或者不要使用zend方法,只创建查询字符串和execute。注释如果你想要一个常规查询的示例而不是zend way我知道如何创建查询字符串和执行,这是我现在使用的,但我想更改和使用对象。如果你知道如何使用,我想要一个zend方式的连接示例。
Please add below code on top of model class
------------------------------------------
use Zend\Db\Sql\Expression;
use Zend\Db\Sql\Predicate;
use Zend\Db\Sql\Sql;
Then You can use this code
--------------------------
$sql = new Sql($this->adapter);
$select = $sql->select();
$select->from(array('nxyr' => 'node-x-y-relation'));
$join = new Expression("ni.node_id = nxyr.node_x_id and ni.node_type_id IN (" . $nodeTypeStr . ")");
$join2 = new Expression("np.node_id = nxyr.node_x_id and np.node_type_id = 2");
$join3 = new Expression("nc.node_id = nxyr.node_x_id and nc.node_type_id = 2");
$select->join(array('nc' => 'node-class'), $join3, array('node_type_id'), 'Left');
$select->join(array('ni' => 'node-instance'), $join, array('node_type_id'), 'Left');
$select->join(array('np' => 'node-instance-property'), $join2, array('node_type_id'), 'Left');
$select->where->equalTo('nxyr.node_y_id', $node_id);
$statement = $sql->prepareStatementForSqlObject($select);
$result = $statement->execute();
$resultObj = new ResultSet();
$nodeXYArr = $resultObj->initialize($result)->toArray();