0

对于以下数据库:

TABLES:
user: [id,username]
project: [id,name]
project_user_assignment: [user_id,project_id,role]

创建新项目时,我想显示一个下拉列表,其中包含可用用户来管理项目,并在保存时将其插入 project_user_assignment 行

[user_id,project_id, '经理' ]

我是 yii 的初学者,不知道在哪里(类、方法)我必须进行插入以及如果在插入时查询失败如何返回错误

4

1 回答 1

1

要在中显示下拉列表,您可以使用以下代码:

<?php echo CHtml::dropDownList(null, 
     'type',
     CHtml::listData(
        User::model()->findAll(),
        'id',
        'username',
     ),
     array('empty' => 'Select a manager from the list ... ', 'id'=>'manager_list')
  );?>

要更新 project_user_assignment 模型的相应字段,请使用以下内容:

 <?php
      <div class="row">
              <?php echo $form->labelEx($model,'user_id'); ?> // Here I assume you have the $model variable set to Project_user_assignemenet ... if not (which probably is the case since you are creating a new project, set a new $model2 variable in the controller and use $model2 instead of $model)
              <?php echo $form->textArea($model,'user_id'); ?>
              <?php echo $form->error($model,'user_id'); ?>
      </div>
 ?>
 <script>
     $('#manager_list').on('change', function(e) {
             $('#Project_user_assignemenet_user_id').val($(this).val());  // Here id maybe wrong! Check them.
             return true;
     });
</script>

最后在控制器中,您只需保存模型。

于 2012-05-08T18:38:01.567 回答