【问题标题】:Doctrine 2: JOIN query not working原则 2:JOIN 查询不起作用
【发布时间】:2014-03-12 22:26:53
【问题描述】:

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

$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="国家") */ 类国家 {

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

}

【问题讨论】:

  • 在使用JOIN时不要忘记使用ON子句。
  • 映射后需要ON子句吗? @Edper

标签: mysql doctrine-orm entity


【解决方案1】:

我不知道你的这个列名是什么,但你应该用这样的反引号来转义它:

   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

【讨论】:

  • 我有两个表'大陆'和'国家'使用一对多关联的学说连接在一起(一个大陆有许多国家)。我正在检索两个表中的所有列。
  • 下面的类似查询在没有'ON CLAUSE'的情况下工作 $query = $this->getEntityManager()->createQuery(" SELECT sa, sc FROM App\Entity\Studyabroadcourses sa JOIN sa.studyabroadschools sc WHERE sa.coursename LIKE :searchTerm AND sa.enabled = 1 AND sa.deleted = 0 ") ->setParameters(array( 'searchTerm' => '%' . $searchTerm . '%' ));
  • 下面的关联应该负责映射 /** * @ORM\ManyToOne(targetEntity="App\Entity\Continents", inversedBy="countries") * @ORM\JoinColumn(name=" id", referencedColumnName="continent_id") */ protected $continents;
  • /** * @ORM\OneToMany(targetEntity="App\Entity\Countries", mappedBy="continents") */ protected $countries;
【解决方案2】:

此问题已修复。问题出在我的实体中。我如下更新了我的大陆和国家实体,它运行良好。我希望这对其他人有帮助。

/** * @Entity(repositoryClass="App\Repository\Continents") * @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;
}

}

/** * @Entity(repositoryClass="App\Repository\Continents") * @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;
}

}

【讨论】:

    猜你喜欢
    • 2015-03-10
    • 2020-04-03
    • 1970-01-01
    • 2018-11-24
    • 1970-01-01
    • 1970-01-01
    • 2015-09-23
    • 2011-05-19
    • 1970-01-01
    相关资源
    最近更新 更多