![](/img/trans.png)
[英]Fatal error: Call to a member function save() on a non-object cakephp
[英]CakePHP Fatal Error Call to a member function schema() on a non-object
我找到一个解决方案有点麻烦..
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
剥离到最小键。
问题是你在$hasAndBelongsToMany
数组上使用'with'键。 删除它或将其设置为'articles_hashtags':
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' => 'articles_hashtags'
)
);
}
库模型代码试图访问'with'键的值,该键为空,因此是非对象错误。
从CakePHP文档 ,关于$hasAndBelongsToMany
数组:
- with :定义连接表的模型名称。 默认情况下,CakePHP将为您自动创建模型。 使用上面的例子,它将被称为IngredientsRecipe。 通过使用此键,您可以覆盖此默认名称。 连接表模型可以像任何“常规”模型一样直接访问连接表。 通过创建具有此类名称和文件名的模型类,您可以向连接表搜索添加任何自定义行为,例如向其添加更多信息/列。
您是否在控制器中初始化了模型文章的对象? 您可以通过在控制器中使用字段$ uses来完成此操作。 在ArticleController中添加以下行:
public $uses = array('Article');
你不需要在ArticlesController中声明模型文章,蛋糕的魔法为你做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.