2011-08-24 20 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