Doctrine 自定义映射类型存在问题
我正在使用Doctrine ODM并尝试创建一个自定义映射类型,但我遇到了一些问题。 我的映射类型与集合类型类似,但它与ArrayCollection一起工作:Doctrine 自定义映射类型存在问题,doctrine,mongodb,Doctrine,Mongodb,我正在使用Doctrine ODM并尝试创建一个自定义映射类型,但我遇到了一些问题。 我的映射类型与集合类型类似,但它与ArrayCollection一起工作: <?php class ArrayCollectionType extends Type { public function convertToDatabaseValue($value) { return $value !== null ? array_values($value->toAr
<?php
class ArrayCollectionType extends Type
{
public function convertToDatabaseValue($value)
{
return $value !== null ? array_values($value->toArray()) : null;
}
public function convertToPHPValue($value)
{
return $value !== null ? new ArrayCollection($value) : null;
}
public function closureToMongo()
{
return '$return = $value !== null ? array_values($value->toArray()) : null;';
}
public function closureToPHP()
{
return '$return = $value !== null ? new \Doctrine\Common\Collections\ArrayCollection($value) : null;';
}
}
实际结果
object(Doctrine\Common\Collections\ArrayCollection)[237]
private '_elements' =>
array
0 => string 'testing-slug' (length=12)
1 => string 'another-test' (length=12)
您可能需要使用不同的更改跟踪策略。在这种情况下,我会选择我尝试实施不同的更改跟踪策略,但无法启动更新 最后,我意识到它没有检测到变化,因为对象是通过引用传递的。因此,文档的简单更新未被检测到,因为与原始文档进行比较时,它是相同的引用 解决方案是在进行更改时克隆对象:
public function addSlug($slug)
{
$this->slugs = clone $this->slugs;
$this->slugs->add($slug);
}
回顾过去,虽然使用“通知”的更改跟踪策略更麻烦,但我认为它仍然是一个更好的解决方案。但现在,我将在以后进行克隆和重构。我尝试过这个,但无法让它工作。我有另外一份工作。
object(Doctrine\Common\Collections\ArrayCollection)[237]
private '_elements' =>
array
0 => string 'testing-slug' (length=12)
1 => string 'another-test' (length=12)
2 => string 'is-it-working' (length=13)
object(Doctrine\Common\Collections\ArrayCollection)[237]
private '_elements' =>
array
0 => string 'testing-slug' (length=12)
1 => string 'another-test' (length=12)
public function addSlug($slug)
{
$this->slugs = clone $this->slugs;
$this->slugs->add($slug);
}