waterschaats
waterschaats

Reputation: 994

CAKEPHP multiple model field condition

I have an article model with Tags and MenuItems associated with it. I want to show Articles with a certain MenuItem id.

When a preform a find operation on the model it return an (Unknown column 'MenuItem.id' in 'field list') error.

I have know idea what i'm doing wrong.

Article Model:

 public $hasAndBelongsToMany = array(
    'Tag' => array(
        'className' => 'Tag',
        'joinTable' => 'articles_tags',
        'foreignKey' => 'article_id',
        'associationForeignKey' => 'tag_id'
    ),
    'MenuItem' => array(
        'className' => 'MenuItem',
        'joinTable' => 'menu_items_articles',
        'foreignKey' => 'article_id',
        'associationForeignKey' => 'menu_item_id',
        'unique' => 'keepExisting'
    )
);

Article Controller:

 $allArticles = $this->Article->find('all',
array(
     'fields' => array('MenuItem.id','Article.id','Article.name'),
     'conditions'=>array('Article.content_type'=>'blog','MenuItem.id'=>7),
     'recursive'=>2
 )
 );

 debug($allArticles);

Upvotes: 0

Views: 1512

Answers (1)

Krishna
Krishna

Reputation: 1540

Try this :

$this->Article->Behaviors->attach('Containable');

$allArticles = $this->Article->find('all', 
    array('contain' => 'MenuItem.id'),
          'fields' => array('Article.id','Article.name'),
          'conditions'=>array('Article.id'=>4),
          )
     );                           

debug($allArticles);

Upvotes: 1

Related Questions