对于以下数据库:
TABLES:
user: [id,username]
project: [id,name]
project_user_assignment: [user_id,project_id,role]
当创建一个新项目时,我想显示一个下拉列表,其中包含可用用户来管理项目,并在保存时将其插入到project_user_assignment行中
[user_id,project_id,'经理']
我是yii的首发,并且不知道我必须在哪里(类,方法)执行插入以及如果在插入查询失败的时刻如何返回错误
答案 0 :(得分: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>
最后在控制器中保存模型。