简体   繁体   English

CakePHP致命错误调用非对象上的成员函数schema()

[英]CakePHP Fatal Error Call to a member function schema() on a non-object

I have some trouble finding a solution for this.. 我找到一个解决方案有点麻烦..

Error: Call to a member function schema() on a non-object
File: /Cake/Model/Model.php
Line: 3627

In my Database there are the tables articles , hashtags and the association articles_hashtags with the foreignkeys article_id and hashtag_id .. So i am trying to get the information what hashtags each article has.. 在我的数据库中有表格文章主题标签和关联articles_hashtags与foreignkeys article_id和hashtag_id ..所以我想知道每篇文章有哪些主题标签..

My Article Model 我的文章模型

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' => ''
               )  
            ); 
          }

Article Controller 文章管理员

class ArticleController extends AppController {
var $name = 'Article';
 public $helpers = array("Html", "Form");
    public function index() 
    {
    $this->set("posts", $this->Article->find("all"));

    } 
}

Thanks for your help!! 谢谢你的帮助!!

Additionally: If i put the generated sql select query from the debugger sql log into my sql database i get the right results .. so i guess theres something wrong with the controller?! 另外:如果我将生成的sql select查询从调试器sql日志放入我的sql数据库中,我得到了正确的结果..所以我猜控制器出了问题?!

I had the same problem so stripped down the $hasAndBelongsToMany to minimum keys possible. 我有同样的问题,所以可以将$hasAndBelongsToMany剥离到最小键。

The problem is that you are using the 'with' key on the $hasAndBelongsToMany array. 问题是你在$hasAndBelongsToMany数组上使用'with'键。 Remove this or set it to 'articles_hashtags': 删除它或将其设置为'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'
        )  
      ); 
   }

The library model code was attempting to access the value of the 'with' key, which was blank, hence the non-object error. 库模型代码试图访问'with'键的值,该键为空,因此是非对象错误。

From CakePHP docs , regarding $hasAndBelongsToMany array: 从CakePHP文档 ,关于$hasAndBelongsToMany数组:

  • with : Defines the name of the model for the join table. with :定义连接表的模型名称。 By default CakePHP will auto-create a model for you. 默认情况下,CakePHP将为您自动创建模型。 Using the example above it would be called IngredientsRecipe. 使用上面的例子,它将被称为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. 通过创建具有此类名称和文件名的模型类,您可以向连接表搜索添加任何自定义行为,例如向其添加更多信息/列。

Have you initialized object of model Article in your controller? 您是否在控制器中初始化了模型文章的对象? You can do this by using field $uses in controller. 您可以通过在控制器中使用字段$ uses来完成此操作。 Add following line in ArticleController : ArticleController中添加以下行:

public $uses = array('Article');

Controllers names are plural 控制器名称是复数

You dont need declare the model Article in ArticlesController, the cake's magic do it for you. 你不需要在ArticlesController中声明模型文章,蛋糕的魔法为你做。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 致命错误:在非对象cakephp上调用成员函数save() - Fatal error: Call to a member function save() on a non-object cakephp CakePHP致命错误:在非对象上调用成员函数check()? - CakePHP Fatal error: Call to a member function check() on a non-object? 致命错误:在cakephp登录应用程序中的非对象上调用成员函数create() - Fatal error: Call to a member function create() on a non-object in cakephp login application CakePHP致命错误:在非对象(控制器)上调用成员函数create() - CakePHP Fatal error: Call to a member function create() on a non-object (Controller) PHP致命错误:在非对象上调用成员函数-但仅当在CakePHP项目中使用文件时? - PHP fatal error: Call to member function on a non-object - but only when file used inside CakePHP project? 致命错误:当它是一个对象时,调用非对象上的成员函数 - Fatal Error: Call to member function on non-object when it is an object 错误致命错误:在非对象上调用成员函数insert() - Error Fatal error: Call to a member function insert() on a non-object 致命错误:在非对象错误上调用成员函数prepare() - Fatal error: Call to a member function prepare() on a non-object ERROR 致命错误:在非对象错误上调用成员函数 query() - Fatal error: Call to a member function query() on a non-object Error Magento:致命错误:在非对象上调用成员函数 getMethodInstance() - Magento : Fatal error: Call to a member function getMethodInstance() on a non-object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM