Symfony 1.4,Doctrine(Doctrine :: HYDRATE_ARRAY non-multiple)

时间:2012-03-28 09:56:54

标签: symfony1 doctrine

我只是教授Symfony和Doctrine。 我不知道在Doctrine中获取1行数组而不是多行数组。 我用这个:

$q = $this->createQuery('a')
      ->innerJoin('a.Translation t')
      ->andWhere('t.lang = ?', $language)
      ->andWhere('t.name LIKE ?', 'somename%');

    return $q->execute(array(), Doctrine::HYDRATE_RECORD);

然后我得到类似的东西:

Array
(
    [0] => Array
        (
            [id] => 1
            [created_at] => 2012-03-19 17:40:52
            [updated_at] => 2012-03-21 17:44:04
            [created_by] => 1
            [updated_by] => 1
            [Translation] => Array
                (
                    [en] => Array
                        (
                            [id] => 1
                            [name] => somename
                            [lang] => en
                            [slug] => somename
                        )

                )

        )

)

但我需要

Array
(
    [0] => Array
        (
            [id] => 1
            [created_at] => 2012-03-19 17:40:52
            [updated_at] => 2012-03-21 17:44:04
            [created_by] => 1
            [updated_by] => 1
            [id] => 1
            [name] => somename
            [lang] => en
            [slug] => somename
        )

)

有些人知道我能做到吗?

3 个答案:

答案 0 :(得分:1)

我只使用

$q = $this->createQuery('a')
  ->select('a.id as id, t.name as name')
  ->leftJoin('a.Translation t')
  ->where('t.name LIKE ?', $name.'%')
  ->andWhere('t.lang = ?', $language);

答案 1 :(得分:0)

您是否尝试使用所需的列明确添加选择?

$q = $this->createQuery('a')
      ->select('a.id, a.created_at, a.updated_at, a.created_by, a.updated_by, t.id, t.name, t.lang, t.slug')
      ->innerJoin('a.Translation t')
      ->andWhere('t.lang = ?', $language)
      ->andWhere('t.name LIKE ?', 'somename%');

return $q->execute(array(), Doctrine::HYDRATE_ARRAY);

答案 2 :(得分:0)

使用WITH关键字?

$q = $this->createQuery('a')
        ->innerJoin('a.Translation t WITH t.lang = ?', $language)
        ->where('t.name LIKE ?', 'somename%');
return $q->execute(NULL, Doctrine::HYDRATE_ARRAY);