问题
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.
N.B. Alternate controller snippet below yields integrity constraint error by trying to set the client's id in the household table to null.
Clients Entity snippet
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")
*/
Household Entity snippet
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 snippet
->add('members', 'collection', array(
'type' => new HouseholdType(),
'allow_add' => true,
'by_reference' => false,
'cascade_validation' => true,
));
Controller snippet
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())));
}
}
Alternate controller snippet
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())));
}
回答1:
The times I have gotten this to work, I did not
use Doctrine\Common\Collections\ArrayCollection;
I just
$this->field = new \Doctrine\Common\Collections\ArrayCollection();
in my constructor.
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,
));
回答2:
In my case I (incorrectly) tried to access the ToMany relation as it was ToOne relation (similar to this answer).
来源:https://stackoverflow.com/questions/13753772/symfony2-class-doctrine-common-collections-arraycollection-was-not-found-in-t