views:

38

answers:

1

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.

A: 

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'];
}
DrColossos
Thanks very much - works like a charm.
Justin
Actually, I'm running into another problem. This hydrates the results into arrays, but I'd like the Group objects. When I try to use execute() or fetchOne() instead of fetchArray(), I get a memory overflow error. Any idea? Thanks.
Justin
Nevermind, I figured it out. Doctrine acts very strangely when you write functions named getField() inside of models. Changed the name of the function and things are working fine now.
Justin