views:

427

answers:

1

I have a problem with a query that works in phpmyadmin but not with Zend_db. You can read about the query in http://stackoverflow.com/questions/1268376/sql-statement-with-several-joins

I have these tables

Post
    ID
    entry
Category
    ID
    name
CategoryBinding
    ID
    postID
    categoryID

And this is my php code:

    class Blog_Model_DbTable_Posts extends Zend_Db_Table
    {
        protected $_name = 'Posts';
    public function getByCategoryId($id)
    {
         $query = 
    "SELECT * 
FROM `Post` AS `p` 
LEFT JOIN `CategoryBinding` AS `cb` ON p.ID = cb.postID 
LEFT JOIN `Category` AS `c` ON cb.categoryID = c.ID
INNER JOIN `Post` AS `p2` ON p.id = p2.id
WHERE p.id in
(
    SELECT p2.id
        FROM `Post` as `p2`
        LEFT JOIN `CategoryBinding` AS `cb` ON p2.ID = cb.postID 
        LEFT JOIN `Category` AS `c` ON cb.categoryID = c.ID
        WHERE c.id = 1
    )
    "
return parent::fetchAll($query);

I have tried to run this query with boath the PDO adopter and with mysqli adopter. I get this error with the mysqli adopter.

Mysqli prepare error: Operand should contain 1 column(s)

I have googled about it and found that it maybe is a bug in Zend_Db, but since my sql knowledge is somewhat limited I do not know if I am doing something wrong or if it indeed is a bug. Can you please help me figuring this out? Tank You.

+3  A: 

Instead of

parent::fetchAll($query);

Use

$this->getAdapter()->fetchAll($query);
Mark
Correct. The Table `fetchAll()` method is different from the Adapter `fetchAll()` method. The latter takes an complete SQL statement. The former takes either an SQL expression to use in a `WHERE` clause, or else an object of type Zend_Db_Table_Select.
Bill Karwin
Thank you, I don't think that I ever would been able to solve this without your help.
unkown