I am giving Yii a go, and started playing around with relations.
As good as the Guide is, it does not seem to answer the following questions (i might be wrong)
-
Do relations need to be defined on both sides of a relation?
-
Is relation Active record supposed to perform saves of the relations when you do a $model->save();
for example, using the data model from page 112 (minus tbl_ prefix) of the Yii guide, if the PostController is modified:
public function actionCreate()
{
$model=new Post;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Post']))
{
$model->attributes=$_POST['Post'];
$model->create_time = new CDbExpression('NOW()');
$model->author_id = '666'; // just for testing purposes
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
));
also the _form belonging to Post views is modified, removing the create time and author id and adding :
<div class="row">
<?php echo $form->dropDownList($model,'category', array('1' => 'One', '2' => 'Two', '3' => 'Three' )); ?>
</div>
and the Post model modified to have the relation:
return array(
'author'=>array(self::BELONGS_TO, 'User', 'author_id'),
'category'=>array(self::MANY_MANY, 'Category',
'post_category(post_id, category_id)'),
);
When saving the post model, is it supposed to also create a row in post_category
populating post_id and category_id automagically? is the code complete enough for it to do that?