Tuesday, June 24, 2014

Re: Cakephp HABTM association is not working

Check your class name in the association you have defined:
 'className' => 'Children',

Should it not be:
 'className' => 'Child',

Enjoy, John

On Tuesday, 24 June 2014 14:29:52 UTC+3, Gregory Fox wrote:
Hi,

I need an association where therapists have many children and visa-versa. 

I have 3 tables:

therapists
children
children_therapists

You can see the association below:

Therapist.php

     public $hasAndBelongsToMany = array('Child' =>
                    array(
                                   'className' => 'Children',
                                   'joinTable' => 'children_therapists',
                                   'foreignKey' => 'therapist_id',
                                   'associationForeignKey' => 'child_id',
                                   'unique' => true,
                                   'conditions' => '',
                                   'fields' => '',
                                   'order' => 'Child.last_name',
                                   'limit' => '',
                                   'offset' => '',
                                   'finderQuery' => '',
                                   'with' => ''
                               )
        );

In the Child.php model, I tried this code:

    public $useTable = 'children';

but that didn't work.

When I code against it, I get the following error:

Missing Database Table

Error: Table childrens for model Child was not found in datasource default.

Any help is greatly appreciated.

Thanks,
Greg

--
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 unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: