Thursday, May 15, 2014

Multiple HABTM to the same Model

Hi folks,

im goggling for this a few days but can't find a clear solution for my problem.
So what i am trying to do is quit simple.
I created a Container model and this $actsAs tree.

Container Database:
id, containertype_id, name, parent_id, lft, rght

Now i want to create a Contact. A Contact $hasAndBelongsToMany Container over contacts_to_containers:
A Contact can be created in a Container.containertype_id 1 and 2
        public $hasAndBelongsToMany = array(
                'Container' => array(
                        'className' => 'Container',
                        'joinTable' => 'contacts_to_containers',
                        'foreignKey' => 'contact_id',
                        'associationForeignKey' => 'container_id'
                ),
         ),

No i want to create a contact group. And i want that a contact group is still a Container in a Container and this is the big problem.
So i created a Contactgroup model and a contact groups table. The table just save a name an a description at the moment.
A Contactgroup should be a Container.containertype_id = 6
        public $belongsTo = array(
                'Container' => [
                        'dependent' => true,
                        'foreignKey' => 'container_id',
                        'className' => 'Container'
        ]);

If i now call $this->Contactgroup->save() CakePHP create a new record in Container and link it correctly to contactgroups.container_id. This is fine. But cake don't create a new link in contacts_to_containers (to the new container id witch is the contact group)

I tried it with my own save function but than cake wants to validate all the required files of a contact. So i think cake thinks it needs to save a complete new contact but i just want that cake adds a new record for contact X in contacts_to_containers

An other problem is, that if i define conditions in $hasAndBelongsToMany like this:
        public $hasAndBelongsToMany = array(
                'Container' => array(
                        'className' => 'Container',
                        'joinTable' => 'contacts_to_containers',
                        'foreignKey' => 'contact_id',
                        'associationForeignKey' => 'container_id'
                        'conditions' => ['containertype_id', =>  1 ]
                ),
         ),

and run $this->Contact->find('all'); cake only select the containers with containertype_id === 1. Thats perfekt! But, if i now run $this->Contact->save($this->reuqest->data); cake can't save this date and throws the error containertype_id not found in contacts_to_containers. Thats true because this column is in the container, but why does this work in find() but not on save() ?

I hope i could explain my problem that someone was able to understood^^

Best regards,

Daniel

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