2014-03-12 35 views
0

下面的查詢沒有給出任何結果。原則2:JOIN查詢不起作用

$query = $this->em->createQuery("    
       SELECT cs, cc 
       FROM App\Entity\Continents cs 
       JOIN cs.countries cc 
       WHERE cs.enabled = 1 AND cs.deleted = 0 
       "); 

如果我用「print_r($ query);」打印查詢,它打印的對象...但如果我嘗試使用「打印($ query-> getSQL())得到sql;」它不起作用。

任何幫助將不勝感激。

請注意:沒有連接,它工作正常。

實體映射

國家實體

/** * @Entity(repositoryClass = 「應用程序\庫\大洲」) * @Table(NAME = 「國家」) */ 類國家{

/** 
* @Id @Column(type="integer") 
* @GeneratedValue(strategy="AUTO") 
*/ 
protected $id; 

/** 
* @ORM\ManyToOne(targetEntity="App\Entity\Continents", inversedBy="countries") 
* @ORM\JoinColumn(name="id", referencedColumnName="continent_id") 
*/ 
protected $continents; 

/** @Column(type="integer", length=1) */ 
protected $continent_id; 

/** @Column(type="string", length=3) */ 
protected $iso_number; 

/** @Column(type="string", length=2) */ 
protected $iso_2_code; 

/** @Column(type="string", length=3) */ 
protected $iso_3_code; 

/** @Column(type="string", length=45) */ 
protected $name; 

/** @Column(type="string", length=3) */ 
protected $default_currency; 

/** @Column(type="string", length=3) */ 
protected $currency_symbol; 

/** @Column(type="integer", length=3) */ 
protected $currency_id; 

/** @Column(type="integer", length=1) */ 
protected $postcode_check; 

/** @Column(type="string", length=150) */ 
protected $postcode_regex; 

/** @Column(type="integer", length=1) */ 
protected $enabled; 

/** @Column(type="integer", length=1) */ 
protected $deleted; 

/** @Column(type="datetime") */ 
protected $created; 

/** @Column(type="integer", length=11) */ 
protected $created_by; 

/** @Column(type="datetime") */ 
protected $modified; 

/** @Column(type="integer", length=11) */ 
protected $modified_by; 

public function __construct() { 

} 

public function getId() { 
    return $this->id; 
} 

public function setId($id) { 
    $this->id = $id; 
} 

public function getContinents() { 
    return $this->continents; 
} 

public function setContinents(\App\Entity\Continents $continents) { 
    $this->continents = $continents; 
} 

public function getContinent_id() { 
    return $this->continent_id; 
} 

public function setContinent_id($continent_id) { 
    $this->continent_id = $continent_id; 
} 

public function getIso_number() { 
    return $this->iso_number; 
} 

public function setIso_number($iso_number) { 
    $this->iso_number = $iso_number; 
} 

public function getIso_2_code() { 
    return $this->iso_2_code; 
} 

public function setIso_2_code($iso_2_code) { 
    $this->iso_2_code = $iso_2_code; 
} 

public function getIso_3_code() { 
    return $this->iso_3_code; 
} 

public function setIso_3_code($iso_3_code) { 
    $this->iso_3_code = $iso_3_code; 
} 

public function getName() { 
    return $this->name; 
} 

public function setName($name) { 
    $this->name = $name; 
} 

public function getDefault_currency() { 
    return $this->default_currency; 
} 

public function setDefault_currency($default_currency) { 
    $this->default_currency = $default_currency; 
} 

public function getCurrency_symbol() { 
    return $this->currency_symbol; 
} 

public function setCurrency_symbol($currency_symbol) { 
    $this->currency_symbol = $currency_symbol; 
} 

public function getCurrency_id() { 
    return $this->currency_id; 
} 

public function setCurrency_id($currency_id) { 
    $this->currency_id = $currency_id; 
} 

public function getPostcode_check() { 
    return $this->postcode_check; 
} 

public function setPostcode_check($postcode_check) { 
    $this->postcode_check = $postcode_check; 
} 

public function getPostcode_regex() { 
    return $this->postcode_regex; 
} 

public function setPostcode_regex($postcode_regex) { 
    $this->postcode_regex = $postcode_regex; 
} 

public function getEnabled() { 
    return $this->enabled; 
} 

public function setEnabled($enabled) { 
    $this->enabled = $enabled; 
} 

public function getDeleted() { 
    return $this->deleted; 
} 

public function setDeleted($deleted) { 
    $this->deleted = $deleted; 
} 

public function getCreated() { 
    return $this->created; 
} 

public function setCreated($created) { 
    $this->created = $created; 
} 

public function getModified() { 
    return $this->modified; 
} 

public function setModified($modified) { 
    $this->modified = $modified; 
} 

public function getCreated_by() { 
    return $this->created_by; 
} 

public function setCreated_by($created_by) { 
    $this->created_by = $created_by; 
} 

public function getModified_by() { 
    return $this->modified_by; 
} 

public function setModified_by($modified_by) { 
    $this->modified_by = $modified_by; 
} 

}

洲實體

/** * @Entity * @Table(名稱= 「大洲」) */ 類洲{

/** 
* @Id @Column(type="integer") 
* @GeneratedValue(strategy="AUTO") 
*/ 
protected $id; 

/** 
* @ORM\OneToMany(targetEntity="App\Entity\Countries", mappedBy="continents") 
*/ 
protected $countries; 

/** @Column(type="string", length=7) */ 
protected $name; 

/** @Column(type="integer", length=1) */ 
protected $enabled; 

/** @Column(type="integer", length=1) */ 
protected $deleted; 

/** @Column(type="datetime") */ 
protected $created; 

/** @Column(type="integer", length=11) */ 
protected $created_by; 

/** @Column(type="datetime") */ 
protected $modified; 

/** @Column(type="integer", length=11) */ 
protected $modified_by; 

public function __construct() { 
    $this->countries = new ArrayCollection(); 
} 

public function getCountries() { 
    return $this->countries; 
} 

public function setCountries($countries) { 
    $this->countries = $countries; 
} 

public function getName() { 
    return $this->name; 
} 

public function setName($name) { 
    $this->name = $name; 
} 

public function getId() { 
    return $this->id; 
} 

public function setId($id) { 
    $this->id = $id; 
} 

public function getCreated_by() { 
    return $this->created_by; 
} 

public function setCreated_by($created_by) { 
    $this->created_by = $created_by; 
} 

public function getModified_by() { 
    return $this->modified_by; 
} 

public function setModified_by($modified_by) { 
    $this->modified_by = $modified_by; 
} 

public function getEnabled() { 
    return $this->enabled; 
} 

public function setEnabled($enabled) { 
    $this->enabled = $enabled; 
} 

public function getDeleted() { 
    return $this->deleted; 
} 

public function setDeleted($deleted) { 
    $this->deleted = $deleted; 
} 

public function getCreated() { 
    return $this->created; 
} 

public function setCreated($created) { 
    $this->created = $created; 
} 

public function getModified() { 
    return $this->modified; 
} 

public function setModified($modified) { 
    $this->modified = $modified; 
} 

}

+0

當您使用'JOIN'時,不要忘記使用'ON'子句。 – Edper

+0

執行映射之後是否需要ON子句? @Edper – user3412905

回答

0

我不知道你有這個專欄名稱,但你應該通過這樣的反引號逃脫它:

SELECT cs.* , cc.*   -- //--you are selecting tables. 
    FROM `Continents` cs --escape by backticks here 
    JOIN countries cc --no need cs here 
    ON ............  --condition here (relation between the two tables) 
    WHERE cs.enabled = 1 AND cs.deleted = 0 --this is good 
+0

我有兩張桌子'洲'和'國家'使用學說一對多協會(一個大陸有很多國家)聯繫在一起。我正在檢索兩個表中的所有列。 – user3412905

+0

我試過上面的查詢,它仍然不起作用 – user3412905

+0

查看編輯答案,你必須使ON子句 –

0

此問題已得到解決。問題出在我的實體。我更新了我的大陸和國家實體,如下所示,它運行良好。我希望這會幫助別人。

/** * @Entity(repositoryClass = 「應用\庫\洲」) * @Table(名稱= 「大洲」) */ 類洲{

/** 
* @Id @Column(type="integer") 
* @GeneratedValue(strategy="AUTO") 
*/ 
protected $id; 

/** 
* @ORM\OneToMany(targetEntity="App\Entity\Countries", mappedBy="continents") 
*/ 
protected $countries; 

/** @Column(type="string", length=7) */ 
protected $name; 

/** @Column(type="integer", length=1) */ 
protected $enabled; 

/** @Column(type="integer", length=1) */ 
protected $deleted; 

/** @Column(type="datetime") */ 
protected $created; 

/** @Column(type="integer", length=11) */ 
protected $created_by; 

/** @Column(type="datetime") */ 
protected $modified; 

/** @Column(type="integer", length=11) */ 
protected $modified_by; 

public function __construct() { 
    $this->countries = new ArrayCollection(); 
} 

public function getCountries() { 
    return $this->countries; 
} 

public function setCountries($countries) { 
    $this->countries = $countries; 
} 

public function getName() { 
    return $this->name; 
} 

public function setName($name) { 
    $this->name = $name; 
} 

public function getId() { 
    return $this->id; 
} 

public function setId($id) { 
    $this->id = $id; 
} 

public function getCreated_by() { 
    return $this->created_by; 
} 

public function setCreated_by($created_by) { 
    $this->created_by = $created_by; 
} 

public function getModified_by() { 
    return $this->modified_by; 
} 

public function setModified_by($modified_by) { 
    $this->modified_by = $modified_by; 
} 

public function getEnabled() { 
    return $this->enabled; 
} 

public function setEnabled($enabled) { 
    $this->enabled = $enabled; 
} 

public function getDeleted() { 
    return $this->deleted; 
} 

public function setDeleted($deleted) { 
    $this->deleted = $deleted; 
} 

public function getCreated() { 
    return $this->created; 
} 

public function setCreated($created) { 
    $this->created = $created; 
} 

public function getModified() { 
    return $this->modified; 
} 

public function setModified($modified) { 
    $this->modified = $modified; 
} 

}

/** * @Entity(repositoryClass = 「應用程序\庫\大洲」) * @Table(NAME = 「國家」) */ 類國家{

/** 
* @Id @Column(type="integer") 
* @GeneratedValue(strategy="AUTO") 
*/ 
protected $id; 

/** 
* @ORM\ManyToOne(targetEntity="App\Entity\Continents", inversedBy="countries") 
* @ORM\JoinColumn(name="continent_id", referencedColumnName="id") 
*/ 
protected $continents; 

/** @Column(type="integer", length=1) */ 
protected $continent_id; 

/** @Column(type="string", length=3) */ 
protected $iso_number; 

/** @Column(type="string", length=2) */ 
protected $iso_2_code; 

/** @Column(type="string", length=3) */ 
protected $iso_3_code; 

/** @Column(type="string", length=45) */ 
protected $name; 

/** @Column(type="string", length=3) */ 
protected $default_currency; 

/** @Column(type="string", length=3) */ 
protected $currency_symbol; 

/** @Column(type="integer", length=3) */ 
protected $currency_id; 

/** @Column(type="integer", length=1) */ 
protected $postcode_check; 

/** @Column(type="string", length=150) */ 
protected $postcode_regex; 

/** @Column(type="integer", length=1) */ 
protected $enabled; 

/** @Column(type="integer", length=1) */ 
protected $deleted; 

/** @Column(type="datetime") */ 
protected $created; 

/** @Column(type="integer", length=11) */ 
protected $created_by; 

/** @Column(type="datetime") */ 
protected $modified; 

/** @Column(type="integer", length=11) */ 
protected $modified_by; 

public function __construct() { 

} 

public function getId() { 
    return $this->id; 
} 

public function setId($id) { 
    $this->id = $id; 
} 

public function getContinents() { 
    return $this->continents; 
} 

public function setContinents(\App\Entity\Continents $continents) { 
    $this->continents = $continents; 
} 

public function getContinent_id() { 
    return $this->continent_id; 
} 

public function setContinent_id($continent_id) { 
    $this->continent_id = $continent_id; 
} 

public function getIso_number() { 
    return $this->iso_number; 
} 

public function setIso_number($iso_number) { 
    $this->iso_number = $iso_number; 
} 

public function getIso_2_code() { 
    return $this->iso_2_code; 
} 

public function setIso_2_code($iso_2_code) { 
    $this->iso_2_code = $iso_2_code; 
} 

public function getIso_3_code() { 
    return $this->iso_3_code; 
} 

public function setIso_3_code($iso_3_code) { 
    $this->iso_3_code = $iso_3_code; 
} 

public function getName() { 
    return $this->name; 
} 

public function setName($name) { 
    $this->name = $name; 
} 

public function getDefault_currency() { 
    return $this->default_currency; 
} 

public function setDefault_currency($default_currency) { 
    $this->default_currency = $default_currency; 
} 

public function getCurrency_symbol() { 
    return $this->currency_symbol; 
} 

public function setCurrency_symbol($currency_symbol) { 
    $this->currency_symbol = $currency_symbol; 
} 

public function getCurrency_id() { 
    return $this->currency_id; 
} 

public function setCurrency_id($currency_id) { 
    $this->currency_id = $currency_id; 
} 

public function getPostcode_check() { 
    return $this->postcode_check; 
} 

public function setPostcode_check($postcode_check) { 
    $this->postcode_check = $postcode_check; 
} 

public function getPostcode_regex() { 
    return $this->postcode_regex; 
} 

public function setPostcode_regex($postcode_regex) { 
    $this->postcode_regex = $postcode_regex; 
} 

public function getEnabled() { 
    return $this->enabled; 
} 

public function setEnabled($enabled) { 
    $this->enabled = $enabled; 
} 

public function getDeleted() { 
    return $this->deleted; 
} 

public function setDeleted($deleted) { 
    $this->deleted = $deleted; 
} 

public function getCreated() { 
    return $this->created; 
} 

public function setCreated($created) { 
    $this->created = $created; 
} 

public function getModified() { 
    return $this->modified; 
} 

public function setModified($modified) { 
    $this->modified = $modified; 
} 

public function getCreated_by() { 
    return $this->created_by; 
} 

public function setCreated_by($created_by) { 
    $this->created_by = $created_by; 
} 

public function getModified_by() { 
    return $this->modified_by; 
} 

public function setModified_by($modified_by) { 
    $this->modified_by = $modified_by; 
} 

}