Yii Framework Forum: Associative Table Design for Yii Help - Yii Framework Forum

Jump to content

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

Associative Table Design for Yii Help How best to design DB for Yii Rate Topic: -----

#1 User is offline   Floorian 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 26-February 10

Posted 26 February 2010 - 10:43 AM

Can anyone provide guidance or an example of how to best design and use an associative array with Yii?

The example I am toying with is a post that can be assigned to multiple categories. I want to use a post_cat table to associate the post and categories and have defined foreign keys as shown in attachment.
Attached File  screenshot_001.png (12.44K)
Number of downloads: 38
Basically:
<post>
id
title

<post_cat>
id
post_id
cat_id

<category>
id
name

post_cat key is a unique id since Yii won't crud a composite key?

I created models and crud. Where I am stumped is the view/edit/save piece relating to the associative table.
> Displaying the list of categories when editing the post and selecting those that are associated with the post.
> Then when saving, how to write to both the post and post_cat tables?

Any guidance/example is much appreciated.
0

#2 User is offline   jayrulez 

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

Posted 26 February 2010 - 10:56 AM

View PostFloorian, on 26 February 2010 - 10:43 AM, said:

Can anyone provide guidance or an example of how to best design and use an associative array with Yii?

The example I am toying with is a post that can be assigned to multiple categories. I want to use a post_cat table to associate the post and categories and have defined foreign keys as shown in attachment.
Attachment screenshot_001.png
Basically:
<post>
id
title

<post_cat>
id
post_id
cat_id

<category>
id
name

post_cat key is a unique id since Yii won't crud a composite key?

I created models and crud. Where I am stumped is the view/edit/save piece relating to the associative table.
> Displaying the list of categories when editing the post and selecting those that are associated with the post.
> Then when saving, how to write to both the post and post_cat tables?

Any guidance/example is much appreciated.



http://www.yiiframew...de/database.arr check the ERD and related data
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   Floorian 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 26-February 10

Posted 27 March 2010 - 08:25 AM

View Postjayrulez, on 26 February 2010 - 10:56 AM, said:

http://www.yiiframew...de/database.arr check the ERD and related data


Using this information, I created the relationship and can display the attached categories when viewing a post. How do I save changes to these when the post is updated? I assume I will need to call the post_catController since these are in a separate table?
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