Yii Framework Forum: How To Define Relation With Through - Yii Framework Forum

Jump to content

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

How To Define Relation With Through Rate Topic: -----

#1 User is offline   Daniel 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 214
  • Joined: 26-September 09

Posted 01 March 2013 - 05:16 PM

Hi all,

I have three tables,

product
-------
id int PK
code varchar
name varchar
unit varchar


category
-------
id int PK
code varchar
name varchar


product_category
---------------
id int PK
productFk FK to product(id)
categoryFk FK to category(id)


My relationship is
ProductCategory.php
'category' => array(self::BELONGS_TO, 'Category', 'categoryFk'),
'product' => array(self::BELONGS_TO, 'Product', 'productFk'),


Product.php
'productCategory' => array(self::HAS_MANY, 'ProductCategory', 'productFk'),


Category.php
'productCategory' => array(self::HAS_MANY, 'ProductCategory', 'categoryFk'),


Are they correct?

In addition,

1. how can I define the relation with through so that from product I can get its category?

2. how can I define the relation with through to get the first product sort by product name from the category?

Thanks in advance.

Daniel
0

#2 User is offline   C.S.Putera 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 92
  • Joined: 18-May 11

Posted 02 March 2013 - 01:24 AM

Hello. I'm just curious regarding the table structure, why would you use a blind integer key instead of using compound foreign key on an associative table (product_category) which does not have elements of its own ? :) I think it is best to use a many-to-many relations and therefore the relations should be :

In Product model :

...
'categories' => array(self::MANY_MANY, 'Category', 'tbl_product_to_category(product_id, category_id)'),
...
}


And in the Category model :

...
'products' => array(self::MANY_MANY, 'Product', 'tbl_product_to_category( category_id, product_id )'),
...

0

#3 User is offline   waitforit 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 382
  • Joined: 09-February 11

Posted 02 March 2013 - 01:30 AM

I agree with previous poster - use many:many if a junction table is used that has no other meta information.

That said, if you do need to go the way you are proposing I prefer to use nested 'with' statements in my AR instead of relations with through.

I haven't really figured out how to nest through relationships. Sometimes you end up with code that has 3-5 layers of relationships and nested 'with' statements just work.
0

#4 User is offline   Daniel 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 214
  • Joined: 26-September 09

Posted 03 March 2013 - 09:13 AM

Thank you for your replies.

The thing is, how could I get the category quickly from product, if I have many to many relationship.

from product I can only have categories.

The relationship is that one product can only have one category while a category can have one or more products.

Also, how can I get the first product (sorted alphabetically) from category.

Thanks,

Daniel
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