Hello,

I aplogize for the confusing statements.. let me try to explain further

I am trying to save the model plus the assoc. User can have many favorites
but at a time he can mark only one of them as a favorite. So when a user
marks some entity as a favorite i need to perform the following things:

1. check if the entity exists in the favorites table. If it exists there
then i note the id where it exists, if it does not exist there i need to
insert that entity into the favorites table. By both the methods i get the
favorite id. and with the following statement in the favorite behavior i set
the id of the favorite.
$model->data['Favorite']['id'] = $id;

I think that there can be some thing to do this stuff automagically.

2. The association works. but it works only if i write the data as:

save_data['User']['entity_id']=$entity_id
but as you can see entity_id is not an attribute of user data, it is an
attribute of favorite table, so i should write:
save_data['Favorite']['entity_id']=$entity_id

and then call save on the user model, but when i use 'favorite' instead of
'user' nothing works at all. i am confused on why it does not work when it
is a field of Favorite only.

and yeah AJAX has got no problems at all. The problem is with how to save
model+assoc.

I hope i were clear. If not lets take an example of how you tube might
implement the favoriting of videos by the users.

Thanks.

On 7/8/08, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>
>
> 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.
> >
>


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

Reply via email to