Home » Php » php – CakePHP Fatal Error Call to a member function schema() on a non-object

php – CakePHP Fatal Error Call to a member function schema() on a non-object

Posted by: admin July 12, 2020 Leave a comment

Questions:

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..

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?!

How to&Answers:

I had the same problem so stripped down the $hasAndBelongsToMany to minimum keys possible.

The problem is that you are using the ‘with’ key on the $hasAndBelongsToMany array. Remove this or set it to ‘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.

From CakePHP docs, regarding $hasAndBelongsToMany array:

  • 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.

Answer:

Have you initialized object of model Article in your controller? You can do this by using field $uses in controller. Add following line in ArticleController:

public $uses = array('Article');

Answer:

Controllers names are plural

You dont need declare the model Article in ArticlesController, the cake’s magic do it for you.