2017-10-16 149 views
0

試圖通過在Symfony3中加入來解決此問題。Symfony3 - 左連接無法正常工作

莫名其妙的結果集已經混淆了值,即

ID Exploit Author 
ID Author Author 

請參閱隨附的屏幕截圖。

Screenshot

的OpenX應該有作者的Metasploit和第二行不應該在那裏。

有這樣的代碼在控制器:

public function indexAction() 
    { 
     $exploits = $this->getDoctrine() 
       ->getRepository('AppBundle:Exploit')->createQueryBuilder('e') 
      ->add('select', 'a,e') 
      ->add('from', 'AppBundle:Exploit e') 
      ->leftJoin('AppBundle:Author', 'a') 
      ->where('e.author = a.id') 
      ->getQuery() 
      ->getResult(); 

     return $this->render('exploit/index.html.twig', array(
      'exploits' => $exploits 
     )); 
    } 

這一觀點:

<tbody> 
      {% for exploit in exploits %} 
      <tr> 
       <th scope="row">{{ exploit.id }}</th> 
       <td>{{ exploit.name }}</td> 

       <td> {{ exploit.author }} </td> 

       <td> 
        <a href="/details/{{ exploit.id }}" class="btn btn-success">View</a> 
        <a href="/edit/{{ exploit.id }}" class="btn btn-default">Edit</a> 
        <a href="/delete/{{ exploit.id }}" class="btn btn-danger">Delete</a> 
       </td> 
      </tr> 
      {% endfor %} 

而這些實體:

<?php 

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* Author 
* 
* @ORM\Table(name="author", indexes={@ORM\Index(name="author_name_id_idx", columns={"id"})}) 
* @ORM\Entity 
*/ 
class Author 
{ 
    /** 
    * @var string 
    * 
    * @ORM\Column(name="name", type="string", length=255, nullable=false) 
    */ 
    private $name; 

    /** 
    * @var \Exploit 
    * 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="NONE") 
    * @ORM\OneToOne(targetEntity="Exploit") 
    * @ORM\JoinColumns({ 
    * @ORM\JoinColumn(name="id", referencedColumnName="author") 
    * }) 
    */ 
    private $id; 



    /** 
    * Set name 
    * 
    * @param string $name 
    * 
    * @return Author 
    */ 
    public function setName($name) 
    { 
     $this->name = $name; 

     return $this; 
    } 

    /** 
    * Get name 
    * 
    * @return string 
    */ 
    public function getName() 
    { 
     return $this->name; 
    } 

    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    public function getAuthor() 
    { 
     return $this->name; 
    } 
} 
<?php 

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* Author 
* 
* @ORM\Table(name="author", indexes={@ORM\Index(name="author_name_id_idx", columns={"id"})}) 
* @ORM\Entity 
*/ 
class Author 
{ 
    /** 
    * @var string 
    * 
    * @ORM\Column(name="name", type="string", length=255, nullable=false) 
    */ 
    private $name; 

    /** 
    * @var \Exploit 
    * 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="NONE") 
    * @ORM\OneToOne(targetEntity="Exploit") 
    * @ORM\JoinColumns({ 
    * @ORM\JoinColumn(name="id", referencedColumnName="author") 
    * }) 
    */ 
    private $id; 


} 

<?php 

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* Category 
* 
* @ORM\Table(name="category", indexes={@ORM\Index(name="category_name_id_idx", columns={"id"})}) 
* @ORM\Entity 
*/ 
class Category 
{ 
    /** 
    * @var string 
    * 
    * @ORM\Column(name="name", type="string", length=255, nullable=false) 
    */ 
    private $name; 

    /** 
    * @var \Exploit 
    * 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="NONE") 
    * @ORM\OneToOne(targetEntity="Exploit") 
    * @ORM\JoinColumns({ 
    * @ORM\JoinColumn(name="id", referencedColumnName="category") 
    * }) 
    */ 
    private $id; 



    /** 
    * Set name 
    * 
    * @param string $name 
    * 
    * @return Category 
    */ 
    public function setName($name) 
    { 
     $this->name = $name; 

     return $this; 
    } 

    /** 
    * Get name 
    * 
    * @return string 
    */ 
    public function getName() 
    { 
     return $this->name; 
    } 

    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 
} 
<?php 

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* Category 
* 
* @ORM\Table(name="category", indexes={@ORM\Index(name="category_name_id_idx", columns={"id"})}) 
* @ORM\Entity 
*/ 
class Category 
{ 
    /** 
    * @var string 
    * 
    * @ORM\Column(name="name", type="string", length=255, nullable=false) 
    */ 
    private $name; 

    /** 
    * @var \Exploit 
    * 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="NONE") 
    * @ORM\OneToOne(targetEntity="Exploit") 
    * @ORM\JoinColumns({ 
    * @ORM\JoinColumn(name="id", referencedColumnName="category") 
    * }) 
    */ 
    private $id; 


} 

<?php 

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* Exploit 
* 
* @ORM\Table(name="exploit", indexes={@ORM\Index(name="exploit_category_idx", columns={"category"}), @ORM\Index(name="exploit_type_idx", columns={"type"}), @ORM\Index(name="exploit_author_idx", columns={"author"})}) 
* @ORM\Entity 
*/ 
class Exploit 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="bigint", nullable=false) 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="IDENTITY") 
    */ 
    private $id; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="edb_id", type="string", length=100, nullable=false) 
    */ 
    private $edbId; 

    /** 
    * @var \DateTime 
    * 
    * @ORM\Column(name="date", type="datetime", nullable=false) 
    */ 
    private $date; 

    /** 
    * @var integer 
    * 
    * @ORM\Column(name="author", type="bigint", nullable=false) 
    */ 
    private $author; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="name", type="string", length=255, nullable=false) 
    */ 
    private $name; 

    /** 
    * @var integer 
    * 
    * @ORM\Column(name="category", type="bigint", nullable=false) 
    */ 
    private $category; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="version", type="string", length=255, nullable=false) 
    */ 
    private $version; 

    /** 
    * @var integer 
    * 
    * @ORM\Column(name="type", type="bigint", nullable=false) 
    */ 
    private $type; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="content", type="text", nullable=false) 
    */ 
    private $content; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="dork", type="string", length=255, nullable=true) 
    */ 
    private $dork; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="software_link", type="string", length=255, nullable=true) 
    */ 
    private $softwareLink; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="tested_on", type="string", length=255, nullable=true) 
    */ 
    private $testedOn; 



    /** 
    * Set edbId 
    * 
    * @param integer $edbId 
    * 
    * @return Exploit 
    */ 
    public function setEdbId($edbId) 
    { 
     $this->edbId = $edbId; 

     return $this; 
    } 

    /** 
    * Get edbId 
    * 
    * @return integer 
    */ 
    public function getEdbId() 
    { 
     return $this->edbId; 
    } 

    /** 
    * Set date 
    * 
    * @param \DateTime $date 
    * 
    * @return Exploit 
    */ 
    public function setDate($date) 
    { 
     $this->date = $date; 

     return $this; 
    } 

    /** 
    * Get date 
    * 
    * @return \DateTime 
    */ 
    public function getDate() 
    { 
     return $this->date; 
    } 

    /** 
    * Set author 
    * 
    * @param integer $author 
    * 
    * @return Exploit 
    */ 
    public function setAuthor($author) 
    { 
     $this->author = $author; 

     return $this; 
    } 

    /** 
    * Get author 
    * 
    * @return integer 
    */ 
    public function getAuthor() 
    { 
     return $this->author; 
    } 

    /** 
    * Set name 
    * 
    * @param string $name 
    * 
    * @return Exploit 
    */ 
    public function setName($name) 
    { 
     $this->name = $name; 

     return $this; 
    } 

    /** 
    * Get name 
    * 
    * @return string 
    */ 
    public function getName() 
    { 
     return $this->name; 
    } 

    /** 
    * Set category 
    * 
    * @param integer $category 
    * 
    * @return Exploit 
    */ 
    public function setCategory($category) 
    { 
     $this->category = $category; 

     return $this; 
    } 

    /** 
    * Get category 
    * 
    * @return integer 
    */ 
    public function getCategory() 
    { 
     return $this->category; 
    } 

    /** 
    * Set version 
    * 
    * @param string $version 
    * 
    * @return Exploit 
    */ 
    public function setVersion($version) 
    { 
     $this->version = $version; 

     return $this; 
    } 

    /** 
    * Get version 
    * 
    * @return string 
    */ 
    public function getVersion() 
    { 
     return $this->version; 
    } 

    /** 
    * Set type 
    * 
    * @param integer $type 
    * 
    * @return Exploit 
    */ 
    public function setType($type) 
    { 
     $this->type = $type; 

     return $this; 
    } 

    /** 
    * Get type 
    * 
    * @return integer 
    */ 
    public function getType() 
    { 
     return $this->type; 
    } 

    /** 
    * Set content 
    * 
    * @param string $content 
    * 
    * @return Exploit 
    */ 
    public function setContent($content) 
    { 
     $this->content = $content; 

     return $this; 
    } 

    /** 
    * Get content 
    * 
    * @return string 
    */ 
    public function getContent() 
    { 
     return $this->content; 
    } 

    /** 
    * Set dork 
    * 
    * @param string $dork 
    * 
    * @return Exploit 
    */ 
    public function setDork($dork) 
    { 
     $this->dork = $dork; 

     return $this; 
    } 

    /** 
    * Get dork 
    * 
    * @return string 
    */ 
    public function getDork() 
    { 
     return $this->dork; 
    } 

    /** 
    * Set softwareLink 
    * 
    * @param string $softwareLink 
    * 
    * @return Exploit 
    */ 
    public function setSoftwareLink($softwareLink) 
    { 
     $this->softwareLink = $softwareLink; 

     return $this; 
    } 

    /** 
    * Get softwareLink 
    * 
    * @return string 
    */ 
    public function getSoftwareLink() 
    { 
     return $this->softwareLink; 
    } 

    /** 
    * Set testedOn 
    * 
    * @param string $testedOn 
    * 
    * @return Exploit 
    */ 
    public function setTestedOn($testedOn) 
    { 
     $this->testedOn = $testedOn; 

     return $this; 
    } 

    /** 
    * Get testedOn 
    * 
    * @return string 
    */ 
    public function getTestedOn() 
    { 
     return $this->testedOn; 
    } 

    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 
} 
<?php 

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* Exploit 
* 
* @ORM\Table(name="exploit", indexes={@ORM\Index(name="exploit_category_idx", columns={"category"}), @ORM\Index(name="exploit_type_idx", columns={"type"}), @ORM\Index(name="exploit_author_idx", columns={"author"})}) 
* @ORM\Entity 
*/ 
class Exploit 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="bigint", nullable=false) 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="IDENTITY") 
    */ 
    private $id; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="edb_id", type="string", length=100, nullable=false) 
    */ 
    private $edbId; 

    /** 
    * @var \DateTime 
    * 
    * @ORM\Column(name="date", type="datetime", nullable=false) 
    */ 
    private $date; 

    /** 
    * @var integer 
    * 
    * @ORM\Column(name="author", type="bigint", nullable=false) 
    */ 
    private $author; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="name", type="string", length=255, nullable=false) 
    */ 
    private $name; 

    /** 
    * @var integer 
    * 
    * @ORM\Column(name="category", type="bigint", nullable=false) 
    */ 
    private $category; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="version", type="string", length=255, nullable=false) 
    */ 
    private $version; 

    /** 
    * @var integer 
    * 
    * @ORM\Column(name="type", type="bigint", nullable=false) 
    */ 
    private $type; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="content", type="text", nullable=false) 
    */ 
    private $content; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="dork", type="string", length=255, nullable=true) 
    */ 
    private $dork; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="software_link", type="string", length=255, nullable=true) 
    */ 
    private $softwareLink; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="tested_on", type="string", length=255, nullable=true) 
    */ 
    private $testedOn; 


} 

<?php 

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* Type 
* 
* @ORM\Table(name="type", indexes={@ORM\Index(name="type_name_id_idx", columns={"id"})}) 
* @ORM\Entity 
*/ 
class Type 
{ 
    /** 
    * @var string 
    * 
    * @ORM\Column(name="name", type="string", length=255, nullable=false) 
    */ 
    private $name; 

    /** 
    * @var \Exploit 
    * 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="NONE") 
    * @ORM\OneToOne(targetEntity="Exploit") 
    * @ORM\JoinColumns({ 
    * @ORM\JoinColumn(name="id", referencedColumnName="type") 
    * }) 
    */ 
    private $id; 



    /** 
    * Set name 
    * 
    * @param string $name 
    * 
    * @return Type 
    */ 
    public function setName($name) 
    { 
     $this->name = $name; 

     return $this; 
    } 

    /** 
    * Get name 
    * 
    * @return string 
    */ 
    public function getName() 
    { 
     return $this->name; 
    } 

    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 
} 
<?php 

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* Type 
* 
* @ORM\Table(name="type", indexes={@ORM\Index(name="type_name_id_idx", columns={"id"})}) 
* @ORM\Entity 
*/ 
class Type 
{ 
    /** 
    * @var string 
    * 
    * @ORM\Column(name="name", type="string", length=255, nullable=false) 
    */ 
    private $name; 

