Relationship in model created from mysql VIEW


 I have created a model from a mysql view as User_view. Model gives proper records for listing. I have also another model which is created from normal mysql table as User model. When I tried to give relationship in User model to User_view model, it gives me error. So can I create relationship to View model or not?


Post a bit of code.

Thanks Fabrizio for your reply. My code is as below

In User Model :

‘rel_UserView’ => array(self::BELONGS_TO, ‘UserView’, ‘usr_id’),

In View File :


Both have same usr_id, mapped with One-To-One.

Still it gives an error "Trying to get property of undefined Object"

Since ‘UserView’ is a view, you should explicit primaryKey:

In UserView model add this method:

public function getPrimaryKey($asArray = false)


     return 'usr_id';


I think that relation does not work because primary key is missing.

Added This method, still it gives same error that "Trying to get property of non-Object".

Also my relationship is one-to-one. so code is : ‘rel_UserView’ => array(self::HAS_ONE, ‘UserView’, ‘usr_id’),

Are you sure that


returns an object?

Yes, it returns an object. In other model i have used this type of relationship to fetch records and it is working fine. Just issue arise only when mysql View related model is used. Can not understand why such error happens?

Hello my friend I agree with Fabrizio, there’s something wrong with your code that is not in the Post. Check again with very slowly, cause the message is simple and clear. It does not return the object, so can not reach the property. If you’re in a case sensitive system check out for that too. Please post again, that I’ll be waiting to help you…