1

I am trying to insert data into MySQL table in yii2 but it gives me errors.

I have a textbox in the view. It is meant to get the data the user inputs in the textbox with name=Topic[topic]; and insert it into the database.

When I try it a different way like $topic->topic = 'good things'; it works well, but when I try to change it like $topic->topic = $topic_data['topic'];. I get this error:

Undefined index: topic`

This is the controller:

use Yii;
use common\models\LoginForm;
use common\models\Topic;
use frontend\models\ContactForm;
use frontend\models\PasswordResetRequestForm;
use frontend\models\ResetPasswordForm;
use frontend\models\SignupForm;;

use yii\base\InvalidParamException;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\web\BadRequestHttpException;
use yii\web\Controller;

    public function actionCompose()
    {
          $topic= new Topic(); 

       $topic_data = Yii::$app->request->post('Topic', []);

        $topic->creator = Yii::$app->user->identity->email;
          $topic->topic = $topic_data['topic'];

         $topic->save();
           return $this->render('compose');
    }  

And this is the view:

<?php

use yii\widgets\ListView;
use yii\data\ArrayDataProvider;
use app\models\MyProfile;
use app\models\LikeDiscussion;
use yii\widgets\ActiveForm;
use common\models\Topic;
use common\models\Comment;
use common\models\Users;
use common\models\Candidate;
use yii\widgets\Pjax;
use yii\helpers\Html;
use frontend\assets\AppAsset;

$this->title = 'My Yii Application';
?>
       <?php $form = ActiveForm::begin(); ?>

  <input type="name" class="form-control"  required="true" name="Topic[topic]" id="topic" placeholder="topic">

   <?= Html::submitButton('My Yii Application', ['class' => 'btn btn-primary', 'name' => 'login-button']) ?>   
    <?php ActiveForm::end(); ?>    

How do I fix this or is their a better way to do it?

1
  • Show us what var_dump($topic_data) shows you Commented Jun 17, 2016 at 12:03

1 Answer 1

2

Try using proper $model->load

   $topic= new Topic(); 
   $topic->load($_POST)
   $topic->creator = Yii::$app->user->identity->email;
   $topic->save();
   return $this->render('compose');
Sign up to request clarification or add additional context in comments.

5 Comments

it worked. how come it knows the right colon it the table to insert to . i mean the $topic->load($_POST)....
The function load is base on the model and using the info provided by active record the model know the how the fields are mapped to the column ... when you use model->load yii2 load in model fields all the column matching the model name and the column name ..like you can see in this doc yiiframework.com/doc-2.0/yii-base-model.html#load()-detail
got it i understand
what if I don't want to use $topic->load($_POST) ?
@MrFaisal then you must assign the each $topic attribute the related $_POST value ..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.