Search code examples
phpsymfonydql

how to join relation Bidirectional ManyToOne SYMFONY


A relationship between the customer and the order.A customer places several orders bidirectional. I want to retrieve the orders by a customer,if the name of the customer enters and I see its command

class Client
{
  /**
     * @var string
     *
     * @ORM\Column(name="nomCl", type="string", length=255)
     */
    private $nomCl;
    
    /**
     * @ORM\OneToMany(targetEntity="Commande", mappedBy="clt", cascade={"persist","remove"})
     * @ORM\JoinColumn(nullable=true)
     */
    private $coms;

    public function __construct()
    {
        $this->coms=new ArrayCollection();
    }
//getters and setters
}

class Commande
{
  /**
     * @var string
     *
     * @ORM\Column(name="numeroC", type="string", length=255)
     */
    private $numeroC;
    
    **
     * @var \DateTime
     *
     * @ORM\Column(name="dateC", type="date")
     */
    private $dateC;
    
    /**
     *@ORM\ManyToOne(targetEntity="Client",inversedBy="coms", cascade={"persist"})
     * @ORM\JoinColumns({
     * @ORM\JoinColumn(name="cl_id", referencedColumnName="id")
     * })
     */
    private $clt;
 //getters,set
 }
    

Controller:

/**
  * @Route("/clt/com", name="info_clt")
  */

    public function cltAction(Request $request)
    {
        $em=$this->getDoctrine()->getManager();
        $motcl=$request->query->get('motcle');//To retrieve the entered custormer `enter code here`name 
        if ($motcl!='') {

             $nomclts=$em->getRepository('SiteTestBundle:Client')->findByNom($motcl);
    //Here I want to retrieve the client id enter And I get his order      
           foreach ($nomclts as $var) {
             $var1[]=$var->getId();
           }

             $comms = $var1->getComms();

             return $this->render('SiteTestBundle:Default:clt.html.twig',array('nomclts'=>$nomclts,'comms'=>$comms));

}

View:

<div class="panel panel-primary">
    	<div class="panel-heading">Information sur le client</div>
    		<div class="panel-body">
    			<div>
    			<label>Nom:</label><br>
    				{% for list in nomclts %}					
    					{{ list.nom }}<br>
    				{%endfor %}
    			</div>						
    		 </div>
 </div>
 <div class="col-md-6">
        <div class="panel panel-primary">
        	<div class="panel-heading">Listes des commandes</div>
        	<div class="panel-body">
        		{% for list in comms %}
        		  {{ list.numero }}<br>
        		{%endfor %}
        	</div>
        </div>
  </div>	

So,a little search engine, I enter the name customer and I would have his order Thank you for your help


Solution

  • When you search the differents order for a customer, do you have the id of this customer ?
    If you have the id, you can make something like this:

    $listCommande = $repository->findBy(array('clt' => $id));