0

嗨,我是 yii 的新手,当我申请练习(RPG 的字符生成器)时,遇到了更新表单问题的问题。我需要更新具有复合 PK 的表。到目前为止,我可以使用我的表单在两个表上创建。但我无法加载更新代码。我的代码如下:

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.']';

这是否可能。注意:我理解错误,但想不出解决方案。

4

1 回答 1

2

activeTextField接受作为第一个参数CModel实例,您在其中传递集合(数组)。所以正确的代码应该是:

<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>
于 2012-08-18T11:25:56.257 回答