具有 OneToMany 关系的 Doctrine ArrayCollection 标准
Doctrine ArrayCollection Criteria with OneToMany Relationship
我在 "Employee" 和 "Status" 之间有一个 OneToMany 单向关系。
"Employee" 和 "Documents" 之间也存在 ManyToMany 双向关系。
当我有我的文档时,我试图通过 "Status"
我不断收到以下错误:
2018-04-05T14:35:19+00:00 [error] Error thrown while running command "app:expiration-check". Message: "Notice: Undefined index: Status"
In DefaultQuoteStrategy.php line 39:
Notice: Undefined index: Status
这是我使用的代码:
$Employees = $Document->getEmployees()->matching(
Criteria::create()
->andWhere(Criteria::expr()->eq('Status',$this->GlobalSettings->getApprovedEmployeeStatus()))
);
有趣的是,如果我使用员工存储库,则完全相同的标准会起作用
$Employees = $this->em->getRepository(Employee::class)->matching(
Criteria::create()
->andWhere(Criteria::expr()->eq('Status',$this->GlobalSettings->getApprovedEmployeeStatus()))
);
匹配静态字段也能正常工作。
$Employees = $Document->getEmployees()->matching(
Criteria::create()
->andWhere(Criteria::expr()->eq('FirstName',"Keven"))
);
这是状态栏的定义
/**
* @ORM\ManyToOne(targetEntity="Entity\Accounts\EmployeeStatus")
* @ORM\JoinColumn(name="StatusId", referencedColumnName="id", nullable=false)
*/
private $Status;
这是员工定义(在文档实体上)
/**
* @ORM\ManyToMany(targetEntity="Entity\Accounts\Employee", mappedBy="Documents")
*/
private $Employees;
/**
* Constructor
*/
public function __construct()
{
parent::__construct();
$this->Employees = new \Doctrine\Common\Collections\ArrayCollection();
}
这是 getEmployees()(也在文档中)
/**
* Get employees.
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getEmployees()
{
return $this->Employees;
}
要管理 ManyToMany 关系,学说使用 Doctrine\ORM\Persisters\Collection\ManyToManyPersister class。
你可以看到它正在被使用here
不幸的是,目前在最新版本 v2.6.1 中,此 class 的方法 loadCriteria 缺少使用关系字段的功能。仅支持静态字段。
目前查看master分支,增加了这个支持:Doctrine\ORM\Persisters\Collection\ManyToManyPersister as of today
但它还不是发布的一部分。另外快速浏览一下 2.7 branch 它看起来不会在那里。
我不确定您是否可以将 master 分支与 symfony 的 doctrine bundle 一起使用。我认为现在很难让它发挥作用。
你可以做的,是初始化ManyToMany集合 $Document->getEmployees()
然后使用匹配函数,这意味着你加载所有员工然后过滤,而不是偷懒按照您的预期加载。
也一样:
$employees = $Document->getEmployees();
$employees->initialize();
$employees->matching(
Criteria::create()
->andWhere(Criteria::expr()->eq('Status',$this->GlobalSettings->getApprovedEmployeeStatus()))
);
并在发布新更改时注释更改代码。
我在 "Employee" 和 "Status" 之间有一个 OneToMany 单向关系。 "Employee" 和 "Documents" 之间也存在 ManyToMany 双向关系。 当我有我的文档时,我试图通过 "Status"
我不断收到以下错误:
2018-04-05T14:35:19+00:00 [error] Error thrown while running command "app:expiration-check". Message: "Notice: Undefined index: Status"
In DefaultQuoteStrategy.php line 39:
Notice: Undefined index: Status
这是我使用的代码:
$Employees = $Document->getEmployees()->matching(
Criteria::create()
->andWhere(Criteria::expr()->eq('Status',$this->GlobalSettings->getApprovedEmployeeStatus()))
);
有趣的是,如果我使用员工存储库,则完全相同的标准会起作用
$Employees = $this->em->getRepository(Employee::class)->matching(
Criteria::create()
->andWhere(Criteria::expr()->eq('Status',$this->GlobalSettings->getApprovedEmployeeStatus()))
);
匹配静态字段也能正常工作。
$Employees = $Document->getEmployees()->matching(
Criteria::create()
->andWhere(Criteria::expr()->eq('FirstName',"Keven"))
);
这是状态栏的定义
/**
* @ORM\ManyToOne(targetEntity="Entity\Accounts\EmployeeStatus")
* @ORM\JoinColumn(name="StatusId", referencedColumnName="id", nullable=false)
*/
private $Status;
这是员工定义(在文档实体上)
/**
* @ORM\ManyToMany(targetEntity="Entity\Accounts\Employee", mappedBy="Documents")
*/
private $Employees;
/**
* Constructor
*/
public function __construct()
{
parent::__construct();
$this->Employees = new \Doctrine\Common\Collections\ArrayCollection();
}
这是 getEmployees()(也在文档中)
/**
* Get employees.
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getEmployees()
{
return $this->Employees;
}
要管理 ManyToMany 关系,学说使用 Doctrine\ORM\Persisters\Collection\ManyToManyPersister class。 你可以看到它正在被使用here
不幸的是,目前在最新版本 v2.6.1 中,此 class 的方法 loadCriteria 缺少使用关系字段的功能。仅支持静态字段。
目前查看master分支,增加了这个支持:Doctrine\ORM\Persisters\Collection\ManyToManyPersister as of today 但它还不是发布的一部分。另外快速浏览一下 2.7 branch 它看起来不会在那里。
我不确定您是否可以将 master 分支与 symfony 的 doctrine bundle 一起使用。我认为现在很难让它发挥作用。
你可以做的,是初始化ManyToMany集合 $Document->getEmployees()
然后使用匹配函数,这意味着你加载所有员工然后过滤,而不是偷懒按照您的预期加载。
也一样:
$employees = $Document->getEmployees();
$employees->initialize();
$employees->matching(
Criteria::create()
->andWhere(Criteria::expr()->eq('Status',$this->GlobalSettings->getApprovedEmployeeStatus()))
);
并在发布新更改时注释更改代码。