【问题标题】:Insert rows in other tables on creation in yii在 yii 中创建时在其他表中插入行
【发布时间】:2012-05-05 18:05:53
【问题描述】:

对于以下数据库:

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

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

[user_id,project_id,'经理']

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

【问题讨论】:

  • 请补充一点细节,比如你试过什么,否则问题太宽泛了
  • 请提供更多详细信息,我认为您想在保存项目时创建一个事务以将用户插入到 project_user_assignment 表中

标签: php yii


【解决方案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>

最后在控制器中保存模型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多