I work with Yii 1.1. I wish to make a create form, which creates a module. A module belongs to one user . A user has many modules .
tbl_user
----------
id
firstName
lastName
email
password
role
and
tbl_user_module
----------
id
name
academicYear
....
userId [FOREIGN KEY]
The relations in models are set up as follow:
Module.php
public function relations()
{
return array(
'user' => array(self::BELONGS_TO, 'User', 'userId'),
);
}
User.php
public function relations()
{
return array(
'module' => array(self::HAS_MANY, 'Module', 'userId'),
);
}
A user uses a from to create a module, which belongs to him (logged in user). In the controller, I need to assign the userId foreign key as the logged in user.
public function actionCreate()
{
$module = new Module();
// collect user input data
if(isset($_POST['Module']))
{
$module->attributes=$_POST['Module'];
$module->userId = Yii::app()->user->id; //assigned userID as logged in user
if($module->validate())
{
if($module->save())
{
$this->redirect(array('home/index'));
}
}
}
//render
$this->render('create',array('model'=>$module));
}
The form is not saved and throws an error:
Property "Module.$userId" is not defined.
Obviously, I am not doing it right. How do I properly save a one-to-many relations in a form?
解决了,我在Module.php中打错了!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.