Yii ActiveRecord field only returns default value for recently added column

只愿长相守 提交于 2019-12-11 14:34:30

问题


Yii 1.1.12: When I select an instance of the User model, I get the default value for certain properties/columns. This happens for columns that I have recently added (manually) to the database:

$user->locale    // returns null (= default value)

If I set a new, different default value in the database (via phpMyAdmin), I will receive that new default value.

Other columns of that table do just fine:

$user->email     // returns the correct value

I will receive the correct value if I run an SQL query:

SELECT locale FROM tbl_users WHERE tbl_users.id = :userID LIMIT 1

From this I deduce that the database is just fine. The problem must be in the Active Records implementation.

I would expect that Yii's magic methods simply find each database column and add them as a (virtual) property to the model. This appears to be happening, but somehow the database-column default value is interjected.

What am I doing wrong?

Solution: As Sam suggested, removing defaultScope() did the trick. Apparently, a AR-property must be listed in defaultScope() to be recognized. Alternatively, defaultScope() can be removed in its entirety.


回答1:


Have you cleaned up Yii cache after adding deafault values to MySQL?



来源:https://stackoverflow.com/questions/13550296/yii-activerecord-field-only-returns-default-value-for-recently-added-column

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!