开发者

Accessing Many-to-Many Field in Doctrine

开发者 https://www.devze.com 2023-01-09 19:06 出处:网络
This must be simple, but I can\'t seem to find the answer online nor figure it out through trial and error.

This must be simple, but I can't seem to find the answer online nor figure it out through trial and error.

I have a class Deck and a class Card which have a many to many relationship with each other. I'm fairly confident that I am adding Cards to Decks correctly (they are being written correctly into the DB, junction table and all), but when I try to access a Deck's cards by using:

$Deck->Cards

, it doesn't seem to be returning anything. The relevant schema (in YAML) is here:

    **from Card model**
    relations:
       Decks:
           class: Deck
           foreignAlias: Cards
           refClass: DeckCard
           local: card_id
           foreign: deck_id

    **from Deck model**
    relations:
       Cards:
          class: Card
          foreignAlias: Decks
          refClass: DeckCard
          local: dec开发者_JS百科k_id
          foreign: card_id

    DeckCard:
       columns:
         deck_id:
           type: integer
           primary: true
         card_id:
           type: integer
           primary: true

Thanks so much. I'm sure this is easy and I'm overlooking something simple.


How have you retrieved the $Deck? The YAML seems fine.

The docs give very good examples: Take this schema. When retrieving, you'll get a collection of objects. The provided sample query:

//User-Group from a m:n relation through UserGroup Model
$q = Doctrine_Query::create()
    ->from('User u')
    ->leftJoin('u.Groups g');

$users = $q->fetchArray();

foreach ($users[0]['Groups'] as $group) {
    echo $group['name'];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号