Question

i'm new to zend framework, in this simple function i want to get a single 'post' and then i want to find all the comments in the related table

public function getPost($idPost)
{
    $db=  Zend_Registry::get('db');

    $select=$db->select()
            ->from($this->_name, '*')
            ->where("idPost= ".$db->quote($idPost, 'INTEGER'));

    $stmt=$select->query();
    $rowset=$stmt->fetchAll();
    $post=$rowset->current();

    //ora devo aggiungerci i commenti che questo post ha ricevuto
    $comm=$post->findDependentRowset('commenti');

    $ris=array($post, $comm);

    return $ris;

}

in my index controller i i simply call this function, but i get this error:

Call to a member function current() on a non-object in C:\xampp\htdocs\...

where's the mistake?

Was it helpful?

Solution

I think you have a few misconceptions about how you're using Zend_Db.

1. You're not using the ORM, just the PDO wrapper

Which means, your queries won't return Zend rowsets and rows and therefore you can't use the methods of you can use on those.

2. The default fetch mode

The default fetch mode of the Zend_Db_Statement fetchAll() method is array, if you want it to return an object (stdClass), change the fetch mode before fetching the data:

$stmt->setFetchMode(Zend_Db::FETCH_OBJ);

3. Using fetchAll() when you actually want one row

If you just want one row, then don't fetch a whole table! With Zend_Db_Statement, use for example:

$row = $stmt->fetch();

or

$rowObj = $stmt->fetchObject();

... again, that's not a zend row object, just a stdClass instance, but you can do:

$rowObj->some_field;

on it.

On the other hand, if this is a method in your Post model, it should look something like:

public function getPost($idPost)
{
    return $this->getRow($idPost);
}

This will return the post, then, if you've setup the table relationships correctly, you can also query for the dependent data or just get all comments with that id separately.

OTHER TIPS

The problem is that unless you define a table class as was previously mentioned you can't uuse the dependent or parent rowsets.

To make your current function work would be best done with two functions, and keep it simple:

public function getPost($idPost)
{
    $db= new Zend_Db_Table($this->_name);

    $select=$db->select()
            ->where("idPost= ?", $idPost);
    /*Fetch just the row you want, or use fetchAll() if you need to match return types*/   
    $row = $db->fetchRow($select);

    return $row;

}

public function getComments($table='comments', $id) {

    $db = new Zend_Db_table($table);

    $select = $db->select()->where('post_id = ?', $id)->order('date ASC');
    $rowset = $db->fetchAll($select);

    return $rowset/* or you could return an array ->$rowset->toArray() */

}

Zend_Db_Table is going to attempt to use the current database adapter, so all you need to do is pass in the tablename.

One more note: you don't need to use any of the quote() function when using select() it's taken care of.

But it is really important, that if you are going to use Zend_Db, you need to learn about "Defining table classes". At least enough to use them in your own classes.

I hope this helps!

To get a rowset and dependent rowset you have to use Zend_Db_Table. You only use the Zend_Db_Adapter with Zend_Db_Select.

Read from here. So you have to define a class which extends from Zend_Db_Table_Abstract.

Example:

class Bugs extends Zend_Db_Table_Abstract
{
    protected $_name = 'bugs';
    protected $_primary = 'bug_id';
}

To get the Zend_Db_Table_Rowset object use:

$bugs   = new Bugs();
$rowset = $bugs->fetchAll("bug_status = 'NEW'");

To find dependent rowsets you have to define the relation in your table class. Look here how to define relationships.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top