我正在尝试为具有一对多关系的实体实现CRUD功能。 为简单起见,我最初实现了一个简单的客户端成员CRUD。如果一个客户有许多客户负责的成员。
Symfony2在我的实现中引发了以下错误。
Warning: spl_object_hash() expects parameter 1 to be object, array given in /home/evolve/public_html/bootstrap/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php line 1509Warning: spl_object_hash() expects parameter 1 to be object, array given in /home/evolve/public_html/bootstrap/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php line 1509
代码: client.php
namespace CRUD\dbBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* CRUD\dbBundle\Entity\client
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="CRUD\dbBundle\Entity\clientRepository")
*/
class client
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string $name
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var string $surname
*
* @ORM\Column(name="surname", type="string", length=255)
*/
private $surname;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return client
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set surname
*
* @param string $surname
* @return client
*/
public function setSurname($surname)
{
$this->surname = $surname;
return $this;
}
/**
* Get surname
*
* @return string
*/
public function getSurname()
{
return $this->surname;
}
/**
* @var Doctrine\Common\Collections\ArrayCollection
*
* @ORM\OneToMany(targetEntity="member", mappedBy="client", cascade={"persist"})
*/
protected $member;
public function __construct()
{
$this->member = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Set the collection of related member
*
* @param \Doctrine\Common\Collections\ArrayCollection $member
*/
public function setMember(\Doctrine\Common\Collections\ArrayCollection $member)
{
foreach ($member as $members) {
$members->addTask($this);
}
$this->member = $member;
}
/**
* Add a member to the collection of related member
*
* @param \CRUD\dbBundle\Entity\member $member
*/
public function addMember(member $member)
{
$this->member->add($member);
$item->setClient($this);//inverse side association
}
/**
* Remove a member from the collection of related member
*
* @param \CRUD\dbBundle\Entity\member $member
*/
public function removeMember(OrderItem $member)
{
$this->member->removeElement($member);
}
/**
* Get the collection of related items
*
* @return \Doctrine\Common\Collections\ArrayCollection
*/
public function getMember()
{
return $this->member;
}
}
member.php
namespace CRUD\dbBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* CRUD\dbBundle\Entity\member
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="CRUD\dbBundle\Entity\memberRepository")
*/
class member
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string $name
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var string $surname
*
* @ORM\Column(name="surname", type="string", length=255)
*/
private $surname;
/**
* @var Order
*
* @ORM\ManyToOne(targetEntity="client", inversedBy="members")
*/
protected $client;
/**
* Set the related client
*
* @param client $client
*/
public function setClient(client $client)
{
$this->client = $client;
}
/**
* Get the related client
*
* @return client
*/
public function getClient()
{
return $this->client;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return member
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set surname
*
* @param string $surname
* @return member
*/
public function setSurname($surname)
{
$this->surname = $surname;
return $this;
}
/**
* Get surname
*
* @return string
*/
public function getSurname()
{
return $this->surname;
}
}
clientController.php
<?php
namespace CRUD\dbBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use CRUD\dbBundle\Entity\client;
use CRUD\dbBundle\Form\clientType;
/**
* client controller.
*
* @Route("/client")
*/
class clientController extends Controller
{
/**
* Lists all client entities.
*
* @Route("/", name="client")
* @Template()
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('CRUDdbBundle:client')->findAll();
return array(
'entities' => $entities,
);
}
/**
* Finds and displays a client entity.
*
* @Route("/{id}/show", name="client_show")
* @Template()
*/
public function showAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('CRUDdbBundle:client')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find client entity.');
}
$deleteForm = $this->createDeleteForm($id);
return array(
'entity' => $entity,
'delete_form' => $deleteForm->createView(),
);
}
/**
* Displays a form to create a new client entity.
*
* @Route("/new", name="client_new")
* @Template()
*/
public function newAction()
{
$entity = new client();
$form = $this->createForm(new clientType(), $entity);
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
/**
* Creates a new client entity.
*
* @Route("/create", name="client_create")
* @Method("post")
* @Template("CRUDdbBundle:client:new.html.twig")
*/
public function createAction()
{
$entity = new client();
$request = $this->getRequest();
$form = $this->createForm(new clientType(), $entity);
$form->bindRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('client_show', array('id' => $entity->getId())));
}
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
/**
* Displays a form to edit an existing client entity.
*
* @Route("/{id}/edit", name="client_edit")
* @Template()
*/
public function editAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('CRUDdbBundle:client')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find client entity.');
}
$editForm = $this->createForm(new clientType(), $entity);
$deleteForm = $this->createDeleteForm($id);
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
/**
* Edits an existing client entity.
*
* @Route("/{id}/update", name="client_update")
* @Method("post")
* @Template("CRUDdbBundle:client:edit.html.twig")
*/
public function updateAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('CRUDdbBundle:client')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find client entity.');
}
$editForm = $this->createForm(new clientType(), $entity);
$deleteForm = $this->createDeleteForm($id);
$request = $this->getRequest();
$editForm->bindRequest($request);
if ($editForm->isValid()) {
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('client_edit', array('id' => $id)));
}
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
/**
* Deletes a client entity.
*
* @Route("/{id}/delete", name="client_delete")
* @Method("post")
*/
public function deleteAction($id)
{
$form = $this->createDeleteForm($id);
$request = $this->getRequest();
$form->bindRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('CRUDdbBundle:client')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find client entity.');
}
$em->remove($entity);
$em->flush();
}
return $this->redirect($this->generateUrl('client'));
}
private function createDeleteForm($id)
{
return $this->createFormBuilder(array('id' => $id))
->add('id', 'hidden')
->getForm()
;
}
}
memberController.php
<?php
namespace CRUD\dbBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use CRUD\dbBundle\Entity\member;
use CRUD\dbBundle\Form\memberType;
/**
* member controller.
*
* @Route("/member")
*/
class memberController extends Controller
{
/**
* Lists all member entities.
*
* @Route("/", name="member")
* @Template()
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('CRUDdbBundle:member')->findAll();
return array(
'entities' => $entities,
);
}
/**
* Finds and displays a member entity.
*
* @Route("/{id}/show", name="member_show")
* @Template()
*/
public function showAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('CRUDdbBundle:member')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find member entity.');
}
$deleteForm = $this->createDeleteForm($id);
return array(
'entity' => $entity,
'delete_form' => $deleteForm->createView(),
);
}
/**
* Displays a form to create a new member entity.
*
* @Route("/new", name="member_new")
* @Template()
*/
public function newAction()
{
$entity = new member();
$form = $this->createForm(new memberType(), $entity);
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
/**
* Creates a new member entity.
*
* @Route("/create", name="member_create")
* @Method("post")
* @Template("CRUDdbBundle:member:new.html.twig")
*/
public function createAction()
{
$entity = new member();
$request = $this->getRequest();
$form = $this->createForm(new memberType(), $entity);
$form->bindRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('member_show', array('id' => $entity->getId())));
}
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
/**
* Displays a form to edit an existing member entity.
*
* @Route("/{id}/edit", name="member_edit")
* @Template()
*/
public function editAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('CRUDdbBundle:member')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find member entity.');
}
$editForm = $this->createForm(new memberType(), $entity);
$deleteForm = $this->createDeleteForm($id);
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
/**
* Edits an existing member entity.
*
* @Route("/{id}/update", name="member_update")
* @Method("post")
* @Template("CRUDdbBundle:member:edit.html.twig")
*/
public function updateAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('CRUDdbBundle:member')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find member entity.');
}
$editForm = $this->createForm(new memberType(), $entity);
$deleteForm = $this->createDeleteForm($id);
$request = $this->getRequest();
$editForm->bindRequest($request);
if ($editForm->isValid()) {
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('member_edit', array('id' => $id)));
}
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
/**
* Deletes a member entity.
*
* @Route("/{id}/delete", name="member_delete")
* @Method("post")
*/
public function deleteAction($id)
{
$form = $this->createDeleteForm($id);
$request = $this->getRequest();
$form->bindRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('CRUDdbBundle:member')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find member entity.');
}
$em->remove($entity);
$em->flush();
}
return $this->redirect($this->generateUrl('member'));
}
private function createDeleteForm($id)
{
return $this->createFormBuilder(array('id' => $id))
->add('id', 'hidden')
->getForm()
;
}
}
我明白我的clientController不完整,但不知道应该添加什么。我一直在努力工作,我很感激任何建议。
PS 这是我的第一篇文章,如果我在发布问题时应遵循的标准方面有任何改进,请告诉我。