Yii Framework Forum: cactiverecord many to many relation question - Yii Framework Forum

Jump to content

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

cactiverecord many to many relation question Rate Topic: -----

#1 User is offline   phpuser0129387 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 02-March 10

Posted 02 March 2010 - 05:48 AM

hello!

i have a 3 tables, world, monster, and possible_monster

possible_monster defines which world has which monsters it contains world_id, monster_id and quantity fields.

i have a many to many relation defined in World model

"possible_monsters" => array(self::MANY_MANY, "Monster", "possible_monster(world_id, monster_id)")


i can read fields from the monsters table with
World::model()->with("possible_monsters")->findByPk(x)->possible_monsters->name
without a problem.

But i can't find a way to read a field quantity from the possible_monster table?
Is that even possible and how?
0

#2 User is offline   phpuser0129387 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 02-March 10

Posted 02 March 2010 - 06:37 AM

ok i got one solution. Basically we simulate many to many relation. here's the code

world relations
return array(
    "possible_monsters" => array(self::HAS_MANY, "PossibleMonster", "world_id", "with" => "monster")		);


and in PossibleMonster we have

return array(
    "monster" => array(self::BELONGS_TO, "Monster", "monster_id")
);


you can access data with

$world = World::model()->with("possible_monsters")->findByPk(2);
echo $world->possible_monsters[0]->monster->name;
echo $world->possible_monsters[1]->monster->name;
echo $world->possible_monsters[0]->quantity;


mind the quantity field in the last line that wasn't available before
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