yii - 更改输入名称格式并更新

时间:2012-08-18 04:17:23

标签: php yii

您好我是yii的新手,当我正在进行练习应用程序(RPG的角色生成器)时,遇到了更新表单问题的问题。我需要更新具有复合PK的表。到目前为止,我可以使用我的表单在两个表上创建。但是我无法加载updateCode。我的代码如下:

public function actionUpdate($id) {
    $model = $this->loadModel($id);
    $magicDiffModel = array();
    $m = Difficulty::getDifficultyListByType(Difficulty::magicDifficulty);
    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);
    if(!isset($_POST['MagicDifficulty']) && $id != ''){
        foreach ($m as $diff => $v) {
            $magicDiffModel[$diff] = MagicDifficulty::model()->findAllByPk(array('magic_id' => $id,'difficulty'=>$diff));
        }
    }else{

    }
    if (isset($_POST['Magic'])) {
        $model->attributes = $_POST['Magic'];
        if ($model->save())
            $this->redirect(array('view', 'id' => $model->id));
    }

    $this->render('update', array(
        'model' => $model,
        'magicDiffModel' => $magicDiffModel,
        'm' => $m,
    ));
}

<tr>
    <td>Zeon</td>
    <?php foreach ($m as $id => $name) { ?>
    <td><?php echo CHtml::activeTextField($magicDiffModel, '['.$id.']zeon'); ?></td>
    <?php } ?>
</tr>
<tr>
    <td>Int B.</td>
    <?php foreach ($m as $id => $name) { ?>
    <td><?php echo CHtml::activeTextField($magicDiffModel, '['.$id.']base_int'); ?></td>
    <?php } ?>
</tr>
<tr>
    <td>Mantenimiento</td>
    <?php foreach ($m as $id => $name) { ?>
    <td><?php echo CHtml::activeTextField($magicDiffModel, '['.$id.']manteinance'); ?></td>
    <?php } ?>
</tr>
<tr>
    <td>Descripción</td>
    <?php foreach ($m as $id => $name) { ?>
    <td><?php echo CHtml::activeTextField($magicDiffModel, '['.$id.']description'); ?></td>
    <?php } ?>
</tr>

这是第一个堆栈项的执行

get_class() expects parameter 1 to be object, array given

/srv/http/animaGen/framework/web/helpers/CHtml.php(2117): get_class(array(1 => array(MagicDifficulty), 2 => array(MagicDifficulty), 3 => array(MagicDifficulty), 4 => array(MagicDifficulty)))              
return get_class($model).$sub.'['.$attribute.']';

这是否有效。 注意:我理解错误,但无法想到解决方案。

1 个答案:

答案 0 :(得分:2)

activeTextField接受第一个param CModel实例,您传递集合( array )。 所以正确的代码应该是:

<tr>
    <td>Zeon</td>
    <?php foreach ($m as $id => $name) { ?>
    <td><?php echo CHtml::activeTextField($magicDiffModel[$id], 'zeon'); ?></td>
    <?php } ?>
</tr>
<tr>
    <td>Int B.</td>
    <?php foreach ($m as $id => $name) { ?>
    <td><?php echo CHtml::activeTextField($magicDiffModel[$id], 'base_int'); ?></td>
    <?php } ?>
</tr>
<tr>
    <td>Mantenimiento</td>
    <?php foreach ($m as $id => $name) { ?>
    <td><?php echo CHtml::activeTextField($magicDiffModel[$id], 'manteinance'); ?></td>
    <?php } ?>
</tr>
<tr>
    <td>Descripción</td>
    <?php foreach ($m as $id => $name) { ?>
    <td><?php echo CHtml::activeTextField($magicDiffModel[$id], 'description'); ?></td>
    <?php } ?>
</tr>