2013-05-03 22 views
0

当我调用CreateController时,出现此错误:“get_class()期望参数1为对象,数组给定”Yii为两个或更多模型收集数据(Get_Class()期望参数1为对象,数组给定)

控制研究/ actionCreate()如下:

public function actionCreate() { 
    $model = new Ogrenci; 
    $model2 =new Adresler; 
    $this->performAjaxValidation($model, 'ogrenci-form'); 
    $this->performAjaxValidation($model2, 'ogrenci-form'); 
    if (isset($_POST['Ogrenci'],$_POST['Adresler'])) { 
     $model->setAttributes($_POST['Ogrenci']); 
     $model2->setAttributes($_POST['Adresler']); 
     if ($model->save(false) && $model2->save(false)) { 
      if (Yii::app()->getRequest()->getIsAjaxRequest()) 
       Yii::app()->end(); 
     else 
      $this->redirect(array('view', 'id' => $model->ogrenci_id)); 
     } 
    } 
    $this->render('create', array('model' => $model,'model2' => $model2)); 
} 

create.php:

<?php $this->renderPartial('_form', array(
    'model' => array('model'=>$model,'model2'=>$model2), 
    'buttons' => 'create')); 
?> 

而且_form.php这个的领域如下:

01上述
<div class="row"> 
    <?php echo $form->labelEx($model2,'aciklama'); ?> 
    <?php echo $form->textField($model2,'aciklama'); ?> 
    <?php echo $form->error($model2,'aciklama'); ?> 
</div><!-- row --> 
+0

什么行号,你得到的错误? – acorncom 2013-05-03 18:49:30

+0

/framework/web/helpers/CHtml.php(2220)但它解决了。 谢谢4编辑teresko – Gnosis00 2013-05-04 12:19:05

回答

2
$this->renderPartial('_form', array(
    'model' => array(
     'model'=>$model, 
     'model2'=>$model2 
    ), 
    'buttons' => 'create' 
)); 

代码意味着文件_form.php将有两个变量的访问: $模型 - 两个元件的阵列,以及$按钮 - 串。

因此,要访问第一个模型,您必须编写$model['model'],第二个 - $model['model2']

但在这个代码

<?php echo $form->labelEx($model2,'aciklama'); ?> 
<?php echo $form->textField($model2,'aciklama'); ?> 
<?php echo $form->error($model2,'aciklama'); ?> 

您试图访问未定义的变量$model2。这应该会引起相应的错误。

是不是得到了错误的事情让我想在某个地方之前上市的代码可以访问变量$model在类似的方式,这样的事情:

echo $form->labelEx($model,'test'); 

在上面的代码$model是数组(因为你通过数组)。这就是为什么你收到错误的对象是预期的。

所以,你应该通过模型或以适当的方式访问它们。

我希望这会有所帮助。

+0

感谢您的协助:)它的工作原理 – Gnosis00 2013-05-03 21:00:00

0

我解决了另一个problem.Perhaps有人可能需要..

(CDbCommand未能执行SQL语句:SQLSTATE [23000]:完整性约束违规:1452不能添加或更新子行,外键)

if ($model->save(false) && $model2->save(false)) { 
     if (Yii::app()->getRequest()->getIsAjaxRequest()) 
       Yii::app()->end(); 
     else 
      $this->redirect(array('view', 'id' => $model->ogrenci_id)); 
} 

$a=$model2->save(false); 
    $model->adresler_id=$model2->adresler_id; 
    if ($a && $model->save(false)) { 
     if (Yii::app()->getRequest()->getIsAjaxRequest()) 
      Yii::app()->end(); 
     else 
      $this->redirect(array('view', 'id' => $model->ogrenci_id)); 
    } 
相关问题