加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 18:18:08 所属栏目:PHP教程 来源:网络整理
导读:我找到一个解决方案有点麻烦.. Error: Call to a member function schema() on a non-objectFile: /Cake/Model/Model.phpLine: 3627 在我的数据库中有表格文章,主题标签和关联articles_hashtags与foreignkeys article_id和hashtag_id ..所以我想知道每篇文章
我找到一个解决方案有点麻烦..
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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读