Wednesday, December 3, 2008

Re: save extra filed 'entity' in HABTM join tabel

If I get this correctly, you want to update the related entities from
the blogs_controller, then you need to set the associated data by
referencing it properly.

The saveAll should handle this OK for you as long as you are setting
the data the way you stated.

You could also manually do the save for the join table by doing a
separate save after you do your save for the Tag:

// for the 'add' action ...

$this->Tag->create();
if ($this->Tag->save($this->data)){
$this->data['Tag']['tag_id'] = $this->Tag-
>getLastInsertId();

$this->data['EntitiesTag']['tag_id'] = $this->data
['Tag']['tag_id'];
$this->data['EntitiesTag']['entity'] = 1;


$this->Tag->EntitiesTag->create();

// Then save the EntitiesTag ...
if ($this->Tag->EntitiesTag->save($this->data
['EntitiesTag'])) {
$user_slot_id = $this->User->UserSlot-
>getLastInsertId();


On Dec 2, 11:24 pm, bookme <rarockthewo...@gmail.com> wrote:
> Hi,
>
> I am trying this from last three days but can not solve
> problem...Please help me to find a solution to this problem.
> I want to save a extra field in HABTM Join tabel.
>
> 1) tags : tag_id, tag_name .
> 3) blogs : id, title, body
> 4) entities_tags : tag_id, entity, entity_id .
>
> I am using HABTM relation ship between Tag and Blog modal
>
> *Blog Model*
> <?php
> class Blog extends AppModel {
>
>     var $name = 'Blog';
>
>     var $hasAndBelongsToMany = array(
>                 'Tag' => array('className' => 'Tag',
>                 'joinTable' => 'entities_tags',
>                 'foreignKey' => 'entity_id',
>                 'associationForeignKey' => 'tag_id',
>                 'with' => 'EntitiesTag',
>                  'unique' => true,
>                 'conditions' => 'EntitiesTag.entity = 1',
>                ),
>          );
>
> }
>
> ?>
>
> entity_id, tag_id is saving in database successfully but entity is not
> saving in tabel.
>
> I tried following solution for this in blogs_controller
>
> 1 $this->data['EntitiesTag']['entity'] = 1
>
> 2 $this->data['EntitiesTag'][0]['entity'] = 1
>
> 3 I read one tutorialhttp://teknoid.wordpress.com/2008/09/24/saving-extra-fields-in-the-jo...
>
> I tried it but not successed.
>
> It would be gr8 for me if somebody tell how can I save this extra
> field 'entity' into Join tabel entites_tags ?
>
> Is there missing something or doing wrong?
--~--~---------~--~----~------------~-------~--~----~
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
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: