如何加入关系双向ManyToOne SYMFONY

时间:2017-05-23 14:19:53

标签: php symfony dql

客户与订单之间的关系。客户双向下订单。 我想要客户检索订单,如果客户的名字输入,我看到它的命令

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>	

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

2 个答案:

答案 0 :(得分:0)

当您搜索客户的不同订单时,您是否拥有此客户的ID?
如果你有身份证,你可以这样做:

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

答案 1 :(得分:0)

对于初学者来说,我不能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>