当我配置源代码(MVC)时,我正在研究ZendFramework2 Doctrine ORM
:类,控制器。我的应用是关于管理"Commande"
和"Produit"
,问题是当我想添加commande
时我有这个错误:
在关联
上找到类型为Doctrine\Common\Collections\ArrayCollection
的实体
这是 Commande.php :
class Commande {
/**
* @ORM\id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
protected $id;
/** @ORM\Column(type="string") */
protected $duree_commande;
/** @ORM\Column(type="string") */
protected $date_commande;
/**
* @ORM\ManyToOne(targetEntity="Produit", inversedBy="commandes",cascade={"persist"})
* @ORM\JoinColumn(name="produit_id", referencedColumnName="id")
*/
protected $produits;
public function __construct()
{
$this->produits = new \Doctrine\Common\Collections\ArrayCollection();
}
public function getIdCommande()
{
return $this->id;
}
public function getDureeCommande()
{
return $this->duree_commande;
}
public function setIdCommande($id_commande)
{
$this->id = $id_commande;
}
public function setDureeCommande($duree_commande)
{
$this->duree_commande = $duree_commande;
}
public function getDateCommande()
{
return $this->date_commande;
}
public function setDateCommande($date_commande)
{
$this->date_commande = $date_commande;
}
public function getProduits()
{
return $this->produits;
}
public function addProduit($produit)
{
$this->produits->add($produit);
}
}
Produit.php :
class Produit {
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
protected $id;
/** @ORM\Column(type="string") */
protected $type_produit;
/** @ORM\OneToMany(targetEntity="Commande", mappedBy="produit", cascade={"persist"})
*/
protected $commande;
public function __construct()
{
$this->commandes = new \Doctrine\Common\Collections\ArrayCollection();
}
// getters/setters
public function getIdProduit()
{
return $this->id;
}
public function getTypeProduit()
{
return $this->type_produit;
}
public function setIdProduit($id_produit)
{
$this->id = $id_produit;
}
public function setTypeProduit($type_produit)
{
$this->type_produit = $type_produit;
}
function getCommande()
{
return $this->commande;
}
function setCommande(Commande $commande)
{
$commande->addProduit($this);
$this->commande = $commande;
}
}
我有指挥控制器:
class CommandeController extends AbstractActionController
{
protected $em;
public function getEntityManager()
{
if (null === $this->em) {
$this->em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
}
return $this->em;
}
public function indexAction()
{
$em = $this->getEntityManager();
$commandes = $em->getRepository('Commande\Entity\Commande')->findAll();
return new ViewModel( array(
'commandes' => $commandes,
));
}
public function addAction()
{
if ($this->request->isPost()) {
$commande = new Commande();
$commande->setDureeCommande($this->getRequest()->getPost('dureeCommande'));
$commande->setDateCommande($this->getRequest()->getPost('dateCommande'));
$this->getEntityManager()->persist($commande);
$this->getEntityManager()->flush();
$newId = $commande->getIdCommande();
return $this->redirect()->toRoute('commande');
}
return new ViewModel();
}
public function editAction()
{
/* $objectManager = $this->getServiceLocator()
->get('Doctrine\ORM\EntityManager');
$commande = $objectManager->find('Commande\Entity\Commande', 13);
$commande->setDureeCommandes('Guilherme Blanco');
$objectManager->flush();
die(var_dump($commande->getIdCommande()));*/
$id = (int) $this->params()->fromRoute('id', 0);
$commande = $this->getEntityManager()->find('\Commande\Entity\Commande', $id);
if ($this->request->isPost()) {
$commande->setDureeCommande($this->getRequest()->getPost('dureeCommande'));
$commande->setDateCommande($this->getRequest()->getPost('dateCommande'));
$this->getEntityManager()->persist($commande);
$this->getEntityManager()->flush();
return $this->redirect()->toRoute('home');
}
return new ViewModel(array('commande' => $commande));
}
public function deleteAction()
{
/* $objectManager = $this->getServiceLocator()
->get('Doctrine\ORM\EntityManager');
$commande = $objectManager->find('Commande\Entity\Commande', 14);
$objectManager->remove($commande);
$objectManager->flush();
die(var_dump($commande->getIdCommande()));*/
$id = (int) $this->params()->fromRoute('id', 0);
$commande = $this->getEntityManager()->find('\Commande\Entity\Commande', $id);
if ($this->request->isPost()) {
$Produits = $commande->getProduits();
if (count($Produits)>1)
{
foreach ($Produits as $produit) {
$this->getEntityManager()->remove($produit);
}
}
$this->getEntityManager()->remove($commande);
$this->getEntityManager()->flush();
return $this->redirect()->toRoute('home');
}
return new ViewModel(array('commande' => $commande));
}
public function getCommandeTable()
{
}
}
这适用于 Produit Controller :
class ProduitController extends AbstractActionController
{
protected $em;
public function getEntityManager()
{
if (null === $this->em) {
$this->em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
}
return $this->em;
}
public function indexAction()
{
$em = $this->getEntityManager();
$produits = $em->getRepository('Commande\Entity\Produit')->findAll();
return new ViewModel( array(
'produits' => $produits,
));
}
public function addAction()
{
/*$objectManager = $this
->getServiceLocator()
->get('Doctrine\ORM\EntityManager');
$commande = $objectManager->find('Commande\Entity\Commande',13);
$produit = new \Commande\Entity\Produit();
$produit->setTitleProduit('commande13');
$produit->setCommande($commande);
$objectManager->persist($produit);
$objectManager->flush();
var_dump($commande->getTitleCommande());*/
$id = (int) $this->params()->fromRoute('id', 0);
$commande = $this->getEntityManager()->find('\Commande\Entity\Commande', $id);
if ($this->request->isPost()) {
$produit = new Produit();
$produit->setTypeProduit($this->getRequest()->getPost('typeProduit'));
$produit->setCommande($commande);
$this->getEntityManager()->persist($produit);
$this->getEntityManager()->flush();
return $this->redirect()->toRoute('home');
}
return new ViewModel(array('commande' => $commande));
}
public function editAction()
{
$id = (int) $this->params()->fromRoute('id', 0);
$produit = $this->getEntityManager()->find('\Commande\Entity\Produit', $id);
if ($this->request->isPost()) {
$produit->setTypeProduit($this->getRequest()->getPost('typeProduit'));
$this->getEntityManager()->persist($produit);
$this->getEntityManager()->flush();
return $this->redirect()->toRoute('produit');
}
return new ViewModel(array('produit' => $produit));
}
public function deleteAction()
{
/* $em = $this
->getServiceLocator()
->get('Doctrine\ORM\EntityManager');
$commande = $em->find('Commande\Entity\Commande', 5);
if($commande)
{
echo 'Found an Commande\Entity\Commande: ' . PHP_EOL
. $commande->getIdCommande() . ' => ' . $commande->getTitleCommande() . '(' . get_class($commande) . ')' . PHP_EOL
. 'and ' . $commande->getProduits()->count() . ' Commande\Entity\Produit attached to it: ' . PHP_EOL;
if($produit = $commande->getProduits()->first())
{
echo 'Removing the first attached comment!' . PHP_EOL;
echo 'Removing comment with id=' . $produit->getIdProduit() . PHP_EOL;
$em->remove($produit);
$em->flush();
} else {
echo 'Could not find any comments to remove...' . PHP_EOL;
}
}
else
{
echo 'Could not find Commande\Entity\Commande with id=5';
}*/
$id = (int) $this->params()->fromRoute('id', 0);
$produit = $this->getEntityManager()->find('\Commande\Entity\Produit', $id);
if ($this->request->isPost()) {
$this->getEntityManager()->remove($produit);
$this->getEntityManager()->flush();
return $this->redirect()->toRoute('produit');
}
return new ViewModel(array('produit' => $produit));
}
public function getProduitTable()
{
}
}
感谢您的帮助。
如果您需要有关我的申请的其他信息,请告诉我。