0

I have this model:

class Application_Model_Categories extends Zend_Db_Table_Abstract
{
    protected $_name = 'categories';
    protected $_referenceMap = array(
            'Stores' => array (
            'columns' => 'store_id',
            'refTableClass' => 'Application_Model_Races',
            'refColumns' => 'id')
        );
 }

Then in my controller:

$race = new Application_Model_Races();
$find = $this->race->find(1);
$current = $find->current();
$categories = $current->findDependentRowset('Application_Model_Categories');

this is returning all the categories. i need to apply a filter to return only the categories with parentId = 0

I´m new to ZF1 so if you also see that im getting the data incorrectly in the controller please let me know. thank you

handsome
  • 2,335
  • 7
  • 45
  • 73

1 Answers1

0

Simply,

$race = new Application_Model_Races();
$find = $this->race->find(1);
$current = $find->current();
$select = $race->getAdapter()->select()->where('parentId = 0');
$categories = $current->findDependentRowset(
    'Application_Model_Categories', 
    null, 
    $select
);

By providing a Zend_Db_Select object as third argument to findDependentRowset table row method call, you can add as much conditions as you want (even adding limit, setting order, ...).

Edit

Well, you should create a custom Row Class, name it Application_Model_Race for example, or Application_Model_RaceRow.

class Application_Model_RaceRow extends Zens_Db_Table_Row_Abstract
{

    public function getParentCategories()
    {
        $select = $this->getTable()->select()->where('parentId = 0');
        return $this->findDependentRowset(
            'Application_Model_Categories', 
            null, 
            $select
        );
    }

}

Add a _rowClass to your Application_Model_Races class.

class Application_Model_Races extends Zend_Db_Table_Abstract
{

    protected $_rowClass = 'Application_Model_RaceRow';

    /** Your old code **/

}

Hope it helps

php-dev
  • 6,998
  • 4
  • 24
  • 38
  • I tried that but i´m getting: Catchable fatal error: Argument 3 passed to Zend_Db_Table_Row_Abstract::findDependentRowset() must be an instance of Zend_Db_Table_Select, instance of Zend_Db_Select given – handsome Oct 24 '14 at 01:57
  • @handsome Remove the `->getAdapter()`, it should appear like `$race->select()` ... – php-dev Oct 24 '14 at 09:42
  • Thank you! that works. is there any other way to do this? i mean.. get the categories with parent_id = 0 directly from the model insteado of having that logic in the controller? thanks again! – handsome Oct 25 '14 at 00:21
  • also echo $race->select()->where('parent_id = 0'); outputs SELECT `races`.* FROM `races` WHERE (parent_id = 0) which makes no sense. – handsome Oct 25 '14 at 00:51
  • @handsome I updated the answer. Regarding the `SELECT races.* FROM races WHERE (parent_id = 0)`, don't carry about that. Zend will set the accurate `Table` to select from for you. – php-dev Oct 25 '14 at 11:33