Yii Faker Not Working

I installed yii faker

"yiisoft/yii2-faker": "*"

advanced template

console/config/main.php:


<?php

$params = array_merge(

    require(__DIR__ . '/../../common/config/params.php'),

    require(__DIR__ . '/../../common/config/params-local.php'),

    require(__DIR__ . '/params.php'),

    require(__DIR__ . '/params-local.php')

);


Yii::setAlias('tests', __DIR__ . '/../tests');


return [

    'id' => 'app-console',

    'basePath' => dirname(__DIR__),

    'bootstrap' => ['log'],

    'controllerNamespace' => 'console\controllers',

    'modules' => [],

    'components' => [

        'log' => [

            'targets' => [

                [

                    'class' => 'yii\log\FileTarget',

                    'levels' => ['error', 'warning'],

                ],

            ],

        ],

    ],

    'params' => $params,

    'controllerMap' => [

        'fixture' => [

            'class' => 'yii\faker\FixtureController',

        ],

    ],

];



console/tests/unit/templates/fixtures/pages.php


<?php


return [

    [

        'title' => 'word',

        'content' => function ($fixture, $faker, $index) {

            //set needed fixture fields based on different conditions

            $fixture['content'] = $faker->sentence(7,true); //generate sentence exact with 7 words.

            return $fixture;

        }

    ],

];

run in console:


yii fixture/generate pages

I get this error:

How can I copy errors from the console?

Also, how can I add the generated data to my pages table?

Not sure about your exception, i cant reproduce it with latest yii2-advanced and yii2-faker. Things i done:

  1. put under @common in templates/fixtures/users.php your code about ‘content’

  2. ran command php yii fixture/generate users

and i got this output:




return [

	[

		'username' => 'sabryna.glover',

		'auth_key' => 'DzkZWZOzny0-ahK3xqcA0KK4ZFJ4iiEn',

		'password_hash' => '$2y$13$eruhwLdBpLWrhR6tqPOEKed1L28KvJByp7DuhvMqs85rQT11fT2H.',

		'password_reset_token' => 'YlwwBiBeerMMKeLhozaJQQYSu_7QYkF-_1404310181',

		'created_at' => '1404310181',

		'updated_at' => '1404310181',

		'email' => 'daugherty.dianna@gmail.com',

		'content' => 'Ea nesciunt eum quam aliquam.',

	],

	[

		'username' => 'hills.dannie',

		'auth_key' => '7ygPMTGQuctDmfMpZQcWdDSjn1pkgndh',

		'password_hash' => '$2y$13$HM50O3LWIjSw6LuHOO5pmO2Ab5GLHUuIS8d7cP40HeJT7tt.tOr5u',

		'password_reset_token' => 'wtLK4GB95Gz1zwa72ekNOGwn3I7K55Of_1404310181',

		'created_at' => '1404310181',

		'updated_at' => '1404310181',

		'email' => 'cartwright.jordane@schroeder.org',

		'content' => 'Doloremque quidem natus quam tempora autem molestiae hic ut amet.',

	],

];




As you can see it is correct.

in tests it is done automatically by using fixtures() method of test case. In console you can use load and unload actions of Fixture controller.

It’s working for me too, as long as I use the default users template, but when I try to create my own template, I’m getting this error

Even with the same content and just renaming the file

don’t you mean @console tests/unit/fixtures/templates ?

that’s where my files are

>don’t you mean @console tests/unit/fixtures/templates ?

nope, i was just showing my use-case, fixtures templates can be anywhere you need them. However as for me, common way is to put some shared fixtures templates under @common as it is done in applications templates.

>Even with the same content and just renaming the file

can you describe this in particular? If you are not using Yii standard templates it can be better if you upload your code somewhere, on Github or other, so we can check it. Mainly i think you just not configured faker as needed.

It’s all working now! thanks man

So this is what I did:

I’m using the advanced template

And I just followed this tutorial:

the only settings I have configured is this in console/config/main.php:


'controllerMap' => [

    'fixture' => [

        'class' => 'yii\faker\FixtureController',

    ],

],



The problem is this piece of code:


//users.php file under template path (by default @tests/unit/templates/fixtures)

return [

    [

        'table_column0' => 'faker_formatter',

        ...

        'table_columnN' => 'other_faker_formatter'

        'body' => function ($fixture, $faker, $index) {

            //set needed fixture fields based on different conditions


            $fixture['body'] = $faker->sentence(7,true); //generate sentence exact with 7 words.

            return $fixture;

        }

    ],

];

It should be like this, without the extra array:


//users.php file under template path (by default @tests/unit/templates/fixtures)

return [

    

        'table_column0' => 'faker_formatter',

        ...

        'table_columnN' => 'other_faker_formatter'

        'body' => function ($fixture, $faker, $index) {

            //set needed fixture fields based on different conditions


            $fixture['body'] = $faker->sentence(7,true); //generate sentence exact with 7 words.

            return $fixture;

        }

    

];

now this is my template:


<?php

return [

    'title' => 'firstName',

    'content' => function ($fixture, $faker, $index) {

        //set needed fixture fields based on different conditions

        $fixture['content'] = $faker->sentence(7,true); //generate sentence exact with 7 words.

        return $fixture;

    }

];

and this is my ActiveFixture in console/tests/unit/fixtures:


<?php


namespace tests\unit\fixtures;


use yii\test\ActiveFixture;


class PageFixture extends ActiveFixture

{

    public $modelClass = 'infoweb\pages\models\Page';

}

I run these commands:


yii fixture/generate pages 100


yii fixture/load Page

how can I add to my database table, now it’s just replacing all data?

>how can I add to my database table, now it’s just replacing all data?

currently working on --append option, see this issue for more info https://github.com/yiisoft/yii2/issues/2316