broxi000 Posted January 13, 2014 Share Posted January 13, 2014 Hello everyone ! I'm studying ZF2 and i would like to create with ZendFrameWork 2 an application about manage "Produit" ( in french ) or "Product" ( english ) and "Commande" ... Now before installing zf2 and configure all source codes , and when i would to add a product on a command i have this error : Fatal error: Call to a member function add() on a non-object in So i can't add a product on a command ; Here is Commande.php : <?php namespace Commande\Entity; use Doctrine\ORM\Mapping as ORM; //Marquer notre classe comme entité avec /** * @ORM\Entity * @ORM\Table(name="commande") */ //par défaut si on ne sécifie pas de table, Doctrine va stoker cette //entité dans une table du même nom 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); } } and Produit.php : <?php namespace Commande\Entity; use Doctrine\ORM\Mapping as ORM; /** @ORM\Entity */ 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; } } And i think that that the mapping is bad configured , because i want to creat a mapping like this : - just one product can be added on a command Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 13, 2014 Share Posted January 13, 2014 Don't you need to have an instance of produit in order to call the add function in that class? Quote Link to comment Share on other sites More sharing options...
broxi000 Posted January 13, 2014 Author Share Posted January 13, 2014 Yes i need to have a instance of produit to add it on this class Quote Link to comment Share on other sites More sharing options...
gizmola Posted January 13, 2014 Share Posted January 13, 2014 All we have here is model classes. You are using the doctrine2 orm. Controllers or command scripts would need to actually initialize your database connection and instantiate objects before you persisted anything at all. Quote Link to comment Share on other sites More sharing options...
broxi000 Posted January 13, 2014 Author Share Posted January 13, 2014 (edited) here there is ProduitController.php : <?php namespace Commande\Controller; use Zend\Mvc\Controller\AbstractActionController; use Commande\Entity\Commande; use Commande\Entity\Produit; use Zend\View\Model\ViewModel; use Doctrine\ORM\EntityManager; 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() { } } and CommandeController.php : <?php namespace Commande\Controller; use Zend\Mvc\Controller\AbstractActionController; use Commande\Entity\Commande; use Commande\Entity\Produit; use Commande\Form\CommandeForm; use Doctrine\ORM\EntityManager; use Zend\View\Model\ViewModel; use Zend\Stdlib\Model\Registry; use Zend\Stdlib\Hydrator\DoctrineObject; 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() { } } I can add/edit/delete a commande but i can't for Profuit ... if you need something else tell me , thanks a lot . Edited January 13, 2014 by broxi000 Quote Link to comment Share on other sites More sharing options...
broxi000 Posted January 13, 2014 Author Share Posted January 13, 2014 i need to resolve that issue and the other one about mapping " Dependency Graph " , in my case i have this one so i want to have produit_id on table commande with ( produit(1) , commande(*) ) ; thanks . Quote Link to comment Share on other sites More sharing options...
broxi000 Posted January 13, 2014 Author Share Posted January 13, 2014 My issue can't be resolved or something is missing or my issue is not clear ? Thanks ! ! Quote Link to comment Share on other sites More sharing options...
broxi000 Posted January 14, 2014 Author Share Posted January 14, 2014 someone help me to resolve my issue ? Quote Link to comment Share on other sites More sharing options...
broxi000 Posted January 14, 2014 Author Share Posted January 14, 2014 Now i can't even add commande on the index page of my application the error : Found entity of type Doctrine\Common\Collections\ArrayCollection on association Really i need you'r help dudes ... ! Thanks Quote Link to comment Share on other sites More sharing options...
gizmola Posted January 14, 2014 Share Posted January 14, 2014 You just have to read this until you're really really clear what it offers: http://docs.doctrine-project.org/en/2.0.x/reference/association-mapping.html Quote Link to comment Share on other sites More sharing options...
broxi000 Posted January 15, 2014 Author Share Posted January 15, 2014 Hello again Dudes ! on my source code , for commande class i have this relation : /** * @ORM\ManyToOne(targetEntity="Produit", inversedBy="commandes",cascade={"persist"}) * @ORM\JoinColumn(name="produit_id", referencedColumnName="id") */ and for Produit class i have this relation : /** @ORM\OneToMany(targetEntity="Commande", mappedBy="produit", cascade={"persist"}) */ And when i see the mapping , i have this : so i wanna just have this relation between the tables " Produit " and "Commande" , so please how can i modify the relation on the source code to have this relation : thanks for replying ! Quote Link to comment Share on other sites More sharing options...
broxi000 Posted January 16, 2014 Author Share Posted January 16, 2014 A command consists of one or more products how can i do it on relation between the tables ( commande and produit ) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.