    /** 
    * @var \Exploit 
    * 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="NONE") 
    * @ORM\OneToOne(targetEntity="Exploit") 
    * @ORM\JoinColumns({ 
    * @ORM\JoinColumn(name="id", referencedColumnName="type") 
    * }) 
    */ 
    private $id; 


} 

有人知道如何改正我的聯接查詢?或者爲什麼它的行爲如此?

感謝,

回答

1

試試這個:

->leftJoin('AppBundle:Author', 'a', "WITH", "e.author = a.id") 

取而代之的是:

->leftJoin('AppBundle:Author', 'a') 
      ->where('e.author = a.id') 
+0

謝謝!試過,但不幸的是輸出是一樣的....沒有幫助:( –

0

嘗試

public function indexAction() 
{ 
    $exploits = $this->getDoctrine() 
     ->getRepository('AppBundle:Exploit')->createQueryBuilder('e') 
     ->leftjoin('e.author','a') 
     ->addSelect('a') 
     ->getQuery() 
     ->getResult(); 

    return $this->render('exploit/index.html.twig', array(
     'exploits' => $exploits 
    )); 
} 

,並在視圖

<tbody> 
      {% for exploit in exploits %} 
      <tr> 
       <th scope="row">{{ exploit.id }}</th> 
       <td>{{ exploit.name }}</td> 

       <td> {{ exploit.author.name }} </td> 

       <td> 
        <a href="/details/{{ exploit.id }}" class="btn btn-success">View</a> 
        <a href="/edit/{{ exploit.id }}" class="btn btn-default">Edit</a> 
        <a href="/delete/{{ exploit.id }}" class="btn btn-danger">Delete</a> 
       </td> 
      </tr> 
      {% endfor %} 
</tbody> 
+0

不幸的是我得到這個錯誤:[2/2] QueryException:[語義錯誤]行0,列63附近'a':錯誤:類AppBundle \ Entity \ Exploit沒有關聯作者 –

+1

那麼它是因爲你有從作者到漏洞的單向OneToOne關係。如果可能的話,將關係定義移動到exploit-> author字段或使關係成雙向(如果可能,最好避免雙向關係) – Asha

+0

謝謝!您似乎對這方面有所瞭解,也許您可​​以在我的其他相關問題中看到我已重寫了實體,請參閱Update 3:在此問題中:https://stackoverflow.com/questions/46764888/symfony3-showing-one-to-many-relations-in-a-view-...但不知怎的,它仍然不起作用 –