如何加入关系双向 ManyToOne SYMFONY

how to join relation Bidirectional ManyToOne SYMFONY

客户与 order.A 客户之间的关系下了多个双向订单。 我想检索客户的订单,如果客户的名字输入并且我看到它的命令

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
 }
    

控制器:

/**
  * @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));

}

查看:

<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> 

所以,一个小搜索引擎,我输入客户的名字,我会得到他的订单 感谢您的帮助

当你搜索一个客户的不同订单时,你有这个客户的id吗?
如果你有id,你可以做这样的事情:

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

首先,我不能 100% 确定 @ORM\JoinColumns({}) 是否有效,或者是否需要它。其他人可以随时纠正我,但我从来没有使用过它。对我来说,它在您的实体中应该看起来像这样。我可能 100% 错,而且它很可能对你有用,因为 Symfony 允许这么多配置。

 /**
  *@ORM\ManyToOne(targetEntity="Client",inversedBy="coms", cascade={"persist"})
  *@ORM\JoinColumn(name="cl_id", referencedColumnName="id")
  */
  private $clt;

前提是您必须 100% 匹配客户姓名:

/**
 * @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 
    $nomclts = null;

    if ($motcl!='') {
        $nomclts=$em->getRepository('SiteTestBundle:Client')->findByNom($motcl);
    }

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

和HTML

<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 nom in nomclts %}
                  {% for list in nom.comms %}
                    {{ list.numero }}<br>
                  {% endfor %}
            {% endfor %}
            </div>
        </div>
  </div>