原则2:JOIN查询不起作用

时间:2014-03-12 22:26:54

标签: mysql doctrine-orm entity

以下查询未给出任何结果。

$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);”打印查询它打印对象...但如果我尝试使用“print($ query-> getSQL());”来获取sql它不起作用。

非常感谢任何帮助。

请注意:没有加入,它可以正常工作。

实体映射

国家/地区实体

/ **  * @Entity(repositoryClass =“App \ Repository \ Continents”)  * @Table(name =“countries”)  * / 国家{

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

}

大陆实体

/ **  * @实体  * @Table(name =“continents”)  * / class Continents {

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

}

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

此问题已得到修复。问题出在我的实体中。我更新了我的大陆和国家实体如下,它运作良好。我希望这会帮助别人。

/ **  * @Entity(repositoryClass =“App \ Repository \ Continents”)  * @Table(name =“continents”)  * / class Continents {

/**
 * @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 =“App \ Repository \ Continents”)  * @Table(name =“countries”)  * / 国家{

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

}