фикстуры и init.php

Общие вопросы по использованию фреймворка. Если не знаете как что-то сделать и это про Yii, вам сюда.
Ответить
PetrOFF
Сообщения: 52
Зарегистрирован: 2011.11.23, 13:07

фикстуры и init.php

Сообщение PetrOFF »

Стал пользоваться фикстурами и наткнулся на проблему.
У меня выстраивается не правильная очередь загрузки фикстур. А так как я использую foriegn key, то у меня появляются ошибки.
Я решил создать файл init.php и в нем переопредилить загрузку фикстур

Код: Выделить всё

<?php

$load_order = array(
    'shop.category',
    'shop.category_description',
    'shop.item',
    'shop.item_description',    
);

$reset_order = array_reverse($load_order);


foreach($this->getFixtures() as $tableName=>$fixturePath){
    if(!in_array($tableName, $reset_order)){
        throw new CException("Table '$tableName' is not in the reset_order.");
    }
    if(!in_array($tableName, $load_order)){
        throw new CException("Table '$tableName' is not in the load_order.");
    }
}

foreach($reset_order as $tableName){
    echo("resetting $tableName\n");
    // this runs the TABLE.init.php if it exists
    $this->truncateTable($tableName);
    //$this->resetTable($tableName);
}
foreach($load_order as $tableName){
    echo("loading $tableName\n");
    $this->loadFixture($tableName);
}


?>

В результате все фикстуры создались без ошибки, но возникла другая проблема, в файле тестов, перестали видеться файлы

Код: Выделить всё

<?php

class ItemTest extends CDbTestCase {

    public $fixtures = array(
//        'category' => 'shop.models.Category',
//        'category_description' => 'shop.models.CategoryDescription',
//        'item' => 'shop.models.Item',
//        'item_description' => 'shop.models.ItemDescription',
    );
    


    public function testCreate() {
        Yii::import('application.modules.shop.models.*');
        $item = new Item;
        $item->setAttributes(array(
            'category_id' => 3,
            'avaliable' => 'yes',
            'lottery_item' => 'no',
            'lottery_limit_sales' => 0,
            'lottery_count_sales' => 0,
            'visible' => 'yes',
            'del' => 'no',
            'total_count' => 10,
            'weigh' => 100,
            'title' => array('ru' => "Test title ru", 'en' => "Test title en", 'kz' => "Test title kz"),
            'description' => array('ru' => "Test description ru", 'en' => "Test description en", 'kz' => "Test description kz"),
            'attribute_item' => array(
                0 => array(
                    'ru'=>'Color red ru',
                    'en'=>'Color red en',
                    'kz'=>'Color red kz',
                    'qty' => 10
                ),
                1 => array(
                    'ru'=>'Color green ru',
                    'en'=>'Color green en',
                    'kz'=>'Color green kz',
                    'qty' => 20
                )
            )
                ),true);
        
        //check
        $this->assertTrue($item->validate(array("attribute")));
        $this->assertTrue($item->validate(array("title")));
        $this->assertTrue($item->validate(array("description")));
        
    

        
        $this->assertTrue($item->save(false));
        
        $item = Item::model()->findByPk($item->id);
        $this->assertTrue($item instanceof Item);
        
        $this->assertEquals("Test title ru", $item->itemDescriptionDefLang->title);
        $this->assertEquals("Test description ru", $item->itemDescriptionDefLang->description);
        $this->assertEquals("yes", $item->avaliable);
        
        
        $this->assertEquals("no", $item->lottery_item);
        $this->assertEquals(0, $item->lottery_limit_sales);
        $this->assertEquals(100, $item->weigh);
        
        
        
    }

}
 
Я добавил Yii::import('application.modules.shop.models.*');
это решили проблему с путями к моделями внутри тестового файла, но в нутри моделей другие классы опять не находятся.
Как можно обойти эту проблему?
PetrOFF
Сообщения: 52
Зарегистрирован: 2011.11.23, 13:07

Re: фикстуры и init.php

Сообщение PetrOFF »

Омг пол дня ушло, оказывается модель подгружается просто не было для нее результата, и иза того что модели подругому подгружаются то исключение стало по другому показыватся
Ответить