Why this part of code never works in Yii? [closed]









up vote
-4
down vote

favorite












I have this code in my controller:



public function actionEdit()

$model = new Section();
$id = ($_GET['id']);
if ($model->load(Yii::$app->request->post()))

$record = Menu::findOne(['id' => $id]);
$record->name = $model->name;
$record->save();
return $this->redirect(['menu/index']);

if ($model = Menu::findOne(['id' => $id]))

return $this->render('edit', [
'model' => $model
]);




this part of code have never worked:



if ($model->load(Yii::$app->request->post()))

$record = Menu::findOne(['id' => $id]);
$record->name = $model->name;
$record->save();
return $this->redirect(['menu/index']);



if it needs, that's my view file:






<?php
/**
* Created by PhpStorm.
* User: Алексей
* Date: 14.01.2016
* Time: 19:00
*/
use yiihelpersHtml;
use yiiwidgetsActiveForm;
?>
<div class="menu-form">
<? echo $message?>
<? $form = ActiveForm::begin(); ?>

<div class="hidden">
<?

echo $form->field($model, 'id');
?>
</div>
<?= $form->field($model, 'name') ?>
<?= Html::submitButton('Изменить название', ['class' => 'btn btn-success']) ?>
<?php ActiveForm::end(); ?>
</div>












share|improve this question















closed as unclear what you're asking by Cœur, Mark Amery, rob006, Paul Crovella, Rob Nov 12 at 3:40


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • it means don't run ...
    – Алексей Ягодаров
    Jan 14 '16 at 20:23














up vote
-4
down vote

favorite












I have this code in my controller:



public function actionEdit()

$model = new Section();
$id = ($_GET['id']);
if ($model->load(Yii::$app->request->post()))

$record = Menu::findOne(['id' => $id]);
$record->name = $model->name;
$record->save();
return $this->redirect(['menu/index']);

if ($model = Menu::findOne(['id' => $id]))

return $this->render('edit', [
'model' => $model
]);




this part of code have never worked:



if ($model->load(Yii::$app->request->post()))

$record = Menu::findOne(['id' => $id]);
$record->name = $model->name;
$record->save();
return $this->redirect(['menu/index']);



if it needs, that's my view file:






<?php
/**
* Created by PhpStorm.
* User: Алексей
* Date: 14.01.2016
* Time: 19:00
*/
use yiihelpersHtml;
use yiiwidgetsActiveForm;
?>
<div class="menu-form">
<? echo $message?>
<? $form = ActiveForm::begin(); ?>

<div class="hidden">
<?

echo $form->field($model, 'id');
?>
</div>
<?= $form->field($model, 'name') ?>
<?= Html::submitButton('Изменить название', ['class' => 'btn btn-success']) ?>
<?php ActiveForm::end(); ?>
</div>












share|improve this question















closed as unclear what you're asking by Cœur, Mark Amery, rob006, Paul Crovella, Rob Nov 12 at 3:40


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • it means don't run ...
    – Алексей Ягодаров
    Jan 14 '16 at 20:23












up vote
-4
down vote

favorite









up vote
-4
down vote

favorite











I have this code in my controller:



public function actionEdit()

$model = new Section();
$id = ($_GET['id']);
if ($model->load(Yii::$app->request->post()))

$record = Menu::findOne(['id' => $id]);
$record->name = $model->name;
$record->save();
return $this->redirect(['menu/index']);

if ($model = Menu::findOne(['id' => $id]))

return $this->render('edit', [
'model' => $model
]);




this part of code have never worked:



if ($model->load(Yii::$app->request->post()))

$record = Menu::findOne(['id' => $id]);
$record->name = $model->name;
$record->save();
return $this->redirect(['menu/index']);



if it needs, that's my view file:






<?php
/**
* Created by PhpStorm.
* User: Алексей
* Date: 14.01.2016
* Time: 19:00
*/
use yiihelpersHtml;
use yiiwidgetsActiveForm;
?>
<div class="menu-form">
<? echo $message?>
<? $form = ActiveForm::begin(); ?>

<div class="hidden">
<?

echo $form->field($model, 'id');
?>
</div>
<?= $form->field($model, 'name') ?>
<?= Html::submitButton('Изменить название', ['class' => 'btn btn-success']) ?>
<?php ActiveForm::end(); ?>
</div>












