Hi
I'm following this tutorial (in particular Part 4):
http://www.ibm.com/developerworks/opensource/tutorials/os-php-cake4/section4.html
it uses a join table for a many-to-many relation between Users and
Products (representing favorite products of users):
CREATE TABLE products_users (
product_id int(5) NOT NULL,
user_id int(5) NOT NULL,
PRIMARY KEY (product_id,user_id)
) ENGINE=MyISAM;
After setting the proper hasAndBelongsToMany in User and Product model,
there's a method in ProductsController that adds a favorite product to
the currently logged user (it also performs an ACL check, but that's not
important here):
function addToFavorites($id) {
$product = $this->Product->read(null, $id);
$username = $this->Session->read('user');
$success = false;
if ($this->Acl->check($username, $id.'-'.$product['Product']
['title'], 'read')) {
$result = $this->Product->User->findByUsername($username);
$product['User'] = array( 'User' =>
array($result['User']['id'])
);
$this->Product->save($product);
$success = true;
}
...
well, the favorite record is created in the join table; the problem is
that if I call this method from another user, it removes the record of
the previous user in the join table (this is also shown in the sql code
executed in the view). So is there anything wrong in the way this code
updates the join table?
thanks in advance
Lorenzo
--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
HOME: http://www.lorenzobettini.it MUSIC: http://www.purplesucker.com
BLOGS: http://tronprog.blogspot.com http://longlivemusic.blogspot.com
Check out the new CakePHP Questions site http://cakeqs.org and help others with
their CakePHP related questions.
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