CakePHP Fatal Error Call to a member function schema() on a non-object
Asked Answered
A

3

7

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

Akee answered 11/9, 2014 at 22:13 Comment(2)
check table name seems like it is typo errorGlazed
oh no sorry that typo was just hereAkee
S
18

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.
Symposium answered 24/9, 2014 at 14:34 Comment(1)
So simple, nice work. I was getting the same and as a result of bake constructing the HABTM relationship that includes the blank with setting ('with' => ''). Hours of debugging solved. You rock.Staves
T
0

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');
Trinitrophenol answered 12/9, 2014 at 10:21 Comment(0)
M
0

Controllers names are plural

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

Mucker answered 12/9, 2014 at 14:1 Comment(1)
Are you still talking about OP?Birthday

© 2022 - 2024 — McMap. All rights reserved.