簡體   English   中英

在數據庫中檢索對象的createForm

[英]createForm with object retrieved in database

我有一個奇怪的問題,當我嘗試使用數據庫中檢索到的對象創建表單時會引發錯誤:

$user = $this->getUser();

try {

  $group = $this->getDoctrine()
    ->getRepository('MuzichCoreBundle:Group')
    ->findOneBySlug($slug)
    ->getSingleResult()
  ;

} catch (\Doctrine\ORM\NoResultException $e) {
    throw $this->createNotFoundException('Groupe introuvable.');
}

if ($group->getOwner()->getId() != $user->getId())
{
  throw $this->createNotFoundException('Vous n\'ête pas le créateur de ce groupe.');
}

$form = $this->createForm(
  new GroupForm(), 
  $group,
  array('tags' => $this->getTagsArray())
);

return array(
  'group' => $group,
  'form'  => $form->createView()        
);

=>

給定類型為“數組”的預期參數,“ Doctrine \\ ORM \\ PersistentCollection”

但是,如果這是一個新對象,則沒有問題:

$form = $this->createForm(
  new GroupForm(), 
  new Group(),
  array('tags' => $this->getTagsArray())
);

=>沒有錯誤

您現在有什么問題嗎?

編輯 :問題“解決”,請參閱下面的注釋。

您只需要getSingleResult即可:

$group = $this->getDoctrine()
    ->getRepository('MuzichCoreBundle:Group')
    ->findOneBySlug($slug)
;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM