[英]Custom sql query in my existing ZF2 (Zend Framework 2) entity
I have a class that already previously had a simple fetchAll() method. 我有一个以前已经有一个简单的fetchAll()方法的类。 I got it from the ZF2 tutorial and it uses ResultSet and TableGateway.
我是从ZF2教程中获得的,它使用ResultSet和TableGateway。 Only now I want it to take an argument that I can check for so that if a customer ID is passed, it will only return the rows with that customer ID.
直到现在,我希望它接受一个可以检查的参数,以便如果传递了客户ID,则仅返回具有该客户ID的行。
This is what the method looks like: 该方法如下所示:
public function fetchAll($CCID = null)
{
if($CCID == null){
$resultSet = $this->tableGateway->select();
return $resultSet;
} else {
// Same as above, only where customerId = $CCID
return $resultSet;
}
}
I've tried this (adding use Zend\\Db\\Sql\\Sql: 我已经尝试过此操作(添加使用Zend \\ Db \\ Sql \\ Sql:
$resultSet = $this->tableGateway->select->where(array('customerId' => $CCID));
This was pretty much a complete guess, but I tried it because it looked like it made sense. 这几乎是一个完整的猜测,但是我尝试了一下,因为它看起来很有意义。 In my Module file the TableGateway factory looks like this:
在我的模块文件中,TableGateway工厂如下所示:
'JobsTableGateway' => function($sm){
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Job());
return new TableGateway('runningjobs', $dbAdapter, null, $resultSetPrototype);
},
Does anyone know how to basically do the standard fetchAll() like in the first part of the conditional except with a 'where' part? 有谁知道如何基本地像在条件的第一部分中那样使用标准的fetchAll(),除了“ where”部分? Or maybe there's a better way to do it?
还是有更好的方法呢?
Read the docs: 阅读文档:
http://framework.zend.com/manual/2.0/en/modules/zend.db.table-gateway.html http://framework.zend.com/manual/2.0/en/modules/zend.db.table-gateway.html
public function select($where = null);
公共函数select($ where = null);
So it should be something like this: 所以应该是这样的:
$resultSet = $this->tableGateway->select(array('customerId' => $CCID));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.