Yii2: Fixture don't run with depends

Created on 4 Jul 2017  路  4Comments  路  Source: yiisoft/yii2

What steps will reproduce the problem?

I just run fixtures normally, and some of them need depends, but the execution don't respect the order

namespace common\fixtures\formgenerator;

/**
 * Fixture to load `SolicitudeValue` records.
 */
class SolicitudeValueFixture extends \tecnocen\formgenerator\fixtures\SolicitudeValueFixture
{
    /**
     * @inheritdoc
     */
    public $dataFile = '@common/fixtures/data/formgenerator/SolicitudeValue.php';

    /**
     * @inheritdoc
     */
    public $depends = [
        'common\fixtures\formgenerator\FieldFixture',
        'common\fixtures\formgenerator\SolicitudeFixture',
        'common\fixtures\formgenerator\SectionFixture'
    ];
}

What is the expected result?

The expected result is Execute FieldFixture, SolicitudFixture and SectionFixture before SolicitudValueFixture, but it doesn't happen, so SolicitudValue fails because it has foreign keys with the other tables.

What do you get instead?

In console I get this

12. formgenerator/DataType
    13. formgenerator/SolicitudeValue
    14. formgenerator/SectionField
    15. formgenerator/FieldRule
    16. formgenerator/FieldRuleProperty
    17. formgenerator/Solicitude
    18. formgenerator/Section
    19. formgenerator/Field
    20. formgenerator/Form

Additional info

| Q | A
| ---------------- | ---
| Yii version | 2.0.12
| PHP version | 5.6.30-12
| Operating system | Xubuntu 16.04.1

ready for adoption bug

All 4 comments

My test (d81fa0a) for this issue shows that fixtures are loaded in the correct order:

  1. SecondIndependent
  2. FirstIndependent
  3. Dependent

Build was successful: https://travis-ci.org/yiisoft/yii2/builds/475090986

I used the following fixture for the test:

class DependentActiveFixture extends ActiveFixture
{
    public $modelClass = 'yiiunit\data\ar\Customer';

    public $depends = [
        'yiiunit\data\console\controllers\fixtures\FirstIndependentActiveFixture',
        'yiiunit\data\console\controllers\fixtures\SecondIndependentActiveFixture',
    ];
}

But if turn on output, you can see the following picture in the console during the tests:

Fixtures below will be loaded:

        ...
    2. DependentActive
        ...
    5. SecondIndependentActive
    6. FirstIndependentActive
    ...

So:

  1. Order of loading is correct.
  2. Order of outputted list is wrong.

Most likely the problem is in the data output in the method FixtureController::actionLoad():
https://github.com/yiisoft/yii2/blob/a140b2b46884003f7aea61b9ad1cf1b61f68c937/framework/console/controllers/FixtureController.php#L109-L167

Researched a bit. The order of fixtures in the console output depends on the order in which the fixture files were found: https://github.com/yiisoft/yii2/blob/a140b2b46884003f7aea61b9ad1cf1b61f68c937/framework/console/controllers/FixtureController.php#L396-L425

Well, of course this order does not correlate with the order in which fixtures are really loaded...

It think it could be adjusted to match real loading order.

Thanks for research @rugabarbo.

Was this page helpful?
0 / 5 - 0 ratings