Tutorial :Accessing Many-to-Many Field in Doctrine



Question:

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: deck_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.


Solution:1

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'];  }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »