Relational Active Record

I can’t get this figured out…

How can I select the User belonging to a given Oauth?

I got two tables:

  • A user table, with PK user_id

  • An oauth table, with a field user_id

I set up a foreign key in the oauth table, from the field oauth.user_id to user.user_id

How can I select a user corresponding with an oauthrecord?

I have the models Oauth and User generated by Gii, so I guess they’re okay?


 $with = array('oauths');

        $arr = array(':oauthID' => $data['oauth_id'], ':provider' => $data['provider']);

        $user = User::model()->with($with)->find('provider_id=:oauthID AND provider=:provider',$arr);

What am I doing wrong? I don’t see it…

The error I get is this one:

When I try to do it non-relational (first selecting the user_id from oauth, than selecting the user using this user_id), it works fine, but that’s not what I want. Altough, I guess it can be a lot easier?

Why don’t you just use the relations added by gii?

By default you should be able to do


//in user object  


$this->oauth  


//in oauth object 


$this->user



This way you are able to find the corresponding FK table entries of a given object.

I tried, but I keep getting the same error all over again:

Edit: The last try, which gave me also this error was with this code:


$oauth = Oauth::model()->find('provider_id=:oauthID AND provider=:provider',

                array(':oauthID' => $data['oauth_id'], ':provider' => $data['provider']));

        $user = $oauth->user;

The code of both models (User and Oauth) is generated by gii, and the relations are OK in the database (foreign key, InnoDB, …)

Ok, just a wild guess: Did you define Primary Keys in your tables? If not than this could be the problem.

You’re a genius!

I had a PK in user, but not in oauth.

Thanks!

want to know your oauth table 's schema :rolleyes: ! do you mind share it ?