I have a module with two models that extend Zend_Db_Table_Abstract:
class Departments_Model_Subpages extends Zend_Db_Table_Abstract
{
protected $_name = 'bktg_departments_subpages';
protected $_primary = 'id';
}
class Departments_Model_Departments extends Zend_Db_Table_Abstract
{
protected $_name = 'bktg_departments';
protected $_primary = 'id';
}
Reading through the documentation, what I want to do is something like this (obviously, wrong):
$depTable = new Departments_Model_Departments();
$subTable = new Departments_Model_Subpages();
$depQuery = $depTable->select()->setIntegrityCheck(false)
->from($depTable, array('id', 'title'))
->join($subTable, array('COUNT(id) as `count`'))
->where('bktg_departments.id = bktg_departments_subpages.parent_id');
What am I missing? I just can't wrap my head around Zend joins, and I'm thinking I'm going to end up writing straight sql.
Thank you for having a look.
Edit
Here's what I'm working with now:
$depQuery = $depTable->select()->setIntegrityCheck(false)
->from($depTable, array('id', 'title'))
->joinLeft($subTable, 'bktg_departments.id = bktg_departments_subpages.parent_id', array('COUNT(id) as count'))
->order('id DESC')
->group('id');
The sql that works for me:
SELECT bktg_departments.id, bktg_departments.title, count(bktg_departments_subpages.id) FROM bktg_departments left join bktg_departments_subpages on bktg_departments.id = bktg_departments_subpages.parent_id group by bktg_departments.id
And I am having issues reproducing that in Zend.