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.
As far as I can tell, passing an object into the table parameter doesn't give you the expected results, and always generates an error. Even a method such as $table->getName() generates errors so I was forced to type out the name manually. I don't know if I was expecting more out of the select object then it's capable of or this is a bug in 1.11. Either way, I hope this helps someone with their frustrations.
The following code works:
$depQuery = $depTable->select()->setIntegrityCheck(false)
->from($depTable, array('id', 'title'))
->joinLeft('bktg_departments_subpages', 'bktg_departments.id = bktg_departments_subpages.parent_id', array('count' => 'COUNT(bktg_departments_subpages.id)'))
->group('bktg_departments.id')
->order('bktg_departments.id DESC');