我的联接在sql中看起来像这样:
SELECT m.*
FROM settings AS s
LEFT JOIN modules AS m on s.name = m.module
WHERE s.value = 1
AND s.category = 'module_status'
我无法弄清楚如何在Doctrine 2中重现这一点。非常感谢任何帮助!
以下是我的实体:
namespace Entities;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Modules
*
* @Table(name="modules")
* @Entity(repositoryClass="Repositories\Modules")
*/
class Modules
{
/**
* @var integer $id
*
* @Column(name="id", type="integer", nullable=false)
* @Id
* @GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string $module
* @Column(name="module", type="string", length=255, nullable=false)
*/
private $module;
/**
* @var string $label
* @Column(name="label", type="string", length=255, nullable=false)
*/
private $label;
/**
* @var string $package
*
* @Column(name="package", type="string", length=255, nullable=true)
*/
private $package;
/**
* @var string $path
*
* @Column(name="path", type="string", length=255, nullable=true)
*/
private $path;
/**
* @var float $version
*
* @Column(name="version", type="float", nullable=false)
*/
private $version;
public function getId() {
return $this->id;
}
public function getModule() {
return $this->module;
}
public function getLabel() {
return $this->label;
}
public function getPackage() {
return $this->package;
}
public function getPath() {
return $this->path;
}
public function getVersion() {
return $this->version;
}
public function setId($id) {
$this->id = $id;
}
public function setModule($module) {
$this->module = $module;
}
public function setLabel($label) {
$this->label = $label;
}
public function setPackage($package) {
$this->package = $package;
}
public function setPath($path) {
$this->path = $path;
}
public function setVersion($version) {
$this->version = $version;
}
}
namespace Entities;
use Doctrine\ORM\Mapping as ORM;
/**
* Settings
*
* @Table(name="settings")
* @Entity(repositoryClass="Repositories\Settings")
*/
class Settings
{
/**
* @var integer $id
*
* @Column(name="id", type="integer", nullable=false)
* @Id
* @GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string $name
*
* @Column(name="name", type="string", length=255, nullable=true)
*/
private $name;
/**
* @var string $category
*
* @Column(name="category", type="string", length=255, nullable=true)
*/
private $category;
/**
* @var text $value
*
* @Column(name="value", type="text", nullable=true)
*/
private $value;
public function getId() {
return $this->id;
}
public function getName() {
return $this->name;
}
public function setName($name) {
$this->name = $name;
}
public function getCategory() {
return $this->category;
}
public function setCategory($category) {
$this->category = $category;
}
public function getValue() {
return $this->value;
}
public function setValue($value) {
$this->value = $value;
}
}
答案 0 :(得分:3)
首先,您需要在“设置”和“模块”实体之间配置关联。我假设两者之间的关系是多对多的:
Class Modules
{
/**
* @ManyToMany(targetEntity="Settings", inversedBy="modules")
* @JoinTable(name="modules_settings")
*/
private $settings;
}
Class Settings
{
/**
* @ManyToMany(targetEntity="Modules", mappedBy="settings")
*/
private $modules;
}
然后您的查询语法如下所示:
$qb->select('s', 'm')
->from('Entities\Settings', 's')
->leftJoin('s.modules')
->where('s.value = 1');
我无法分析你的AND子句中发生了什么,但是如果你拥有的是正确的,那么你只需将这一行添加到查询的末尾:
->andWhere('s.category = m.status');
答案 1 :(得分:1)
感谢您的帮助。这就是查询最终的结果:
$qb = $this->_em->createQueryBuilder()
->select('m')
->from('Entities\Modules', 'm')
->leftJoin('m.settings', 's')
->where('s.value = :enabled')
->andWhere('s.category = :moduleStatus')
->setParameter('moduleStatus', 'module_status')
->setParameter('enabled', 1)
->getQuery();
这是我配置模块实体的方式:
/**
* @OneToOne(targetEntity="Entities\Settings")
* @JoinColumn(name="module", referencedColumnName="name")
*/
private $settings;