脚本宝典收集整理的这篇文章主要介绍了如何在CakePHP中的包含模型中查找类别,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
现在,我的问题是如何检索每个帖子的类别.有时,帖子在此查询中包含一个或多个类别:
$this->set('posts',$this->Category->find( 'First',array( 'condITions' => array( 'Category.uri' => $uri ),'contain' => array('Post') ) ));
我想象这样的事情:
$this->set('posts','contain' => array('Post' => array( 'contain' => 'Category' )) ) ));
这是我的模型的样子.
// Category Model class Category extends AppModel { VAR $name = 'Category'; var $hasAndBelongsToMany = array( 'Post' => array( 'classname' => 'Post' ) ); var $actsAs = array('Containable'); } // Post Model class Post extends AppModel { var $name = 'Post'; var $hasAndBelongsToMany = array( 'Category' => array( 'className' => 'Category' ) ); var $actsAs = array('Containable'); var $virtualFields = array( 'date_posted' => 'DATE_Sub(Post.created,INTERVAL 7 DAY)' ); }
示例数据如下:
categories id name 1 holidays 2 destinations posts id title 1 I am a post 2 I am another post categories_posts post_id category_id 1 1 2 2 2 1
我正在从假期中检索帖子.
Array ( [Category] => Array ( [id] => 3 [name] => holidays [uri] => holidays [created] => 2010-11-25 20:43:03 [modified] => 2010-11-25 20:43:03 ) [Post] => Array ( [0] => Array ( [id] => 1 [title] => I am a post ),[1] => Array ( [id] => 2 [title] => I am a another post ) ) )
问题是其中一个帖子分为两类.我想也有这方面的信息.
// From the CategoriesController $this->Category->Post->find('all',array( 'joins' => array( array( 'table' => 'categories_posts','alias' => 'CategoryFilter','tyPE' => 'inner','conditions' => array( 'CategoryFilter.post_id = Post.id' ) ),array( 'table' => 'categories','alias' => 'CategoryUriFilter','conditions' => array( 'CategoryUriFilter.id = CategoryFilter.category_id' ) ) ),'conditions' => array( 'CategoryUriFilter.uri' => $uri ),'contain' => array('Category') ));
返回的数组格式有点不同,但它应包含您要查找的所有数据.
以上是脚本宝典为你收集整理的如何在CakePHP中的包含模型中查找类别全部内容,希望文章能够帮你解决如何在CakePHP中的包含模型中查找类别所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。