Zend relationship with the choice

I am new to zend. I was asked to remake a website that was once written in plain PHP and put it in a zend environment.

I have a lot of database relationship issues, I don't seem to know how to identify and query relationships.

I would like to find a category. From this category, I would like to find all the categorization associated with it and be able to query / sort / restrict this dataset.

Here are my models.

Categorys.php

<?php class Default_Model_Categorys extends Zend_Db_Table_Abstract { protected $_name = 'Categorys'; protected $_primary = 'id'; protected $_dependentTables = array('Default_Model_CategoryInfo'); } ?> 

CategoryInfo.php

 <?php class Default_Model_CategoryInfo extends Zend_Db_Table_Abstract { protected $_name = 'Category_Info'; protected $_primary = 'id'; protected $_referenceMap = array( 'Categorys' => array( 'columns' => array('cat_id'), 'refTableClass' => 'Default_Model_Categorys', 'refColumns' => array('id') ) ); } ?> 

CategoryController.php

 <?php class CategorysController extends Zend_Controller_Action { public function indexAction() { /* this should redirect to all games */ return $this->_forward("index", "games"); } public function categoryAction() { /* shows a specific category */ $id = (int) $this->_request->getParam('id'); $category = new Default_Model_Categorys(); $this->view->category = $category->fetchRow( $category->select()->where('id = ?', $id) ); $categoryInfo = $this->view->category->findDependentRowset('Default_Model_CategoryInfo'); } } 

First ... am I doing something wrong?

Secondly ... how do I access the dependent rowset?

+4
source share
2 answers

First, if you are looking for a category by its primary key, it is easier to use the find() method:

 $id = (int) $this->_request->getParam('id'); $category = new Default_Model_Categorys(); $this->view->category = $category->find($id)->current(); 

Secondly, to limit or sort dependent Category_Info strings, you can use the Zend_Db_Table_Select object as an optional findDependentRowset() parameter. Here is an example:

 $select = $category->select()->where("info_type = 'PRICE'") ->order("info_date") ->limit(3); $categoryInfo = $this->view->category->findDependentRowset( 'Default_Model_CategoryInfo', null, $select); 

Note that you can use any table object to create this select object. Since the FROM for this selection will be set using the findDependentRowset() method, you simply add other clauses and then pass it.

PS: You don’t need to declare $_dependentTables at all unless you intend to use cascading updates or cascading deletes via PHP code. I highly recommend doing this - it’s much more efficient to allow RDBMS to handle these cascading operations.

You also do not need to declare $_primary if the primary key constraints are indeed declared in the database tables. Zend_Db_Table_Abstract knows how to check metadata to get primary key columns.

+2
source

Everything looks right to me. You are not querying a dependent rowset. This is the query itself, and it returns a result set. Basically what it does is pull all the records associated with the current line you're working with, as defined by $ _referenceMap. After you run findDependentRowset (), you can see the results, which will give you examples of Zend_Db_Table_Row. From there, you can display the relevant data as needed.

Personally, I do not use the Zend_Db relationship. It is much simpler to just make a second model method to query what I need. In addition, Zend_Db relationships do not support where clauses, so just make the second request much more flexible than relationships.

0
source

All Articles