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
(
)
)