Yii Framework Forum: Model Relationships - Yii Framework Forum

Jump to content

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

Model Relationships How to auto-build model relationships? Rate Topic: -----

#1 User is offline   intel352 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 196
  • Joined: 05-February 10
  • Location:Southport, NC

Posted 08 February 2010 - 11:31 AM

I haven't seen anyone really address the proper way in Yii to build relationships between models, automatically, when using yiic to create your model classes.

It seems that Yii should be able to automatically detect that a column named "user_id" in the "posts" table, would relate to the "users" table's "id" column, but in practice, the only time I've had yiic actually build in model relationships, is when there is a composite key in a table.
Are there no conventions in place for Yii to be able to auto-detect relations when building models?

Additionally, the composite key support in v1.1 is lacking for CRUD generation, which sucks, as it existed for v1.0?
Need live Yii support? - Join the #yii IRC channel on Freenode!
0

#2 User is offline   jayrulez 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 348
  • Joined: 29-July 09

Posted 08 February 2010 - 03:24 PM

View Postintel352, on 08 February 2010 - 11:31 AM, said:

I haven't seen anyone really address the proper way in Yii to build relationships between models, automatically, when using yiic to create your model classes.

It seems that Yii should be able to automatically detect that a column named "user_id" in the "posts" table, would relate to the "users" table's "id" column, but in practice, the only time I've had yiic actually build in model relationships, is when there is a composite key in a table.
Are there no conventions in place for Yii to be able to auto-detect relations when building models?

Additionally, the composite key support in v1.1 is lacking for CRUD generation, which sucks, as it existed for v1.0?



If you use innodb tables and add constraints, yiic will generate your models with the relationships accordingly. I think i've also seen where you can specify relationships in table comments for your mysiam tables and yiic will also generate your relationships
php:
foreach(array('cat', 'dog', 'cow') as $animal) echo $animal."\n";

python:
[(animal, print(animal)) for animal in ['cat', 'dog', 'cow']]

ruby:
['cat', 'dog', 'cow'].each {|animal| puts animal}


You say Tomato, I say Tomato.
0

#3 User is offline   intel352 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 196
  • Joined: 05-February 10
  • Location:Southport, NC

Posted 09 February 2010 - 10:50 AM

Thanks for the tip on InnoDB+foreign key constraints, that works perfectly :-)
Need live Yii support? - Join the #yii IRC channel on Freenode!
0

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