简体   繁体   中英

Custom sql query in my existing ZF2 (Zend Framework 2) entity

I have a class that already previously had a simple fetchAll() method. I got it from the ZF2 tutorial and it uses ResultSet and 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.

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:

    $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:

     '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? 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

public function select($where = null);

So it should be something like this:

$resultSet = $this->tableGateway->select(array('customerId' => $CCID));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM