Symfony2:在链配置的名称空间中找不到类"Doctrine \ Common \ Collections \ ArrayCollection" [英] Symfony2: class 'Doctrine\Common\Collections\ArrayCollection' was not found in the chain configured namespaces

查看:79
本文介绍了Symfony2:在链配置的名称空间中找不到类"Doctrine \ Common \ Collections \ ArrayCollection"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Error occurs when attempting to persist a one-to-many relationship. In particular, a client may have multiple household members; a given household member is associated with one and only one client. [I am new to Symfony so errors are expected!] Your guidance gratefully accepted.

下面的备用控制器代码段尝试通过将家庭表中的客户端ID设置为null来产生完整性约束错误.

N.B. Alternate controller snippet below yields integrity constraint error by trying to set the client's id in the household table to 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")
     */

ClientsType代码段

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

控制器代码段

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())));
    }
}

备用控制器代码段

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())));
    }

推荐答案

我已经将此工作了,但我没有

The times I have gotten this to work, I did not

use Doctrine\Common\Collections\ArrayCollection; 

我只是

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

在我的构造函数中.

此外,您可能需要考虑将住户"(似乎是客户中的成员)添加为实体形式:

Also, you may want to consider adding Household (which appear to be the members in clients) as an entity form:

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

这篇关于Symfony2:在链配置的名称空间中找不到类"Doctrine \ Common \ Collections \ ArrayCollection"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