Yii Framework Forum: Fixture for related tables - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Fixture for related tables Rate Topic: -----

#1 User is offline   Raoul 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 634
  • Joined: 29-November 08
  • Location:Paris, France

Posted 21 February 2010 - 12:10 PM

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 profile_id.
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)
0

#2 User is offline   jonupm 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 09-October 09

Posted 26 February 2010 - 12:13 PM

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'],
	),
);

0

#3 User is offline   jonupm 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 09-October 09

Posted 26 February 2010 - 12:20 PM

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
1

#4 User is offline   Raoul 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 634
  • Joined: 29-November 08
  • Location:Paris, France

Posted 26 February 2010 - 05:04 PM

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

#5 User is offline   journey4712 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 02-July 09

Posted 27 February 2010 - 01:24 PM

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
1

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users