share|improve this question















I have this code in my controller:



public function actionEdit()

$model = new Section();
$id = ($_GET['id']);
if ($model->load(Yii::$app->request->post()))

$record = Menu::findOne(['id' => $id]);
$record->name = $model->name;
$record->save();
return $this->redirect(['menu/index']);

if ($model = Menu::findOne(['id' => $id]))

return $this->render('edit', [
'model' => $model
]);




this part of code have never worked:



if ($model->load(Yii::$app->request->post()))

$record = Menu::findOne(['id' => $id]);
$record->name = $model->name;
$record->save();
return $this->redirect(['menu/index']);



if it needs, that's my view file:






<?php
/**
* Created by PhpStorm.
* User: Алексей
* Date: 14.01.2016
* Time: 19:00
*/
use yiihelpersHtml;
use yiiwidgetsActiveForm;
?>
<div class="menu-form">
<? echo $message?>
<? $form = ActiveForm::begin(); ?>

<div class="hidden">
<?

echo $form->field($model, 'id');
?>
</div>
<?= $form->field($model, 'name') ?>
<?= Html::submitButton('Изменить название', ['class' => 'btn btn-success']) ?>
<?php ActiveForm::end(); ?>
</div>








<?php
/**
* Created by PhpStorm.
* User: Алексей
* Date: 14.01.2016
* Time: 19:00
*/
use yiihelpersHtml;
use yiiwidgetsActiveForm;
?>
<div class="menu-form">
<? echo $message?>
<? $form = ActiveForm::begin(); ?>

<div class="hidden">
<?

echo $form->field($model, 'id');
?>
</div>
<?= $form->field($model, 'name') ?>
<?= Html::submitButton('Изменить название', ['class' => 'btn btn-success']) ?>
<?php ActiveForm::end(); ?>
</div>





<?php
/**
* Created by PhpStorm.
* User: Алексей
* Date: 14.01.2016
* Time: 19:00
*/
use yiihelpersHtml;
use yiiwidgetsActiveForm;
?>
<div class="menu-form">
<? echo $message?>
<? $form = ActiveForm::begin(); ?>

<div class="hidden">
<?

echo $form->field($model, 'id');
?>
</div>
<?= $form->field($model, 'name') ?>
<?= Html::submitButton('Изменить название', ['class' => 'btn btn-success']) ?>
<?php ActiveForm::end(); ?>
</div>






php yii2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 15:33









Cœur

17.2k9102141




17.2k9102141










asked Jan 14 '16 at 20:14









Алексей Ягодаров

159214




159214




closed as unclear what you're asking by Cœur, Mark Amery, rob006, Paul Crovella, Rob Nov 12 at 3:40


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






closed as unclear what you're asking by Cœur, Mark Amery, rob006, Paul Crovella, Rob Nov 12 at 3:40


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.













  • it means don't run ...
    – Алексей Ягодаров
    Jan 14 '16 at 20:23
















  • it means don't run ...
    – Алексей Ягодаров
    Jan 14 '16 at 20:23















it means don't run ...
– Алексей Ягодаров
Jan 14 '16 at 20:23




it means don't run ...
– Алексей Ягодаров
Jan 14 '16 at 20:23












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










The order of your code looks a little messy and that's probably what's causing the issue. By the way, you can always use the Gii's CRUD Generator and compare the structure of your actions.



About your code: The $id parameter is required for this action, since you can't edit something without the id. So you should call it with your function: public function actionEdit($id).



