dounao4179 2014-12-23 09:20
浏览 124

未知方法 - 在活动记录模型属性Yii2上使用set / get方法时

Lets say I have a table 'account' with below structure

CREATE TABLE IF NOT EXISTS `account` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Id',
  `name` varchar(255) NOT NULL COMMENT 'Name',
  `index` int(11) NOT NULL COMMENT 'Index',
  `user_id` int(11) NOT NULL COMMENT 'UserID',
  PRIMARY KEY (`id`),
  KEY `user_id` (`user_id`)
)

I have created a model 'Account' for that using gii.

When I try create new account, I want user_id to be assigned from session

So, In the controller, I tried like below.

$model = new Account();
$model->**setUser_Id**(Yii::$app->user->getID());

if ($model->load(Yii::$app->request->post()) && $model->save())

But, I don't write any setUser_Id method in Account model, since I assumed when I call set methods, it will automatically assign the property of ActiveRecord.

But its throwing error. But it works fine when I try

$model->user_id = Yii::$app->user->getID();

As per documentation http://www.yiiframework.com/wiki/167/understanding-virtual-attributes-and-get-set-methods/, Note that the get/set functions can be called directly as functions (but requiring the usual function-call parentheses):

$x = $model->fullname;
$x = $model->getFullname();        // same thing

$model->active = 1;
$model->setActive(1);              // same thing

Can you experts throw some light on this please?

  • 写回答

3条回答 默认 最新

  • dsj1961061 2014-12-23 10:04
    关注

    [edit] read the question a bit better and did few clarifications to make the answer more comprehensive.

    The exception is actually expected.

    The way the whole virtual attributes functionality has been implemented in Yii shows that when calling $model->attribute the magic methods __get() and __set() search for the attribute name in the table columns, and if not, search for the getter or setter, respectively, that you might have implemented. (That's very approximate but should give you the idea, more information can be seen by looking at the code: https://github.com/yiisoft/yii2/blob/master/framework/db/BaseActiveRecord.php#L237)

    This means that the virtual attributes work only one way natively. Otherwise you can always access them via a direct method call using $model->getAttribute('my_attribute'); and $model->setAttribute('my_attribute', $value);

    The example you're taking from the link you posted is out of context (apart having been written for Yii 1): in there you get the getter/setter implemented manually and later called in both ways.

    The only case where names might not be coherent with what you have in the database is when the table name contains underscores, in which case they'll be removed: e.g. people_manager becomes PeopleManager as class name for the model.

    It's not anyway clear what you're trying to achieve, this might help us give you a better direction on how to achieve what you want.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab