vojkodrev
(Vojkodrev)
1
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?
vojkodrev
(Vojkodrev)
2
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