下面的查询没有给出任何结果。

$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 =“大陆”)
 * /
大洲类{

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


}

最佳答案

我不知道您拥有的列名称是什么,但是您应该通过反引号将其转义:

   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

关于mysql - 原则2:JOIN查询不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22365222/

10-09 15:55
查看更多