My table looks something like this

    id      uniqueId    userId  album
    1       1           1       Example
    2       2           1       Example
    3       1           2       Example
    4       3           1       Example
    5       2           2       Example

I would like to find the Album Title where userId => 1 and uniqueId => 3 (Where id => 4, but I won't have access to that, only the other two).

To do this I am attempting to getAlbumsByUserId($userId) which will return a RowSet object with User 1's Albums. Then I am trying to get the album from the returned rowset with uniqueId => 3

Something like the below won't work
$albumObject = $albumTable->getAlbumsByUserId(1);
$row = $albumObject->select(array('uniqueAlbumID' => 3))->current();

This is the function which gets Albums by UserId as a RowSet

public function getAlbumsByUserId($userId)
    {
        $userId     = (int) $userId;
        $rowset = $this->tableGateway->select(array('user_id' => $userId));

        return $rowset;
    }

I could do a foreach on the RowSet object, to find the uniqueId with an if statement, but I have to think there is a better way.

Edit:
I still haven't found a way to do a select from resultSet, but you can do 2 where conditions in the select array.

有帮助吗?

解决方案

Maybe i don't understand what do you want exactly but how about making your query statement with join you have nice set of functions to accomplish that in Zend.

In your AlbumTable.php:

public function getAlbum($userId, $uniqueAlbumId){
    $resultSet = $this->tableGateway->select()->where(array('userId = 1', 'uniqueId = 3'));
    $row = $resultSet->current();
    if(!$row){
         throw new Exception('No row found');
   }
    return $row;
}

其他提示

First, I believe that the rowset returned is an iterator that you can use a foreach on and iterate through each of the rows. However, if you want to return an album of a certain ID value, then you would need to add a function something like:

public function getAlbumById($id)
{
  $rowset = $this->tableGateway->select(array('id' => $id));
  $row = $rowset->current();
  return $row;
}

You can check out ZF's site about tablegateway here: http://framework.zend.com/manual/2.0/en/modules/zend.db.table-gateway.html

and information about RowSet is here: http://framework.zend.com/manual/1.12/en/zend.db.table.rowset.html

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top