Ricardo Garza V.
Ricardo Garza V.

Reputation: 909

yii - change input name format and update

Hi I am fairly new to yii, and when I was making an application for practice (a character generator for an RPG), and got a problem updating problems with a form. i need to update a table that has a composite PK. so far I can create on both tables using my form. but I cant load the updateCode. my code is like follows:

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>

and this is the exeption with the first stack item

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

IS this eaven posible. NOTE: I understand the error, but can't think of a solution.

Upvotes: 0

Views: 1264

Answers (1)

Boris Belenski
Boris Belenski

Reputation: 1412

activeTextField accept as first param CModel instance, where you passing collection(array). So right code should be:

<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>

Upvotes: 2

Related Questions