Hi,
It is not exactly the easiest to follow exactly what you have a
problem with. Guess that may be why no-one has come to the rescue.
You should strip your code and your question down to the root issue.
This makes it much easier to follow your reasoning, plus it show that
you have "done your homework", which is always a good thing.
For example. Issues with saving HABTM records has nothing to do with
Ajax. If it works without Ajax then you probably have a problem
related to Ajax and not specifically HABTM associations.
What specifically are you trying to save? (Model+assoc?, just the
assoc?, multiple assoc?)
What is the association you are having a problem with? (I.E. if User
HABTM Favorite why mention Eitity at all?)
Is the association working? (I.E. manually pushing data into the
database. Does it link up OK in the Models?)
Also, if you make a test setup without ajax, behaviors and anything
but form->controller->model you have eliminated a bunch of variables
and probably made the code more readable to others.
try to clarify and hopefully I (or someone else) may be abel to
understand your problem and be of help.
/Martin
On Jul 7, 9:07 pm, "Novice Programmer" <[EMAIL PROTECTED]>
wrote:
> No one to the rescue?
>
> Thanks.
>
> On Mon, Jul 7, 2008 at 9:50 PM, Novice Programmer <[EMAIL PROTECTED]>
> wrote:
>
>
>
> > Hello again,
>
> > I am stuck with the HABTM save/retrieval.. Please help.. I have got three
> > models.
>
> > 1. Entitiy (id auto_increment)
> > 2. User (id auto_increment)
> > 3. Favorite(id auto_increment, entity_id)
>
> > A user can have many favorite entities and a favorite can belong to many
> > users.. so i have created a HABTM table users_favorites (user_id,
> > favorite_id).
> > I have defined the relationship as follows in the User Model.
>
> > var $hasAndBelongsToMany = array(
> > 'Favorite' =>
> > array('className' => 'Favorite',
> > 'joinTable' => 'users_favorites',
> > 'foreignKey' => 'user_id',
> > 'associationForeignKey' => 'favorite_id',
> > 'conditions' => '',
> > 'order' => '',
> > 'limit' => '',
> > 'unique' => false,
> > 'finderQuery' => '',
> > 'deleteQuery' => '',
> > 'insertQuery' => ''
> > )
>
> > I provide an AJAX link to the user to favorite an entity like
> > http://<something.com>/users/favorite/entity_id<http://%3Csomething.com%3E/
> > users/favorite/entity_id>
> > .
>
> > In the users_controller, I have written teh following.
>
> > $user_id = $this->Auth->user('id');
> > $this->User->Behaviors->Attach(Behavior);
> > *save_data['User']['entity_id']=$entity_id;*
> > $this->User->save($save_data);
>
> > In the favorites behavior:
>
> > *function beforeSave($&model) {*
> > *$Favorite = new $Favorite;*
> > **
> > *if ($res = $Favorite->find($model->data['User']['entity_id'])) {*
> > *$id = $res['Favorite']['id'];*
> > *}*
> > *else {*
> > *$Favorite->save(array('id'=>"", 'entity_id' =>
> > $model->data['User']['entity_id']));*
> > *$id = $Favorite->getLastInsertID();*
> > *}*
> > *$model->data['Favorite']['id'] = $id;*
> > *}*
> > *}*
>
> > The above said code works quite well.. but my queries here revolove around
> > the hacks to get the HABTM working:
>
> > 1. If i change $*save_data['User']['entity_id']=$entity_id; *to
> > $*save_data['Favorite']['entity_id']=$entity_id;
> > *and also at other places in the before save in the behavior, then weird
> > things happen.. now why would i want to set the user's field because
> > entity_id is actually a part of favorite model.
> > 2. Why do i need to write a behavior to generate the linked model and then
> > fill the id. Isn't there some thing that does it automagically.. i am sure
> > that i am missing some thing...
>
> > Thanks in Advance.
>
> > --
> > Thanks & Regards,
> > Novice.
>
> --
> Thanks & Regards,
> Novice.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---