2

I want to use the Sql object in my model to query the database. I have an entity object lets say Person and person object has exchangeArray() method. I want to set this Person object to be an Array Object Prototype of the ResultSet for my sql queries.

I research how to do this but the only information I found is how to set Array Object Prototype to a ResultSet object assigned to a Zend\Db\TableGateway\TableGateway.

$personEntity = new PersonEntity();
$resultSet = new \Zend\Db\ResultSet\ResultSet();
$resultSet->setArrayObjectPrototype($personEntity);
$db = $sm->get('Zend\Db\Adapter\Adapter');
$table = new \Zend\Db\TableGateway\TableGateway('table', $db, null, $resultSet);

My question is how can I set PersonEntity object to represent rows of my database that are return from a Zend\Db\Sql\Sql objects?

Thanks in advance,

Steve

2 Answers 2

4

I think the following example will answer your question:

class Foo
{
    ...
    public function fetchAll() 
    {
        $sql = new Sql($dbAdapter); // Zend\Db\Sql\Sql
        $select = $sql->select('table_foo');
        $query = $sql->prepareStatementForSqlObject($select);
        $resutl = $query->execute();
        $resultSet = new ResultSet(); // Zend\Db\ResultSet\ResultSet
        $resultSet->setArrayObjectPrototype(new EntityObject()); // <-- HERE you set your entity object
        $resultSet->initialize($resutl);
        return $resultSet;
    }
    ...
}

This code will return a ResultSet object in which every row return will be repsresented by, in this case, EntityObject.

Here is a reference to the manual that shows injection of the dataSource into a result object.

Sign up to request clarification or add additional context in comments.

Comments

3

You can set an array object prototype to any result set that you want like this:

$resultSet->setArrayObjectPrototype(new Entity);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.