Basic Question for ActiveRecord->load();

Hi, i got a problem with the load method of ActiveRecord.

After loading data into the model, it is still empty, and save generates an empty entry in the DB.

I started with "The Definitive Guide to Yii 2.0" getting started Forms and Database examples, both worked,

but this is basically the same and doesn’t? Does the load method for ActiveRecord work different then for the basic model?

The DB:




CREATE TABLE IF NOT EXISTS `yiidemo` (

  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,

  `first_name` varchar(256) COLLATE utf8mb4_unicode_ci NOT NULL,

  `last_name` varchar(256) COLLATE utf8mb4_unicode_ci NOT NULL,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci AUTO_INCREMENT=1 ;



The model:




<?php


namespace app\models;


use yii\db\ActiveRecord;


class Yiidemo extends ActiveRecord

{

        public static function tableName()

        {

            return 'yiidemo';

        }

}



The Controller:




<?php


namespace app\controllers;


use yii\web\Controller;

use app\models\Yiidemo;


class YiidemoController extends Controller

{


    public function actionDemo()

    {

        $model = new Yiidemo();


        $data['Yiidemo']['first_name'] = 'Demo';

        $data['Yiidemo']['last_name'] = 'Demo';

        $model->load($data);


        print_r( $model );


    }


}



load() return true, but the output shows that the attributes are not set.




app\models\Yiidemo Object

(

    [_attributes:yii\db\BaseActiveRecord:private] => Array

        (

        )


    [_oldAttributes:yii\db\BaseActiveRecord:private] => 

    [_related:yii\db\BaseActiveRecord:private] => Array

        (

        )


    [_errors:yii\base\Model:private] => 

    [_validators:yii\base\Model:private] => ArrayObject Object

        (

            [storage:ArrayObject:private] => Array

                (

                )


        )


    [_scenario:yii\base\Model:private] => default

    [_events:yii\base\Component:private] => Array

        (

        )


    [_behaviors:yii\base\Component:private] => Array

        (

        )


)



I replaced the model with an Gii generated model, load() works now.

Tried to remove some parts, it looks like the rules part is required.

This maybe should be mentioned in the database guide?

http://www.yiiframework.com/doc-2.0/guide-start-databases.html

But i guess best practise would be do use a Gii generated model anyway?

edit - just to make this complete - the Gii model:




<?php


namespace app\models;


use Yii;


/**

 * This is the model class for table "yiidemo".

 *

 * @property integer $id

 * @property string $first_name

 * @property string $last_name

 */

class Yiidemo extends \yii\db\ActiveRecord

{

    /**

     * @inheritdoc

     */

    public static function tableName()

    {

        return 'yiidemo';

    }


    /**

     * @inheritdoc

     */

    public function rules()

    {

        return [

            [['first_name', 'last_name'], 'required'],

            [['first_name', 'last_name'], 'string', 'max' => 256]

        ];

    }


    /**

     * @inheritdoc

     */

    public function attributeLabels()

    {

        return [

            'id' => 'ID',

            'first_name' => 'First Name',

            'last_name' => 'Last Name',

        ];

    }

}