gpt4 book ai didi

php - 如何将附加变量传递给匿名 TableGateway 方法参数?

转载 作者:行者123 更新时间:2023-11-29 01:55:09 24 4
gpt4 key购买 nike

我有我的模型表,一切正常,但我试图将一些变量传递给函数,但无法识别该变量。

我在 Application/Model/someTable.php 中的函数

/**
* @param array $myWhere
* @param $myOrder
* @return \Zend\Db\ResultSet\ResultSet
*/
public function fetchAllPagination(Array $myWhere,$myOrder)
{

$resultSet = $this->tableGateway->select(function(Select $select){

$select->where(function(Where $where){
$where->equalTo($myWhere[0],$myWhere[1]);
});

$select->order($myOrder);
});
return $resultSet;
}

在 Controller 中调用它

    $all = $someTable->fetchAllPagination(array('id_user'=>$identity->users_id),'insert_date ASC');

问题是变量 $myWhere$myOrderfunction(Select $select) 中无法识别,我有一个SQL 错误:

Statement could not be executed (42000 - 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= NULL ORDER BY ASC' at line 1)

我该如何解决这个问题?有什么想法吗?

最佳答案

这里与ZF2无关。由于 select()where() 方法的参数是 closure 的实例, $myWhere$myOrder 参数都超出了范围。闭包无法访问其定义或执行的范围。

无论如何,您可以尝试使用 use 关键字(语言结构)传递附加变量,如下所示:

$resultSet = $this->tableGateway->select(
function(Select $select) use ($myOrder) {
$select->where(
function(Where $where) use ($myWhere) {
$where->equalTo($myWhere[0],$myWhere[1]);
});
$select->order($myOrder);
});

您可能想阅读更多关于 anonymous functions 的信息在官方文档中。

关于php - 如何将附加变量传递给匿名 TableGateway 方法参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31146342/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com