Jump to content

Fatal error: Call to a member function add() on a non-object in


broxi000

Recommended Posts

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

Link to comment
Share on other sites

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 by broxi000
Link to comment
Share on other sites

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 : 

 

1389809725-relation.jpg

 

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 :

 

1389809851-rrelation.jpg

 

thanks for replying ! 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.