tags:

views:

294

answers:

1

I have two models and a join table, User, Post and posts_users.

The posts_users table contains a few extra fields, one of which is is_active. I want to set all of a users posts to active providing the user is active themselves.

I have the following

$this->Post->PostsUser->updateAll(array('PostsUser.is_active' => 1), array('PostsUser.user_id' => $data['User']['id'], 'User.is_active' => 1));

Which causes a MySQL error

Warning (512): SQL Error: 1054: Unknown column 'User.is_active' in 'where clause' ...

Query: UPDATE posts_sites AS PostsUser SET PostsUser.is_active = 1 WHERE PostsUser.user_id = 1 AND User.is_active = 1

As you can see the User table isn't getting joined in the query. Is there away around this problem?

+1  A: 

Have you defined the PostsUser model explicitly, or are you just using the implicitly auto-generated model?

It seems the latter is the case, so that model will probably not have any associations. You could bind them on the fly before making the update call, or you could explicitly create the model file and define the associations.

I.e. the Post and User models are connected to each other, but the PostsUser model is just an empty helper model that dangles off of either of them. It does not by itself have any connections, so when making a call from PostsUser it has no defined associations.

  Post   <--->   User
    |             |
    v             v
PostsUser     PostsUser
deceze
It was an auto-generated model, I've created the model file with the appropriate associations and now it's working, Nice job!
DanCake