1

ZendフレームワークでDoctrine 1.2を使用してデータベースクエリを実行しています。なぜ私の教義クエリは多次元配列を返す内部結合でですか?

$q = Doctrine_Query::create() 
    ->from('My_Model_MaterialsFromDb g') 
    ->innerJoin('t.My_Model_TeachingMaterials t'); 
    ->where('g.id= ?', $id) 
$result = $q->fetchArray(); 

基本的には、最初のテーブル(materialsFromDbが)私は授業に使用する教材のすべてのリストが含まれています。私はそうのように、内部結合使用してクエリ2つのテーブルを持っています。第2のもの(teachingMaterials)は、材料そのものの詳細を持っています。

私はこのクエリを実行すると、ここでの結果は常に次のようになります。

Array 
(
[0] => Array 
    (
     [id] => 1 
     [activityId] => 1 
     [materialId] => 2 
     [My_Model_Materials] => Array 
      (
       [id] => 2 
       [title] => My Groovy Material 
       [materialType] => Worksheet 
       [description] => This is my groovy material. It looks really cool. 
       [filename] => Groovy Material.doc 
       [uploaderId] => 1 
       [uploadDate] => 2012-02-16 
      ) 

    ) 
) 

は、私は、単一のアレイにこれを「フラット化」する教義のクエリを実行できる方法はありますか?両方のテーブルに "id"というプライマリキーがあるので、結果はこのようになりますか?この多次元配列を私の結果として持つことは、言い表すのは本当の苦痛です。

答えて

3

Doctrine 1.2ドキュメントによると、これは起こりそうなことです。

Doctrine hydration removes all duplicated data. It also performs many other things such as: 

Custom indexing of result set elements 
Value casting and preparation 
Value assignment listening 
Makes multi-dimensional array out of the two-dimensional result set array, the number of dimensions is equal to the number of nested joins 
Now consider the DQL equivalent of the SQL query we used: 

// test.php 

// ... 


$q = Doctrine_Query::create() 
     ->select('u.id, u.username, p.phonenumber') 
     ->from('User u') 
     ->leftJoin('u.Phonenumbers p'); 

    $results = $q->execute(array(), Doctrine_Core::HYDRATE_ARRAY); 

    print_r($results); 
The structure of this hydrated array would look like: 

$ php test.php 
Array 
(
    [0] => Array 
     (
      [id] => 1 
      [username] => 
      [Phonenumbers] => Array 
       (
        [0] => Array 
         (
          [id] => 1 
          [phonenumber] => 123 123 
         ) 

        [1] => Array 
         (
          [id] => 2 
          [phonenumber] => 456 123 
         ) 

        [2] => Array 
         (
          [id] => 3 
          [phonenumber] => 123 777 
         ) 

       ) 

     ) 
    // ... 
) 

試して...

$result = $q->execute(array(), Doctrine_Core::HYDRATE_NONE); 

か...

$result = $q->execute(array(), Doctrine_Core::HYDRATE_SCALAR); 

これは非常に有用

$user = array(
    'u_username' => 'jwage', 
    'u_password' => 'changeme', 
    // ... 
); 
+0

のようなものを返す必要があります。ありがとうございました! – blainarmstrong

関連する問題