Yii Framework Forum: MANY_MANY question - Yii Framework Forum

Jump to content

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

MANY_MANY question Rate Topic: -----

#1 User is offline   imehesz 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 704
  • Joined: 13-June 09
  • Location:Jacksonville, FL

Posted 05 June 2010 - 10:50 PM

Hello,

I have the following table setup

++++++++++++++                     +++++++++++++
|  TABLE A   |                     +  TABLE B  +
++++++++++++++                     +++++++++++++
| id         |---  +++++++++++  ---| id        |
| name       |  |  + A_AND_B +  |  | something |
++++++++++++++  |  +++++++++++  |  +++++++++++++
                -->| aID     |  |
                   | bID     |<--
                   | status  |
                   +++++++++++



I successfully set up the MANY_MANY relationship between these tables:

  // TABLE A relations
  return array(
    'tableb' => array( self::MANY_MANY, 'TABLE B', 'A_AND_B(aID,bID)' )
  )


and it works great and as I expected, but is there a way for me to get the `status` column from my table A_AND_B?

I also tried to add the `together` param, but didn't help...

thanks,
--iM
It’s done, the great act of creation.
The maker rests. The wheel’s in motion.
-- Imre Madách

check out Yii Theme Factory at http://yii.themefactory.net
0

#2 User is offline   zitter 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 140
  • Joined: 14-July 09

Posted 06 June 2010 - 01:20 AM

not elegant solution, but what if you create *another* relation between TableA and A_AND_B?
0

#3 User is offline   nasrul 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 98
  • Joined: 14-April 09
  • Location:Sorowako - Indonesia

Posted 06 June 2010 - 03:03 AM

agree with @zitter,

return array(
    'tableb' => array( self::MANY_MANY, 'TABLE B', 'A_AND_B(aID,bID)' ),
    'tableab' => array( self::HAS_MANY, 'A_AND_B', 'aID' ), //another relation between Table A and A_AND_B
  );


btw, thanks for the podcasts. I do like it. :lol:
0

#4 User is offline   mech7 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 220
  • Joined: 26-March 09

Posted 06 June 2010 - 07:50 AM

I had the same problem before.. maybe it be of some help :P I still haven't found an elegant solution yet :)

http://www.yiiframew...-the-link-table
http://www.yiiframew...__fromsearch__1
0

#5 User is offline   imehesz 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 704
  • Joined: 13-June 09
  • Location:Jacksonville, FL

Posted 06 June 2010 - 09:13 AM

well in my case, I added a condition, and it's enough right now (i just want to make sure that status is 1)

so it looks something like this:
return array(
    'tableb' => array( self::MANY_MANY, 'TABLE B', 'A_AND_B(aID,bID)', 'condition' => '`status`=1' )
  )


not pretty but does the trick

--iM
It’s done, the great act of creation.
The maker rests. The wheel’s in motion.
-- Imre Madách

check out Yii Theme Factory at http://yii.themefactory.net
0

#6 User is offline   thyseus 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 300
  • Joined: 18-April 09
  • Location:Leipzig, Germany

Posted 06 June 2010 - 12:27 PM

I always solve this problem by generating a tableA_has_tableB Model to operate with.
0

#7 User is offline   rikardo 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 50
  • Joined: 16-December 09
  • Location:Sweden

Posted 07 June 2010 - 08:59 AM

Why not use a model for the 'A_and_B' table???
0

#8 User is offline   imehesz 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 704
  • Joined: 13-June 09
  • Location:Jacksonville, FL

Posted 07 June 2010 - 12:23 PM

rikardo,

I don't wanna create a model every time I have a pivot table. (ok, it's a little more than that in this case)

btw, I used to do that :)

--iM
It’s done, the great act of creation.
The maker rests. The wheel’s in motion.
-- Imre Madách

check out Yii Theme Factory at http://yii.themefactory.net
0

#9 User is offline   james2010 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 48
  • Joined: 19-February 10

Posted 07 June 2010 - 02:26 PM

hi, ive got a table like this.


Table : products (products_id, name)

Table: categories(categories_id, name)

Table: products_categories(products_id, categories_id)

before i did this in my controller

  $criteria = new CDbCriteria;
		  $criteria->condition = "products_id = :value";
		  //$criteria->params = array(":value"=>"%$name%");
		  $criteria->params= array(":value" => "1");

$dataProvider=new CActiveDataProvider('products',



now i like to search for all products in category 1.

so i change this in my models

Products.php
	public function relations()
	{
		
		return array('categories'=>array(self::MANY_MANY, 'Categories', 'products_categories(categories_id,products_id)')

		);
	}



Categories.php

	public function relations()
	{
		
		return array('products'=>array(self::MANY_MANY, 'Products', 'products_categories(categories_id,products_id)')

		);
	}


how have i change my query to get all products in category 1.?
0

#10 User is offline   james2010 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 48
  • Joined: 19-February 10

Posted 08 June 2010 - 02:26 AM

anyone can help me ?
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