Search code examples
zend-frameworkzend-db-table

zend framework subquery


I am using zend framework 1.12. I have following query to run.

"SELECT name,(select count(*) from  org_quote_template_items where org_quote_template_items.quote_template_id = org_quote_templates.`id` ) as total_line_item FROM `org_quote_templates`"

In my model file , I created it like this. following is my model file.

    class default_Model_DbTable_QuoteTemplates extends Zend_Db_Table_Abstract
{
    /**
     * Name of the original db table
     *
     * @var string
     */
    protected $_name = 'org_quote_templates';


    public function getAllTemplate($where){
        $select = $this->select();        
        $subquery = " (SELECT COUNT(*) FROM  org_quote_template_items WHERE org_quote_template_items.quote_template_id = org_quote_templates.`id` )";

        $select->from(array($this), array('org_quote_templates.*','total_line_items' => new Zend_Db_Expr($subquery)));

        $select = $select->where('organization_id = ?',$where['org_id']);

        $adapter = new Zend_Paginator_Adapter_DbSelect($select);
        $paginator = new Zend_Paginator($adapter);
        $paginator->setItemCountPerPage(
                Zend_Registry::get('config')->paginator->general);
        pr($adapter);
        exit;
    }
}

I am getting following error when I run the code. " exception 'Zend_Db_Table_Select_Exception' with message 'Select query cannot join with another table' "

please let me know what should I do ?


Solution

  • There is an error in your request. You should have:

        $select = $this->select ();
        $subquery = "(SELECT COUNT(*) FROM  dtempls WHERE order_id = orders.id)";
    
        $select->from ($this, array (
            'id',
            'total_line_items' => new Zend_Db_Expr ($subquery)
        ));