【问题标题】:join 2 tables using tablegateway in ZF2在 ZF2 中使用 tablegateway 连接 2 个表
【发布时间】:2013-05-07 09:49:27
【问题描述】:

您好,感谢阅读,

我有两张桌子(使用 tablegateway) 项目和 l_agent_projet 它们设置正确,我可以使用它们插入、删除、更新等我的数据库

我正在尝试使用此函数连接这两个表:

public function getAgentsByProject($id_projet)
{
    $sql = new Sql( $this->tableGateway->adapter ) ;
    $where = new Where() ;
    $where -> equalTo( 'l_agent_projet.id_projet', $id_projet ) ;

    $select = $sql->select() ;
    $select -> from ( $this->tableGateway->getTable() )
            -> join ( 'projet' , 'projet.id_projet = l_agent_projet.id_projet')
            -> where( $where ) ;

    $result = $this->tableGateway->selectWith($select) ;

    return $result ;
}

但是当我尝试读取结果时,在我的一个控制器中使用它:

     $test = $this->InitProjectByIDAgentTable()->getAgentsByProject('1') ;
     var_dump($test) ;
     echo "<br />" ;

     foreach ( $test as $tmp1 ):
        foreach  ( $tmp1 as $tmp2 ):
            echo $tmp2 ;
            echo '+' ;
        endforeach ;
        echo "<br />" ;
     endforeach ;

这是我得到的:

object(Zend\Db\ResultSet\ResultSet)#236 (8) { ["allowedReturnTypes":protected]=> array(2) { [0]=> string(11) "arrayobject" [1]=> string(5) "array" } ["arrayObjectPrototype":protected]=> object(Application\Model\LAgentProject)#220 (2) { ["id_agent"]=> NULL ["id_projet"]=> NULL } ["returnType":protected]=> string(11) "arrayobject" ["buffer":protected]=> NULL ["count":protected]=> int(2) ["dataSource":protected]=> object(Zend\Db\Adapter\Driver\Pdo\Result)#235 (8) { ["statementMode":protected]=> string(7) "forward" ["resource":protected]=> object(PDOStatement)#234 (1) { ["queryString"]=> string(179) "SELECT "l_agent_projet".*, "projet".* FROM "l_agent_projet" INNER JOIN "projet" ON "projet"."id_projet" = "l_agent_projet"."id_projet" WHERE "l_agent_projet"."id_projet" = :where1" } ["options":protected]=> NULL ["currentComplete":protected]=> bool(false) ["currentData":protected]=> NULL ["position":protected]=> int(-1) ["generatedValue":protected]=> NULL ["rowCount":protected]=> int(2) } ["fieldCount":protected]=> int(7) ["position":protected]=> int(0) }
1+1+
3+1+

上面写着 ["fieldCount":protected]=> int(7) ,但只能打印 l_agent_projet 表对应的前两个字段。所以有问题。

但是当我复制生成的sql命令时:

SELECT "l_agent_projet".*, "projet".*
FROM "l_agent_projet"
INNER JOIN "projet" ON "projet"."id_projet" = "l_agent_projet"."id_projet" 
WHERE "l_agent_projet"."id_projet" = :'1'"

效果很好,可以打印所有字段。

我是 ZF2 的新手,我不明白这里发生了什么。 ps: 使用 PDO:pgsql 处理 postgresql

【问题讨论】:

    标签: postgresql join zend-framework2


    【解决方案1】:

    你需要使用下面这两行:

    $statement = $sql->prepareStatementForSqlObject($select);
    $result = $statement->execute();
    

    而不是行:

    $result = $this->tableGateway->selectWith($select) ;
    

    以上行将返回数据数组。

    【讨论】:

    • 当我这样做时,我得到:致命错误:在 /var/www/projet_b/module/Application/src/Application/Model 中的非对象上调用成员函数 initialize() /LAgentProjectTable.php 。为什么我不能在控制器中的 $result 上使用 toArray 方法? (感谢您的快速回答)
    • @aramir 我已经编辑了我的答案。尝试一下,您可以在我们的结果集上使用 toArray 方法。
    • 使用您编辑的解决方案,我得到了这个:致命错误:调用未定义的方法 Zend\Db\Adapter\Driver\Pdo\Result::toArray() in /var/www/projet_b /module/Application/src/Application/Model/LAgentProjectTable.php 。但是现在如果我跳过/不使用 toArray() 方法,我已经拥有了所有的字段......现在我真的不知道它为什么会起作用,我猜想 selectwith() 方法正在发生一些魔法。无论如何,谢谢你的帮助。 (抱歉不能投票给你,我需要 15 个代表:/)
    【解决方案2】:

    要在表网关模型中使用 prepareStatementForSqlObject(),您需要一个 \Zend\Db\Sql\Sql 的实例,并且要获得它,您需要一个适配器。

    $adapter = $this->tableGateway->getAdapter();
    $sql = new Sql($adapter);
    $statement = $sql->prepareStatementForSqlObject($select);
    $result = $statement->execute();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-15
      • 1970-01-01
      • 2014-11-29
      • 1970-01-01
      • 2017-10-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多