0

我是 yii 框架的新手,所以我可以使用一些帮助。假设我在我的数据库中有一个带有 POST 的表,其中 1 个字段是 TYPE。在另一个表中,我有很多这样的类型:

表类型:

id  name
1   Politic
2   Sport
3   Espiritual

表帖:

id  title
1   Politic in Barsovia
2   God exist!
3   Del Po Win in Rotterdam

表 Post_type

id  id_post id_type
1   1       1
2   2       3 
3   3       2

我在 TYPE 中有关系

'posttype' => array(self::HAS_MANY, 'Post_type', 'ID_TYPE'),

我在 POST 中有关系

'posttype' => array(self::HAS_MANY, 'Post_type', 'ID_POST'),

我想要这个例子

问题:

如何从表 TYPE 中制作复选框列表

如何使用 activeCheckBoxList 宽度 CAdvancedArBehavior

4

1 回答 1

1

You can use the CHtml::checkBoxList() (or CHtml::activeCheckBoxList() or its wrapper in CActiveForm). For example, in your controller, you can have this line to retrieve all your related types:

$types = CHtml::listData($model->posttype, 'id', 'name'); // prepare the data for check box list
// the rest of your controller code ...
$this->render('create', array(
    'model' => $model,
    'types' => $types,
));

In your view, you can use CActiveForm::checkBoxList() to generate the check boxes:

<?php echo $form->checkBoxList($model, 'type', $types); ?>

Also, I recommend you make your relationships more user-friendly by changing the names: You should have "posts" as the name of your relation in your Type model, and "types" as the name of your relation in the Post model.

于 2013-02-21T16:16:36.860 回答