Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 602
tobysas Avatar asked Sep 11 '14 22:09

tobysas


1 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.
like image 96
Sam Avatar answered Oct 09 '22 11:10

Sam