douzhi9635 2014-01-14 15:07
浏览 69

找到关联时Doctrine \ Common \ Collections \ ArrayCollection类型的实体

I'm studying ZendFramework2 Doctrine ORM, when I configure the source code ( MVC ) : classes , controllers. My application is about managing "Commande" and "Produit" , the issue is when I want to add a commande I have this Error :

Found entity of type Doctrine\Common\Collections\ArrayCollection on association

This is 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;
}
}

I have this for Commande controller :

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()
{


  }
}

and this for 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()
{


}
}

Thanks for helping me.

If you need other information about my application tell me .

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
    • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
    • ¥60 pb数据库修改与连接
    • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
    • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
    • ¥20 神经网络Sequential name=sequential, built=False
    • ¥16 Qphython 用xlrd读取excel报错
    • ¥15 单片机学习顺序问题!!
    • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
    • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)