0

我从表'disability_types'中获取所有类型(id、name)。在此之后,我需要在结果数组前面加上 id=-1 的新记录'all'。然后这些记录用于填充多项选择。我愿意:

$disabilityType = $this->getDoctrine()->getRepository("AldenBonBundle:DisabilityType")->findAll();

我试过了:

$item = new \Alden\BonBundle\Entity\DisabilityType();
$item->setAsAll();//set name and id
array_unshift($disabilityType, $item);

但这给了我错误Entities passed to the choice field must be managed。经过一番研究,我尝试了

$disabilityType = $this->getDoctrine()->getEntityManager()->merge($item);

但这会产生错误Entity was not found

我应该如何将新的 $item 合并到 disableType ?

4

1 回答 1

0

您需要使用阵列水合,从而对阵列进行操作。

$query = $entityManager
             ->createQuery('SELECT d FROM AldenBonBundle\DisabilityType d');
$disabilityTypes = $query->getResult(Query::HYDRATE_ARRAY);

现在$disabilityTypes变量是一个数组,你可以array_unshift等等。

于 2012-10-04T00:05:32.330 回答