简体   繁体   中英

Yii calling controller function from another controller

In my web application the work flow demands that I should call one controller function from another function Should I do add extra code or some configuration to proceed ? Right now This is how I implemented .But When I click "save" button nothing is happening values are just getting empty from the form .

My code .I want to create an object of model "BookVegetable" inside "ProducerOfferController" . My code inside producerOffer controller

 public function actionCreate()
{
    //$book_vegetable=new BookVegetable;
    $model=new BookVegetable;
    if(isset($_POST['BookVegetable']))
    {
        $model->attributes=$_POST['BookVegetable'];
        $model->booked_by = Yii::app()->user->id;
        $model->save();
        if ($model->hasErrors() === false)
        {
            $this->redirect(Yii::app()->user->returnUrl);
        }
    }
    else
    {
        Yii::app()->user->setReturnUrl($_GET['returnUrl']);
    }
    $this->render('book',array('model'=>$model,));
}

My code for from view

    <div style='padding-left:50px'>
    <?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm',array('id'=>'non-ajax_form','enableAjaxValidation'=>false,)); ?>
    <p class="help-block">Fields with <span class="required">*</span> are required.</p>

    <?php echo $form->errorSummary($model); ?>
    <?php echo "<br>" ?>


    <?php echo CHtml::textField("booked_quantity",$model->booked_quantity); ?>

My scenario

public function actionBookvegetable($id){
    $BookVegetable=new BookVegetable;
    $model=$this->loadModel($id);
    if(isset($_POST['ProducerOffer'],$_POST['BookVegetable']))
    {

        $model->attributes=$_POST['ProducerOffer'];
        $BookVegetable->attributes=$_POST['BookVegetable'];
        $BookVegetable->booked_by=Yii::app()->user->id;
        $BookVegetable->producer_offer_id=$model->id;
        $model->save();
        $BookVegetable->save();

        if (($model->hasErrors() === false)||($BookVegetable->hasErrors()=== false))
        {
            $this->redirect(Yii::app()->user->returnUrl);
          }
    }
        else
        {
            Yii::app()->user->setReturnUrl($_GET['returnUrl']);
        }
        $this->render('book',array('model'=>$model,'BookVegetable'=>$BookVegetable));
        }






<div class="form-actions">
    <?php $this->widget('bootstrap.widgets.TbButton', array('buttonType'=>'submit', 'type'=>'primary', 'label'=>  'Save',)); ?> 

How should I resolve this ? Is it essential to add anything extra to use one controller action inside another controller The url before saving and its the same after I press save also

 http://localhost/xxx/producerOffer/bookvegetable/20?returnUrl=%xxx%2FproducerOffer%2Fmanage

According to your information provided.. when you go here,

http://xxx.yyy.zzz/xxxx/producerOffer/create

Actually it should show you the form of book and when you click the save button there and go to returnUrl .

$this->redirect(Yii::app()->user->returnUrl);

I suggest you to write the following as,

$model->save();
$BookVegetable->save();

if (($model->hasErrors() === false)||($BookVegetable->hasErrors()=== false))
{
    $this->redirect(Yii::app()->user->returnUrl);
  }

To

if($model->save() && $BookVegetable->save())
    $this->redirect('yourAction'); //if params needed, $this->redirect(array('yourAction', 'id' => $model->id));

When you go here, http://xxx.yyy.zzz/xxxx/producerOffer/bookvegetable

What ever the code you have written under ActionBookvegetable will trigger.

To make sure your values submitted properly please change this code

$model->save();
if ($model->hasErrors() === false)
{
    $this->redirect(Yii::app()->user->returnUrl);
}

To

if($model->save())
    $this->redirect('yourAction');
else
    print_r(getErrors());

This will print any errors thats preventing from saving the model. let me know after you try this.

One way to do this in Yii2

Background

In SiteController action index method get the records of all objects from another model called voyzes.

In SiteController

Include the other model ex. Voyzes

在此处输入图片说明

Now in the SiteController action index method, implement the code to access the model/SQL/NoSQL or anything and set it in a array and return it to the view. For ex.

在此处输入图片说明

Now in the index view you should have your data from another model.

In my work , I have a lot of logic around the "loadModel" routine in controllers to make sure the user logged in has access to the particular model. I found this to work from another controller when I need to access the model, without moving or re-copying the loadmodel routine:

$caseviewController = Yii::app()->createController('Caseview');
//use this method from caseview controller to securley load case view model
$caseview = $caseviewController[0]->loadModel($caseviewid); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM