1.有兩個Table,一個是News另一個是News_Category.
2.News.category_id跟News_Category.id為關連欄位.
3.我們在承繼了Zend_Db_Table_Abstract的class進行關連設定:
class News_Category extends Zend_Db_Table_Abstract
{
protected $_name='News_Category';
}
class News extends Zend_Db_Table_Abstract
{
protected
$_name='News',
$_referenceMap=array('Category'=>array(
'columns'=>'category_id',
'refClass'=>'News_Category',
'refColumns'=>'id'));
}
{
protected $_name='News_Category';
}
class News extends Zend_Db_Table_Abstract
{
protected
$_name='News',
$_referenceMap=array('Category'=>array(
'columns'=>'category_id',
'refClass'=>'News_Category',
'refColumns'=>'id'));
}
4.於Controller中進行資料提取,例子中將會取得類別中所有新聞:
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
$category=new News_Category();
$row=$category->find(1)->current();
$info=$row->findDependentRowset('News');
exit(var_dump($info)); //直接瀏覽結果.
}
}
{
public function indexAction()
{
$category=new News_Category();
$row=$category->find(1)->current();
$info=$row->findDependentRowset('News');
exit(var_dump($info)); //直接瀏覽結果.
}
}
5.洐生出來的問題就是如果該類別的新聞有幾千萬筆,那麼該怎辦?方法如下:
$info=$row->findDependentRowset('News',null,$row->select()->limit(20,0));
6.在一般的情況下,如果要進行分頁,很多時都會使用到sql的count(*),以下方式可以取得總行數(尚未測試使用這個方式的效率如何):
$amount=$row->findDependentRowset('News')->count();
如果category中有parent id,並要進行關連,只要在category自身加入一個reference map,將class指向自已就可以了。
沒有留言:
張貼留言