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?