2011-08-24 15 views
0

我通過動態更改hasAndBelongsToMany條件來選擇Blog條目,它不再適用於CakePHP 1.3中的我。 奇怪的問題,導致它與1.2工作正常,在模型中,我通過將條件與靜態ID進行測試來查看發生了什麼,(Tag.name => 'libros')。但它通過hasAndBelongsToMany條件。帶給我任何結果。通過更改模型中的條件,hasAndBelongsToMany問題

var $hasAndBelongsToMany = array('Tag' => 
         array('className' => 'Tag', 
          'joinTable' => 'blogs_tags', 
          'foreignKey' => 'blog_id', 
          'associationForeignKey'=> 'tag_id', 
          'conditions' => '', 
          'order'  => '', 
          'limit'  => '', 
          'unique'  => true, 
          'finderSql' => '', 
          'deleteQuery'=> '' 
         ) 
控制器

$this->Blog->bindModel(array(
        'hasAndBelongsToMany' => array(
         'Tag' => array('conditions'=>array('Tag.name'=>'libros')) 
      ))); 
      $this->Blog->find('all'); 

現在我沒有MySQL錯誤了,但我也有其它的記錄與他人的結果。奇怪的。

回答

0

如果你有一個正確的數據庫結構,必須使用此=

$this->Blog->bindModel(array(
         'hasOne' => array(
           'BlogsTag', 
           'FilterTag' => array(
            'className' => 'Tag', 
            'foreignKey' => false, 
            'conditions' => array('FilterTag.id = BlogsTag.tag_id') 
       )))); 
      $data = $this->Blog->find('all', array(
         'fields' => array('Blog.*'), 
         'conditions'=>array('FilterTag.name'=>'libros') 
      )); 

你可以閱讀更多關於這個在: http://book.cakephp.org/view/1044/hasAndBelongsToMany-HABTM

+0

呀它的作品!我非常感謝你,這非常有用! – user784083