0
votes

I have a hasMany through (the join model) which is not saving, but when reading, it works naturally.

I will try to provide as much detail as possible, so bear with me. Please note I am trying to save a new user paired with quantities of existing products, and I am not wanting to add new products in this save.

Database Tables: users, products, subscribed_users

Table structure of joining table: id, user_id, product_id, quantity, created, modified

User.php

public $hasMany = array(
   'SubscribedUser'
);

Product.php

public $hasMany = array(
   'SubscribedUser'
);

SubscribedUser.php

class SubscribedUser extends AppModel {
   public $belongsTo = array(
       'User', 'Product'
   );
}

Data array I am trying to save from within the UsersController.php

Array (
    [User] => Array (
        [username] => asdf
    )
    [SubscribedUser] => Array (
        [0] => Array (
            [product_id] => 50f1ef25-6884-44cf-80e4-bd346f01a4c2
            [count] => 12
        )
        [1] => Array (
            [product_id] => 50f1f1d6-89b4-4ade-961f-c1a06f01a4c2
            [count] => 12
        )
    )
)

Using $this->User->SubscribedUser->saveAll($data); I have the User properly saved, but my subscribed_users table only has one row (as you can see from my above data, there should be two) and it contains missing data. There is no product_id, and the count number is 0.

I am not sure what I am missing here. Thoughts?

2

2 Answers

1
votes

Unless it's a typo the filename Users.php is the most obvious error.

1
votes

The problem was I was using $this->User->SubscribedUser->saveAll() and not $this->User->saveAll()

My problem is now solved with this resolution.