I found a solution:
$condition = '';
if ($tag) {
$id = $this->Content->Tag->field('id', array('Tag.slug' => $tag));
$condition = array('ContentsTag.tag_id' => $id);
$this->Content->bindModel(array('hasOne' => array('ContentsTag')));
}
$this->Content->Behaviors->attach('Containable');
$posts = $this->Content->find('all', array(
'order' => 'Content.created DESC',
'fields' => array('Content.id', 'Content.active',
'Content.slug', 'Content.title', 'Content.body', 'Content.author',
'Content.comment_count', 'Content.created'),
'conditions' => array('Content.active' => 1,
'Type.name' => 'Posts', 'Lang.alias' =>
Configure::read('Config.language'), $condition),
'contain' => array(
'Type' => array('fields' => array('Type.name')),
'Lang' => array('fields' => array('Lang.alias')),
'ContentsTag' => array('fields' =>
array('ContentsTag.tag_id')),
'Tag' => array('fields' => array('Tag.name',
'Tag.slug'))
)
));
$this->set('posts', $posts);
On 18 feb, 19:45, pablosky <[email protected]> wrote:
> Hello,
>
> I use CakePhp 2.0.5.
>
> In my project I have the following relations:
>
> Content (belongTo) > Type
> Content (belongTo) > Lang
> Content (HABTM) > Tag
> Type (hasMany) > Content
> Lang (hasMany) > Content
> Tag (HABTM) > Content
>
> For a listing of records for a blog I have the following code:
>
> $posts = $this->Content->find('all', array(
> 'order' => 'Content.created DESC',
> 'fields' => array('Content.id', 'Content.active',
> 'Content.slug', ...),
> 'conditions' => array('Type.name' => 'Posts',
> 'Content.active' => 1, 'Lang.alias' =>
> Configure::read('Config.language')),
> 'contain' => array(
> 'Lang' => array('fields' => array('Lang.alias')),
> 'Type' => array('fields' => array('Type.name')),
> 'Tag' => array('fields' => array('Tag.name',
> 'Tag.slug'))
> )
> ));
>
> So I get all records such as "Post" and with the current language. But
> now I need also filter records by "tag". I tried getting the data from
> the model Tag. But that way I can not filter the records by type and
> by language.
>
> I do not know how to do this. I need help.
>
> Thanks!
--
Our newest site for the community: CakePHP Video Tutorials
http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others
with their CakePHP related questions.
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php