Pergunta

I get an error

Fatal error: Call to a member function isAttributeRequired() on a non-object in C:\xampp\htdocs\yii\framework\web\helpers\CHtml.php on line 1414.

I have two tables Party and customers, I want edit party view in one form with customers fields

in my controller

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;


        }


    }

In 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)); ?>

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

Please Help..

Foi útil?

Solução

You're not returning the Customer model in your loadCusModel method in your controller, when there is a row found with that id:

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

You're only returning it if $customer_form is null. So put the return outside the if-statement and it will work:

if($customer_form===null) {
       throw new CHttpException(404,'The requested page does not exist.');
} 
return $customer_form;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top