0

我必须在 Zend 2 中使用 select 进行连接,有没有办法做到这一点?我看到 join 期望第一个参数是一个字符串(你加入的表的名称)。我设法使用类似这样的方法对另一个选择进行了选择

$this->select(function (Select $select) use ($params)

但是又一次 join(function (Select $select) ) 不起作用。

4

2 回答 2

0

A join has two required parameters, the name of the table and the condition to join on. You can also optionally specify columns to return and the join type, which defaults to an inner join. Here is a quick example to fetch the author for a post.

// 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'));
于 2015-01-15T16:21:15.857 回答
0
    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();
于 2018-02-23T06:32:34.337 回答