worhund
(Janssensjannes)
March 24, 2011, 5:35pm
1
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?
Haensel
(Johannes)
March 24, 2011, 5:42pm
2
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.
worhund
(Janssensjannes)
March 24, 2011, 10:49pm
3
I tried, but I keep getting the same error all over again:
PHP Error
Invalid argument supplied for foreach()
C:\xampp\htdocs\yii\db\ar\CActiveFinder.php(424)
412
413 /**
414 * Performs lazy find with the specified base record.
415 * @param CActiveRecord $baseRecord the active record whose related object is to be fetched.
416 */
417 public function lazyFind($baseRecord)
418 {
419 if(is_string($this->_table->primaryKey))
420 $this->records[$baseRecord->{$this->_table->primaryKey}]=$baseRecord;
421 else
422 {
423 $pk=array();
424 foreach($this->_table->primaryKey as $name)
425 $pk[$name]=$baseRecord->$name;
426 $this->records[serialize($pk)]=$baseRecord;
427 }
428
429 foreach($this->stats as $stat)
430 $stat->query();
431
432 if(empty($this->children))
433 return;
434
435 $child=reset($this->children);
436 $query=new CJoinQuery($child);
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, …)
Haensel
(Johannes)
March 24, 2011, 11:45pm
4
Ok, just a wild guess: Did you define Primary Keys in your tables? If not than this could be the problem.
worhund
(Janssensjannes)
March 25, 2011, 12:58pm
5
You’re a genius!
I had a PK in user, but not in oauth.
Thanks!
yiqing95
(Yiqing 95)
October 21, 2011, 1:41pm
6
want to know your oauth table 's schema ! do you mind share it ?