Fixture for related tables

Hi,

I’m working on the test part and I have a question regarding how to set up fixtures. Let’s say we have 2 tables : USER and PROFILE. Table USER has one foreign key called [color="#FF0000"]profile_id[/color].

How is it possible to create fixtures for a user and its profile ? … so when tests begin, a row is inserted in the USER table, with a profile_id refering to a row in the PROFILE table.

Maybe that’s a stupid question, but really I have no idea ;)

Thanks for your help.

B)

I am not sure this is the proper way… But I had a similar issue and this worked. I have adapted my code to use USERS and PROFILES like yours does.




<?php

$profiles = $this->getRows('profiles');


return array(

	'user1'=>array(

		'name' => 'aaaaa',

		'profile_id' => $profiles['profile1']['id'],

	),

	'user2'=>array(

		'name' => 'aaaaa',

		'profile_id' => $profiles['profile1']['id'],

	),

);



Here is my test and fixture code.

The Test:




<?php

class ProjectTest extends WebTestCase

{

	public $fixtures=array(

                'clients'=>'Client',

		'projects'=>'Project',

	);

	

	public function testIndex()

	{

		$this->open('project');

		$this->assertTextPresent('Login');

	}


}



And the fixture




<?php


$clients = $this->getRows('clients');


return array(

	'project1'=>array(

		'title' => 'Print Guide #1',

		'clientId' => $clients['client1']['id'],

	),

	'project2'=>array(

		'title' => 'Print Guide #1',

		'clientId' => $clients['client2']['id'],

	),

);



Could someone (an expert in Yii) let us know if this is the proper way to configure related data in fixtures?

Thanks

thanks for your reply jonupm

I don’t know if it’s the ‘proper way’ but in the meantime I’ll go with your solution … seems all right for me ;)

B)

Personally, i just went with ‘assumed’ ids. The fixture code in Yii will always reset the counter that gives you primary key ids, so the first row in the fixture will always be id 1, the second id 2, etc. So when i need to relate one fixture to another i just give it the id of whichever row it is in the other fixture

journey4712