1

I'm trying to link two (and more) models in a CakePHP application.

I have two database tables: clients and notes. I have the following model classes:

<?php
class Client extends AppModel {
    var $name = 'Client';
    var $hasMany = 'Note';
}

And:

<?php
class Note extends AppModel {
    var $name = 'Note';
    var $belongsTo = 'Client';
}

However, I don't think they're linking. When I run print_r($this->Client->find()); in a controller I get the following output:

Array
(
    [Client] => Array
        (
            [id] => 1
            [name] => Martin Bean
        )

)

How can I also fetch the associated Note records for each Client?

EDIT: Despite having the above in my model classes, if I place the following in a controller:

$this->Client->Behaviors->attach('Containable');
print_r($this->Client->find('all', array('contain' => 'Note')));

I get the following error message:

Warning (512): Model "Client" is not associated with model "Note" [CORE/cake/libs/model/behaviors/containable.php, line 363]

Have I missed something that isn't explained in the CakePHP cookbook?

EDIT 2: I don't know if this is any help; it's the debug output:

ContainableBehavior::containments() - CORE/cake/libs/model/behaviors/containable.php, line 363 ContainableBehavior::beforeFind() - CORE/cake/libs/model/behaviors/containable.php, line 121 ModelBehavior::dispatchMethod() - CORE/cake/libs/model/model_behavior.php, line 169 BehaviorCollection::trigger() - CORE/cake/libs/model/model_behavior.php, line 494 Model::find() - CORE/cake/libs/model/model.php, line 2108 PostsController::index() - APP/controllers/posts_controller.php, line 6 Dispatcher::_invoke() - CORE/cake/dispatcher.php, line 204 Dispatcher::dispatch() - CORE/cake/dispatcher.php, line 171 [main] - APP/webroot/index.php, line 83

2
  • 1
    Try clearing your model cache as well APP/tmp/cache/models - especially if you added these changes after initially creating and running these files. Sometimes it's all it takes ~
    – Ross
    Jul 24, 2011 at 19:34
  • Also check your (app_)model for any trace of $this->recursive - if it's less than 0 you won't get related data.
    – Ross
    Jul 24, 2011 at 19:35

3 Answers 3

1

So just to confirm

You have two tables 'notes' and 'clients'

Both have a primary key named id

'notes' contains 'client_id'

There is correct test data, 'client' with id 1 is on several 'note' rows

Before your find() call there is not a $this->Client->recursive = -1 assignment.

Add

$var actsAs = array('Containable');

to both your Client and Note model or just add it to your app_model.php to attach the containable behaviour to every model automatically.

P.S Set debug to 2 to clear our the model cache if you have recently added a new table or amended a field.

3
  • That's correct. I've set debug level to 2, flushed the cache, and added the $actsAs declaration to app_model.php (which I moved to my app/models/ directory, yet still getting the Model "Client" is not associated with model "Note" error message. Jul 24, 2011 at 20:23
  • Have you dropped the attach behaviour call? This is perculiar
    – Leo
    Jul 24, 2011 at 20:41
  • I've dumped the contents of my controller and related models here: gist.github.com/1103085. Hopefully this can show any problems. Jul 24, 2011 at 20:46
0

look up containable. $this->Client->find('all', array('contain' => array('Note')));

2
  • are these models in a plugin by chance?
    – dogmatic69
    Jul 24, 2011 at 18:47
  • 1
    if containable is saying that, either the files are named incorrectly or some permissions error. cake will create model instances on the fly without a file and seems what the problem is here. do pr(get_class($this->Client)) and it will say AppModel. then you know the above is true. if that says Client maybe the Note is the problem
    – dogmatic69
    Jul 24, 2011 at 18:49
0

Fixed it! My models' filenames were incorrect.

Instead of client.php or note.php I had saved them as client_model.php and note_model.php. All works fine now!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.