I already missed one valid model association - has many through, and that solved my issue. Thanks anyway for the reply!
On Monday, February 9, 2015 at 5:25:58 PM UTC, John Andersen wrote: > > Assuming you are using CakePHP 2.x > > It is a good idea to define a primary key in your HABTM table - in the > books_users table (name should be alphabetic order, but you can deviate). > Thus your table definition is: > books_users > id, book_id, user_id > > What is your comment table reflecting? > 1) comments added to a book by a user? > 2) comments added to a book_user? > > To me it sounds like you have implemented 2) - if that is so, please > clarify your reason :) > > I would go with 1) - so that your comments table definition becomes: > comments > id, book_id, user_id, comment, ... > where book_id is a reference to the books table and user_id is a reference > to the users table. > > Enjoy, John > > > On Friday, 6 February 2015 22:46:08 UTC+2, gt0p wrote: >> >> I am trying to bind 3 tables and I am having problems with proper >> relations. >> Table users (pk id), books (pk id), users_books (user_id, book_id), >> comments (pk and foreign key book_id, user_id -> with reference to >> users_books) >> >> Here are the models: >> >> Book >> >> public $hasAndBelongsToMany = array( >> 'User' => >> array( >> 'className' => 'User', >> 'joinTable' => 'users_books', >> 'foreignKey' => 'book_id', >> 'associationForeignKey' => 'user_id', >> 'unique' => true, >> 'conditions' => '', >> 'fields' => '', >> 'order' => '', >> 'limit' => '', >> 'offset' => '', >> 'finderQuery' => '', >> // 'with' => 'UserBook' >> ),); >> >> public $hasMany = array('Comment'=>array('className'=>'Comment')); >> >> Comment: >> >> public $belongsTo = array ( >> 'Book' => array ( >> 'className'=>'Book', >> >> ) >> ); >> >> >> When I try to save with the saveAll method I got: >> >> *Error: * SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot >> add or update a child row: a foreign key constraint fails >> (`metabook`.`comments`, CONSTRAINT `comments_ibfk_1` FOREIGN KEY >> (`user_id`, `book_id`) REFERENCES `users_books` (`user_id`, `book_id`)) >> >> I know that this is not according to the cakephp conventions, but what I >> can do to bind those tables? >> > -- 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+unsubscr...@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.