0

私は教義の教義に従って始めました。これがドキュメントです。

私のコードは

$User = Doctrine_Core::getTable("User")->find(1);

$ User-> Phonenumbersでリレーションにアクセスすると、機能します。toArray()メソッドを使用してUserオブジェクトを配列に変換すると、リレーションが配列に変換されません。$Userデータを表示するだけです。

私は何かが足りないのですか?

4

2 に答える 2

1

findメソッドを使用すると、ユーザーデータのみが取得されます。そのため、toArrayの戻り値はそのデータに限定されます。ロードする追加データを指定する必要があります。これを行うのに最適な場所は、通常、元のクエリです。リンクした例から、選択部分を追加します。

$q = Doctrine_Query::create()
    ->select('u.*, e.*, p.*')  // Example only, select what you need, not *
    ->from('User u')
    ->leftJoin('u.Email e')
    ->leftJoin('u.Phonenumbers p')
    ->where('u.id = ?', 1);

次に、その結​​果をtoArrayすると、関連する電子メールと電話番号のデータも表示されます。

于 2010-03-25T01:51:34.953 に答える
0

I also noticed an anomaly with this where if you call the relationship first then call the ToArray, the relationship somehow gets included. what i mean is that, taking your own eg,

$User = Doctrine_Core::getTable("User")->find(1); 
$num= $User->Phonenumbers->office; // assumed a field 'office' in your phone num table
$userArray = $user->toArray(true);

In the above case, $userArray somehow contains the whole relationship. if we remove the $num assignment it doesn't.

am guessing this is due to doctrine only fetching the one record first, and it's only when you try to access foreign key values that it fetches the other related tables

于 2011-04-20T07:17:54.537 に答える