I have 2 tables (2 models)
User
-uid
-email
-password
-(other fields)
Profile
-uid
-name
-age
-phone
-(other fields)
They have 1-1 relationship and I implemented the relationship as following:
class User extends Model
{
public function initialize()
{
$this->hasOne('uid', 'Profile', 'uid');
}
}
class Profile extends Model
{
public function initialize()
{
$this->hasOne('uid', 'User', 'uid');
}
}
This implementation is right? Can I replace hasOne by belongsTo? Thank you for help! :-)
hasOne is defined in the parent model while belongsTo is defined in the child model.
A User hasOne Profile and that Profile belongsTo one User.
The correct relationship definitions for your case would be:
class User extends Model
{
public function initialize()
{
$this->hasOne('uid', 'Profile', 'uid');
}
}
class Profile extends Model
{
public function initialize()
{
$this->belongsTo('uid', 'User', 'uid');
}
}