【发布时间】:2015-09-18 18:22:16
【问题描述】:
使用 Doctrine2,我正在尝试在一个实体上执行 findOneBy,其中我使用 OneToOne 连接了一个表,并且我想搜索连接表中的列。
发挥作用的两个 PHP 实体是(简化版本):
页面:
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="Page")
* @ORM\HasLifecycleCallbacks()
*/
class Page extends EntityInterface
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="boolean")
*/
public $isActive;
/**
* @ORM\OneToOne(targetEntity="\PageLocalization")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id", referencedColumnName="pageId")
* })
**/
public $pageLocalization;
}
页面本地化:
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="PageLocalization")
* @ORM\HasLifecycleCallbacks()
*/
class PageLocalization extends EntityInterface
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
*/
public $pageId;
/**
* @ORM\Column(type="string")
*/
public $localeCode;
/**
* @ORM\Column(type="string")
*/
public $title;
}
实体工作,我可以很好地提取数据,例如$entityRepository->findOneBy(["id"=>1]).
例如,现在我想搜索Page.id = 1、Page.isActive = true 和PageLocalization.localeCode = "en-US"。这个搜索是如何执行的?
我在下面的尝试不起作用。
$entityRepository->findOneBy([
"id" => 1,
"isActive" => true,
"pageLocalization" => [
"localeCode" => "en-US"
]
]);
我没有运气通过 Google、Stackoverflow 或 Doctrine2 文档找到答案。
我看到的最常见的解决方案是人们习惯于使用$entityManager->createQueryBuilder() 手动构建查询。但是,我觉得这种方法违背了拥有实体的目的。
这种类型的搜索是否完全可以使用纯粹的注释和实体?
【问题讨论】:
标签: php join doctrine-orm annotations