Yii Framework Forum: Inserting Data into Database - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Inserting Data into Database I would like to know how can I insert data from a form in the database

#1 User is offline   Yii Junior 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 13-March 17

Posted 04 June 2018 - 12:34 PM

Hello,

I have hard time getting my form to insert data into the database. I followed the guide from here:Active Record. When click the create button, the form seem to work but when I check my database there's no entry.

Here's my code:
MODEL
namespace it\alumregistry\models;

use yii\base\Model;
use yii\db\Query;
use yii\db\ActiveRecord;
use Yii;
use yii\helpers\ArrayHelper;

use yii\data\SqlDataProvider;
use yii\data\ArrayDataProvider;
use yii\db\ActiveQuery;
use yii\validators\EmailValidator;
use yii\web\app;

class MSE_Gapps_Pending extends ActiveRecord
{
    public $jenzId;
    public $email;

    public function rules()
    {
        return [
            [['email', 'jenzId'], 'safe'],
	    ['email', 'validateAtLeastOne', 'skipOnEmpty' => false],
        ];
    }

    public function attributeLabels()
    {
        return [
            'jenzId' => 'Your ID',
        ];
    }

    public function validateAtLeastOne($attribute, $params)
    {
        if (empty($this->email) || empty($this->jenzId)) {
            $this->addError('email', 'All search terms need to be provided.');
            $this->addError('jenzId');
        }
    }

	public static function MSE_Gapps_Pending() {

		return 'MSE_GAPPS_PENDING';
	}


Here's the Controller
public function actionCreate()
    {
        

$model = new MSE_Gapps_Pending();
		

$modelCanSave = false;
		
		
if ($model->load(Yii::$app->request->post()) && $model->validate()) {

	$model->ID_NUM = '775577';

	$model->Alt_Email ='emailtest@gmail.com';
	
	$model->insert();
		
}


Would someone please point what am I doing wrong here?

Thanks,
0

#2 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,642
  • Joined: 16-February 11
  • Location:Japan

Posted 05 June 2018 - 06:09 AM

Hi Yii Junior,

Did you write your MSE_Gapps_Pending model on your own without the help of Gii? It looks to have some flaw as an ActiveRecord model.

1) It doesn't have 'tableName' function.
2) You should not declare 'email' and 'jenzId' as public variables explicitly if they represent the columns in the table.

I think you should use Gii for the creation of the model and the CRUD for it. It's the easiest way to implement some basic things.
0

#3 User is offline   Yii Junior 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 13-March 17

Posted 13 June 2018 - 06:38 PM

View Postsoftark, on 05 June 2018 - 06:09 AM, said:

Hi Yii Junior,

Did you write your MSE_Gapps_Pending model on your own without the help of Gii? It looks to have some flaw as an ActiveRecord model.

1) It doesn't have 'tableName' function.
2) You should not declare 'email' and 'jenzId' as public variables explicitly if they represent the columns in the table.

I think you should use Gii for the creation of the model and the CRUD for it. It's the easiest way to implement some basic things.


No, I didn't not use Gii. The 'email' and 'jenzId' do not represent columns in the table but wanted to get the record from the form so that I can insert them into the table.
0

#4 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,642
  • Joined: 16-February 11
  • Location:Japan

Posted 14 June 2018 - 07:56 AM

View PostYii Junior, on 13 June 2018 - 06:38 PM, said:

No, I didn't not use Gii. The 'email' and 'jenzId' do not represent columns in the table but wanted to get the record from the form so that I can insert them into the table.


I see.

You have to create your MSE_Gapps_Pending model extending "Model" instead of "ActiveRecord" when you want it to be independent of any db table.

But a model extended from "Model" doesn't have any method to read from or write to db table. You need to do it using another model extended from "ActiveRecord" or using DAO.

Something like this:
public function actionCreate()
{
    $model = new MSE_Gapps_Pending();
    if ($model->load(Yii::$app->request->post()) && $model->validate()) {
        $model2 = new SomeModel();   // SomeModel is an ActiveRecord model
        $model2->ID_NUM = $model->jenzId;
        $model2->Alt_Email = $model->email;
        $model2->save();
        return $this->redirect(['index']);
    } else {
        return $this->render('login', [
            'model' => $model,
        ]);
    }
}

0

#5 User is offline   Yii Junior 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 13-March 17

Posted 18 June 2018 - 12:44 PM

View Postsoftark, on 14 June 2018 - 07:56 AM, said:

I see.

You have to create your MSE_Gapps_Pending model extending "Model" instead of "ActiveRecord" when you want it to be independent of any db table.

But a model extended from "Model" doesn't have any method to read from or write to db table. You need to do it using another model extended from "ActiveRecord" or using DAO.

Something like this:
public function actionCreate()
{
    $model = new MSE_Gapps_Pending();
    if ($model->load(Yii::$app->request->post()) && $model->validate()) {
        $model2 = new SomeModel();   // SomeModel is an ActiveRecord model
        $model2->ID_NUM = $model->jenzId;
        $model2->Alt_Email = $model->email;
        $model2->save();
        return $this->redirect(['index']);
    } else {
        return $this->render('login', [
            'model' => $model,
        ]);
    }
}



I ended up doing this below and it's working:
public function actionCreate()
    {
		$model = new MSE_Pending();
		
		$MSE_PENDING = false;
			
		if ($model->load(Yii::$app->request->post())) {
			if( $model->validate() ){
				Yii::$app->db->createCommand()->insert('MSE_PENDING', ['id_num' => $model->Id, 'Email' => $model->email,
				])->execute();
			}
		}

        return $this->render('create', ['model' => $model, 'MSE_PENDING' => $MSE_PENDING]);
    }

0

#6 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,642
  • Joined: 16-February 11
  • Location:Japan

Posted 18 June 2018 - 05:54 PM

Your code should work fine and there's nothing wrong in it, but it looks like a kind of riding a bicycle when you can drive a car.

Please consider using Gii. (https://www.yiiframe...e-views#layouts)
0

#7 User is offline   Yii Junior 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 13-March 17

Posted 20 June 2018 - 08:44 AM

View Postsoftark, on 18 June 2018 - 05:54 PM, said:

Your code should work fine and there's nothing wrong in it, but it looks like a kind of riding a bicycle when you can drive a car.

Please consider using Gii. (https://www.yiiframe...e-views#layouts)


I'm still learning this framework, so, I'm just taking baby steps.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users