gpt4 book ai didi

Symfony2 : class 'Doctrine\Common\Collections\ArrayCollection' was not found in the chain configured namespaces

转载 作者:行者123 更新时间:2023-12-04 19:45:38 25 4
gpt4 key购买 nike

尝试保持一对多关系时发生错误。特别是,客户可能有多个家庭成员;给定的家庭成员与一个且只有一个客户相关联。 [我是 Symfony 的新手,所以会出现错误!] 非常感谢您的指导。

注意下面的备用 Controller 代码段通过尝试将 household 表中的客户端 ID 设置为 null 来产生完整性约束错误。

客户端实体片段

namespace Mana\AdminBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
* Mana\AdminBundle\Entity\Clients
*
* @ORM\Table(name="clients")
* @ORM\Entity
*/
class Clients
{
protected $members;

public function __construct()
{
$this->members = new ArrayCollection();
}

public function getMembers()
{
return $this->members;
}

public function setMembers(ArrayCollection $members)
{
$this->members = $members;
}

/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
* @ORM\OneToMany(targetEntity="Household", mappedBy="clients")
* @ORM\ManyToOne(targetEntity="EthDesc", inversedBy="clients")
* @ORM\JoinColumn(name="eid", referencedColumnName="id")
*/

家庭实体片段

namespace Mana\AdminBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* Mana\AdminBundle\Entity\Household
*
* @ORM\Table(name="household")
* @ORM\Entity(repositoryClass="Mana\AdminBundle\Entity\HouseholdRepository")
*/
class Household {

/**
* @var integer $hid
*
* @ORM\Column(name="hid", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
* @ORM\ManyToOne(targetEntity="Clients", inversedBy="members")
* @ORM\JoinColumn(name="cid", referencedColumnName="id")
* @ORM\ManyToOne(targetEntity="EthDesc", inversedBy="members")
* @ORM\JoinColumn(name="eid", referencedColumnName="id")
*/

客户类型片段

    ->add('members', 'collection', array(
'type' => new HouseholdType(),
'allow_add' => true,
'by_reference' => false,
'cascade_validation' => true,
));

Controller 片段

public function createAction(Request $request) {
$entity = new Clients();
$form = $this->createForm(new ClientsType(), $entity);
$form->bind($request);

if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->persist($entity->getMembers());
$em->flush();
return $this->redirect($this->generateUrl('clients_show', array('id' => $entity->getId())));
}
}

备用 Controller 代码段

public function createAction(Request $request) {
$entity = new Clients();
$form = $this->createForm(new ClientsType(), $entity);
$form->bind($request);

if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
foreach ($entity->getMembers() as $member)
{
$em->persist($member);
}
$em->flush();
return $this->redirect($this->generateUrl('clients_show', array('id' => $entity->getId())));
}

最佳答案

我已经让这个工作的时候,我没有

use Doctrine\Common\Collections\ArrayCollection; 

我只是

$this->field = new \Doctrine\Common\Collections\ArrayCollection();

在我的构造函数中。

此外,您可能需要考虑将 Household(似乎是 clients 中的成员)添加为实体表单:

->add('members', 'entity', array(
'type' => new HouseholdType(),
'allow_add' => true,
'by_reference' => false,
));

关于Symfony2 : class 'Doctrine\Common\Collections\ArrayCollection' was not found in the chain configured namespaces,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13753772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com