yii在非对象上调用成员函数isAttributeRequired()

时间:2013-09-15 17:02:27

标签: php yii updatemodel

我收到错误

  

致命错误:在a上调用成员函数isAttributeRequired()   C:\ xampp \ htdocs \ yii \ framework \ web \ helpers \ CHtml.php中的非对象   第1414行。

我有两个表派对和客户,我希望在一个表单中使用客户字段编辑派对视图

在我的控制器中

public function actionUpdate($id)
    {
        //$party_form = new Party;
        //$customer_form = new Customer;

        $model=$this->loadModel($id);
        $customer_form=$this->loadCusModel($id);

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if(isset($_POST['Party'], $_POST['Customer']))
        {
            $model->attributes=$_POST['Party'];
            $customer_form->attributes=$_POST['Customer'];

            if($model->save())
                $this->redirect(array('view','id'=>$model->id));
        }

        $this->render('update',array(
            'model'=>$model,
            'customer_form'=>$customer_form,
        ));
    }
........
public function loadModel($id)
    {   
        $model=Party::model()->with()->findByPk((int)$id);
        //$model=Party::model()->with(array('customer'=> array('select'=>'first_name, mobile, phone, type')))->findByPk($id);
        if($model===null)
            throw new CHttpException(404,'The requested page does not exist.');
        return $model;
    }


    public function loadCusModel($id)
    {   

        $customer_form=Customer::model()->with()->findByPk($id);

        /*
        $cmd = Yii::app()->db->createCommand();
        $cmd->select = 'party.account_title';
        $cmd->from = 'customer, party';
        $cmd->where = 'customer.id=party.customer_id';
        $cmd->andWhere("party.id=$id");
        $row = $cmd->queryRow();

        $customer_form=$row;
        if($customer_form===null)
            throw new CHttpException(404,'The requested page does not exist.');
        return $party_form;

        */
        if($customer_form===null) {
            throw new CHttpException(404,'The requested page does not exist.');
        return $customer_form;


        }


    }

在update.php中

<?php
/* @var $this PartyController */
/* @var $model Party */

$this->breadcrumbs=array(
    'Parties'=>array('index'),
    $model->id=>array('view','id'=>$model->id),
    'Update',
);

$this->menu=array(
    array('label'=>'List Party', 'url'=>array('index')),
    array('label'=>'Create Party', 'url'=>array('create')),
    array('label'=>'View Party', 'url'=>array('view', 'id'=>$model->id)),
    array('label'=>'Manage Party', 'url'=>array('admin')),
);
?>

<h1>Update Party <?php echo $model->id; ?></h1>

<?php $this->renderPartial('_formUpd', array('model'=>$model, 'customer_form'=>$customer_form)); ?>

和_formUpd.php

<?php
/* @var $this PartyController */
/* @var $model Party */
/* @var $form CActiveForm */
?>

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'party-formUpd',
    // Please note: When you enable ajax validation, make sure the corresponding
    // controller action is handling ajax validation correctly.
    // There is a call to performAjaxValidation() commented in generated controller code.
    // See class documentation of CActiveForm for details on this.
    'enableAjaxValidation'=>false,
    'htmlOptions' => array(
            'autocomplete' => 'off',


    )
)); ?>

    <p class="note">Fields with <span class="required">*</span> are required.</p>

    <?php echo $form->errorSummary($model); ?>
    <?php //echo $form->errorSummary($customer_form); ?>

    <div class="row">
        <?php echo $form->labelEx($customer_form,'first_name'); ?>
        <?php echo $form->textField($customer_form,'first_name',array('size'=>30,'maxlength'=>30, 'style' => 'text-transform:uppercase')); ?>
        <?php echo $form->error($customer_form,'first_name'); ?>
    </div>

    <div class="row">
        <?php //echo $form->labelEx($model,'last_name'); ?>
        <?php //echo $form->textField($model,'last_name',array('size'=>30,'maxlength'=>30, 'style' => 'text-transform:uppercase')); ?>
        <?php //echo $form->error($model,'last_name'); ?>
    </div>

    ............    

    <div class="row buttons">
        <?php //echo CHtml::submitButton($party_form->isNewRecord ? 'Create' : 'Save'); ?>
        <?php
        $this->widget('bootstrap.widgets.TbButton', array('buttonType'=>'submit', 'type'=>'primary', 'label'=>'Submit', 'size'=>'large'));
?>
    </div>

<?php $this->endWidget(); ?>

</div><!-- form -->

请帮助..

1 个答案:

答案 0 :(得分:3)

如果找到包含该ID的行,则不会在控制器中的loadCusModel方法中返回Customer模型:

if($customer_form===null) {
       throw new CHttpException(404,'The requested page does not exist.');
       return $customer_form;
}

如果$ customer_form为null,则只返回它。所以把回报放在if语句之外,它会起作用:

if($customer_form===null) {
       throw new CHttpException(404,'The requested page does not exist.');
} 
return $customer_form;