我找到一个解决方案有点麻烦..
Error: Call to a member function schema() on a non-object File: /Cake/Model/Model.PHP Line: 3627
在我的数据库中有表格文章,主题标签和关联articles_hashtags与foreignkeys article_id和hashtag_id ..所以我想知道每篇文章有哪些主题标签..
我的文章模型
class Article extends AppModel { public $hasAndBelongsToMany = array( 'Hashtag' => array( 'className' => 'Hashtag','joinTable' => 'articles_hashtags','foreignKey' => 'article_id','associationForeignKey' => 'hashtag_id','unique' => true,'conditions' => '','fields' => '','order' => '','limit' => '','offset' => '','finderQuery' => '','with' => '' ) ); }
class ArticleController extends AppController { var $name = 'Article'; public $helpers = array("Html","Form"); public function index() { $this->set("posts",$this->Article->find("all")); } }
谢谢你的帮助!!
另外:
如果我将生成的sql select查询从调试器sql日志放入我的sql数据库中,我得到了正确的结果..所以我猜控制器出了问题?!
我有同样的问题,所以可以将$hasAndBelongsToMany剥离到最小键.
原文链接:https://www.f2er.com/php/135306.html问题是你在$hasAndBelongsToMany数组上使用’with’键.删除它或将其设置为’articles_hashtags’:
class Article extends AppModel { public $hasAndBelongsToMany = array( 'Hashtag' => array( 'className' => 'Hashtag','with' => 'articles_hashtags' ) ); }
库模型代码试图访问’with’键的值,该键为空,因此是非对象错误.
From CakePHP docs,关于$hasAndBelongsToMany数组:
- with: Defines the name of the model for the join table. By default CakePHP will auto-create a model for you. Using the example above it would be called IngredientsRecipe. By using this key you can override this default name. The join table model can be used just like any “regular” model to access the join table directly. By creating a model class with such name and filename,you can add any custom behavior to the join table searches,such as adding more information/columns to it.