Tuesday, November 13, 2012

Re: Yet another "Call to a member function find on a non-object" error

At a guess, it's the usage of the model name in the hasMany OModule order directive.  Since the User has many OModule, then the order should also reference OModule, rather than the class Module.

But a stack trace might help.

On Wednesday, 14 November 2012 13:20:11 UTC+10, Andrew Johnston wrote:
I know this post gets written every couple of months, and I swear that I've read through all of the back posts, yet can't find a solution that works.

Whenever I try to call related data from one of my models, I get the above error. Below is the relationship code for each model.

class Slide extends AppModel {
public $name = 'Slide';
public $belongsTo =  array(
'Module' => array(
'className' => 'Module',
'foreignKey' => 'module_id',
));

class Module extends AppModel {
public $name = 'Module';
public $hasMany = array(
'Slide' => array(
'className'  => 'Slide',
'foreignKey' => 'module_id',
'order' => 'Slide.position ASC',
'dependent' => true
)
);
public $belongsTo = array(
'Creator' => array(
'className' => 'User'));
public $hasAndBelongsToMany = array(
'Learner' => array(
  'className'              => 'User',
           'joinTable'              => 'modules_users',
           'foreignKey'             => 'module_id',
           'associationForeignKey'  => 'users_id',
           'unique'                 => 'keepExisting',
));

class User extends AppModel {
public $name = 'User';
public $uses = 'users';
public $hasMany = array(
'OModule' => array(
'className' => 'Module',
'foreignKey' => 'users_id',
'order' => 'Module.created DESC'
));
public $hasAndBelongsToMany = array(
'Module' => array( //We gotta remember this later...
  'className'              => 'Module',
           'joinTable'              => 'modules_users',
           'foreignKey'             => 'user_id',
           'associationForeignKey'  => 'module_id',
           'unique'                 => 'keepExisting',
));

I've tried to adhere to all of the requirements outlined in the CookBook, but I know I must be doing something wrong. Any help is appreciated. Thanks in advance.

Andrew

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
 
 

No comments: