0

I use HABTM to populate database. I have 3 models. User, Book, Comment. To join records in the tables books and users I use in HABTM users_books table, and to bind comments I use 2 primary keys, user_id and book_id, which both are set as foreign key:

PRIMARY KEY (user_id, book_id)
FOREIGN KEY (user_id, book_id) REFERENCES users_books (user_id, book_id)

Table books, and users_books are being populated but in some cases I can get the comment table populated but only with user_id and book_id, other data is NULL. Or Iif I remove user_id (Comment array) from the saveAll query I got:

Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails 

So I guess it is either against the cakephp conventions or I am doing something wrong.

Here is the part of Model Book:

  public $hasAndBelongsToMany = array(
  'User' =>
  array(
    'className' => 'User',
      'joinTable' => 'users_books',
        'foreignKey' => 'book_id',
        'associationForeignKey' => 'user_id',
        'unique' => true,
    'conditions' => '',
    'fields' => 'book_id',
    'order' => '',
    'limit' => '',
    'offset' => '',
    'finderQuery' => '',

),
'Comment' =>
array(
  'className' => 'Comment',
  'joinTable' => 'comments',
  'foreignKey' => 'book_id',
  'associationForeignKey' => 'user_id',
  'unique' => true,
  'conditions' => '',
  'fields' => '',
  'order' => '',
  'limit' => '',
  'offset' => '',
  'finderQuery' => '',),);

And Comment:

public $belongsTo = array (
'Book' => array (

'foreignKey'=>false,
'conditions'=> array('Comment.book_id = UserBook.book_id')));

edit:

Data to save

$this->request->data['Book']['title'] = "title".rand(1,1220);
$this->request->data['Book']['author'] = "author".rand(1,1220);
$this->request->data['User']['id'] = $this->Auth->user('id');
$this->request->data['Book']['Comment']['body'] = "cm: ".rand(1,1220);
$this->Book->save($this->request->data, array('deep'=>TRUE));

And the save call:

  $this->Book->saveAll($this->request->data, array('deep'=>TRUE));
pt0
  • 175
  • 1
  • 14

1 Answers1

1

I think that the request data array is not in the correct form for cakephp to save correctly. Can you post the array? you can add this line before save() function:

pr($this->request->data);

See this link: Saving HABTM

Laura
  • 71
  • 1
  • `Array ( [Book] => Array ( [title] => title125 [author] => title625 ) [User] => Array ( [id] => 4 ) [Comment] => Array ( [body] => cm: 507 ) )` – pt0 Feb 06 '15 at 12:51
  • I think the problem here is mulitiple indexing keys in one table. – pt0 Feb 06 '15 at 22:34
  • in my opinion the problem is inside the model Comment. I understand it's a join table, but the primary keys must be comment_id and book_id and then you will have another foreign key called user_id that is the reference id of a user. If I understand well you don't need to use a HABTM model but a Has Many Through model because you don't have only two primary keys inside the model but also another information of the user. See reference: [link] (http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasmany-through-the-join-model) – Laura Feb 09 '15 at 08:31
  • Thank you for pointing this out. I thought the HABTM was enough to set a connection, but the right answer was the HMT and I had malformed data, while I tried to save it to the database. So I vote the first of your answers. Cheers – pt0 Feb 09 '15 at 12:56