I notice you have 2 models being called in your action. Not sure why are you using this Section (it's never being saved), but i think you want to create a new Section everytime someone edit a Menu, populate the Menu with that information and getting rid of Section (again, so far there is no point of this Section model). For that, you will need to add a second parameter in your load method. Here is a example:



public function actionEdit($id)

$sectionModel = new Section();

if ($menuModel = Menu::findOne($id))
if ($sectionModel->load(Yii::$app->request->post(), 'Menu'))
$menuModel->name = $sectionModel->name;
$menuModel->save();

return $this->redirect(['menu/index']);


return $this->render('edit', [
'model' => $menuModel
]);




If that's not it, can you please explain better what are you trying to accomplish?






share|improve this answer



























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    The order of your code looks a little messy and that's probably what's causing the issue. By the way, you can always use the Gii's CRUD Generator and compare the structure of your actions.



    About your code: The $id parameter is required for this action, since you can't edit something without the id. So you should call it with your function: public function actionEdit($id).



    I notice you have 2 models being called in your action. Not sure why are you using this Section (it's never being saved), but i think you want to create a new Section everytime someone edit a Menu, populate the Menu with that information and getting rid of Section (again, so far there is no point of this Section model). For that, you will need to add a second parameter in your load method. Here is a example:



    public function actionEdit($id)

    $sectionModel = new Section();

    if ($menuModel = Menu::findOne($id))
    if ($sectionModel->load(Yii::$app->request->post(), 'Menu'))
    $menuModel->name = $sectionModel->name;
    $menuModel->save();

    return $this->redirect(['menu/index']);


    return $this->render('edit', [
    'model' => $menuModel
    ]);




    If that's not it, can you please explain better what are you trying to accomplish?






    share|improve this answer
























      up vote
      1
      down vote



      accepted










      The order of your code looks a little messy and that's probably what's causing the issue. By the way, you can always use the Gii's CRUD Generator and compare the structure of your actions.



      About your code: The $id parameter is required for this action, since you can't edit something without the id. So you should call it with your function: public function actionEdit($id).



      I notice you have 2 models being called in your action. Not sure why are you using this Section (it's never being saved), but i think you want to create a new Section everytime someone edit a Menu, populate the Menu with that information and getting rid of Section (again, so far there is no point of this Section model). For that, you will need to add a second parameter in your load method. Here is a example:



      public function actionEdit($id)

      $sectionModel = new Section();

      if ($menuModel = Menu::findOne($id))
      if ($sectionModel->load(Yii::$app->request->post(), 'Menu'))
      $menuModel->name = $sectionModel->name;
      $menuModel->save();

      return $this->redirect(['menu/index']);


      return $this->render('edit', [
      'model' => $menuModel
      ]);




      If that's not it, can you please explain better what are you trying to accomplish?






      share|improve this answer






















        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        The order of your code looks a little messy and that's probably what's causing the issue. By the way, you can always use the Gii's CRUD Generator and compare the structure of your actions.



        About your code: The $id parameter is required for this action, since you can't edit something without the id. So you should call it with your function: public function actionEdit($id).



        I notice you have 2 models being called in your action. Not sure why are you using this Section (it's never being saved), but i think you want to create a new Section everytime someone edit a Menu, populate the Menu with that information and getting rid of Section (again, so far there is no point of this Section model). For that, you will need to add a second parameter in your load method. Here is a example:



        public function actionEdit($id)

        $sectionModel = new Section();

        if ($menuModel = Menu::findOne($id))
        if ($sectionModel->load(Yii::$app->request->post(), 'Menu'))
        $menuModel->name = $sectionModel->name;
        $menuModel->save();

        return $this->redirect(['menu/index']);


        return $this->render('edit', [
        'model' => $menuModel
        ]);




        If that's not it, can you please explain better what are you trying to accomplish?






        share|improve this answer












        The order of your code looks a little messy and that's probably what's causing the issue. By the way, you can always use the Gii's CRUD Generator and compare the structure of your actions.



        About your code: The $id parameter is required for this action, since you can't edit something without the id. So you should call it with your function: public function actionEdit($id).



        I notice you have 2 models being called in your action. Not sure why are you using this Section (it's never being saved), but i think you want to create a new Section everytime someone edit a Menu, populate the Menu with that information and getting rid of Section (again, so far there is no point of this Section model). For that, you will need to add a second parameter in your load method. Here is a example:



        public function actionEdit($id)

        $sectionModel = new Section();

        if ($menuModel = Menu::findOne($id))
        if ($sectionModel->load(Yii::$app->request->post(), 'Menu'))
        $menuModel->name = $sectionModel->name;
        $menuModel->save();

        return $this->redirect(['menu/index']);


        return $this->render('edit', [
        'model' => $menuModel
        ]);




        If that's not it, can you please explain better what are you trying to accomplish?







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 15 '16 at 15:33









        Clyff

        2,90811025




        2,90811025













            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            ReactJS Fetched API data displays live - need Data displayed static

            政党