微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我找到一个解决方案有点麻烦..
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','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.

原文地址:https://www.jb51.cc/php/135306.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