Use create of a controller in another controller

HI, my question is or seems a little confused!

So, I want to call the create action of another controller, how do it?

Controller A


public function actionCreate()

{

$model = new Post();

$model->save();

}

But, I want:


public function actionCreate()

{

$model = new Post();


// call another model

$category = new Category();

$category->id_post = $model->category_id_post;

// save record in another model

$category->save();


// save record this model

$model->save();

}

  • Read comments in code to understand ;)

Thanks

In general, you should not call another action method within an action method.

Action methods of controller have a dedicated purpose: they get a request and return a response.

If you don’t want to repeat some code, consider refactoring in the model layer.