Doctrine 索引通过实体字段的注解
Doctrine indexBy annotation by a entity field
我有下一个用不同语言定义旅行目的地的结构。
在 Destinations
中,检索 DestinationsTranslate
(getDestinationsTranslations) 集合的方法我需要 id
[=16] 字段的索引=] 能够调用的实体,例如 $destinationEntity->getDestinationsTranslations()->get('en')
以检索英文目的地的翻译;但如果我在 indexBy
注释中设置 idLanguage
,则不起作用。我试试:
- indexBy="idLanguage"
- indexBy="idLanguage.id"
- 将 __toString 设置为语言实体。
这一切都不起作用。如果我尝试按 translation
字段进行索引,则有效。
不能按实体索引吗?
Destination
的实体是:
/**
* Destinations
*
* @ORM\Table(name="destinations", uniqueConstraints={@ORM\UniqueConstraint(name="id_UNIQUE", columns={"id"})}, indexes={@ORM\Index(name="fk_destinations_time_zones1_idx", columns={"id_time_zone"})})
* @ORM\Entity
*/
class Destinations
{
/**
* @var string
*
* @ORM\Column(name="id", type="string", length=3, nullable=false, options={"fixed"=true,"comment"="Three letter IATA code."})
* @ORM\Id
*/
private string $id;
/**
* @var bool
*
* @ORM\Column(name="mac", type="boolean", nullable=false, options={"comment"="Indicate if it is a metropolitan airline destination."})
*/
private $mac = false;
/**
* @var \TimeZones
*
* @ORM\ManyToOne(targetEntity="TimeZones")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id_time_zone", referencedColumnName="id")
* })
*/
private TimeZones $idTimeZone;
/**
* @ORM\OneToMany(targetEntity=DestinationsTranslation::class, mappedBy="idDestination", indexBy="idLanguage.id")
*/
private $destinationsTranslations;
/**
* Constructor
*/
public function __construct(string $iata, TimeZones $tz, bool $mac = false)
{
$this->id = $iata;
$this->idTimeZone = $tz;
$this->mac = $mac;
$this->destinationsTranslations = new ArrayCollection();
}
}
Languages
的实体是:
/**
* Languages
*
* @ORM\Table(name="languages", uniqueConstraints={@ORM\UniqueConstraint(name="id_UNIQUE", columns={"id"})})
* @ORM\Entity
*/
class Languages
{
/**
* @var string
*
* @ORM\Column(name="id", type="string", length=2, nullable=false, options={"fixed"=true,"comment"="Two letter code from ISO 639-1."})
* @ORM\Id
*/
private string $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=64, nullable=false, options={"comment"="ISO 639-1 language name."})
*/
private string $name;
/**
* @ORM\OneToMany(targetEntity=DestinationsTranslation::class, mappedBy="idLanguage")
*/
private $destinationsTranslations;
/**
* Constructor
*/
public function __construct(string $code, string $name)
{
$this->id = $code;
$this->name = $name;
$this->destinationsTranslations = new ArrayCollection();
}
}
DestinationsTranslation
的实体是:
/**
* @ORM\Entity(repositoryClass=DestinationsTranslationRepository::class)
*/
class DestinationsTranslation
{
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity=Languages::class, inversedBy="destinationsTranslations")
* @ORM\JoinColumn(nullable=false)
*/
private Languages $idLanguage;
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity=Destinations::class, inversedBy="destinationsTranslations")
* @ORM\JoinColumn(nullable=false)
*/
private Destinations $idDestination;
/**
* @ORM\Column(type="string", length=64)
*/
private string $translation;
public function __construct(Languages $lang, Destinations $dest, string $translation)
{
$this->idLanguage = $lang;
$this->idDestination = $dest;
$this->translation = $translation;
}
}
问题出在命名关系的方式上,我重新生成了带有关系字段的实体,没有在它们的名称中设置 id
,学说命令自动生成它并可以执行 indexBy。
注释是这样的:
/**
* @ORM\OneToMany(targetEntity=DestinationsTranslation::class, mappedBy="destination", indexBy="language_id")
*/
private Collection $destinationsTranslations;
我有下一个用不同语言定义旅行目的地的结构。
在 Destinations
中,检索 DestinationsTranslate
(getDestinationsTranslations) 集合的方法我需要 id
[=16] 字段的索引=] 能够调用的实体,例如 $destinationEntity->getDestinationsTranslations()->get('en')
以检索英文目的地的翻译;但如果我在 indexBy
注释中设置 idLanguage
,则不起作用。我试试:
- indexBy="idLanguage"
- indexBy="idLanguage.id"
- 将 __toString 设置为语言实体。
这一切都不起作用。如果我尝试按 translation
字段进行索引,则有效。
不能按实体索引吗?
Destination
的实体是:
/**
* Destinations
*
* @ORM\Table(name="destinations", uniqueConstraints={@ORM\UniqueConstraint(name="id_UNIQUE", columns={"id"})}, indexes={@ORM\Index(name="fk_destinations_time_zones1_idx", columns={"id_time_zone"})})
* @ORM\Entity
*/
class Destinations
{
/**
* @var string
*
* @ORM\Column(name="id", type="string", length=3, nullable=false, options={"fixed"=true,"comment"="Three letter IATA code."})
* @ORM\Id
*/
private string $id;
/**
* @var bool
*
* @ORM\Column(name="mac", type="boolean", nullable=false, options={"comment"="Indicate if it is a metropolitan airline destination."})
*/
private $mac = false;
/**
* @var \TimeZones
*
* @ORM\ManyToOne(targetEntity="TimeZones")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id_time_zone", referencedColumnName="id")
* })
*/
private TimeZones $idTimeZone;
/**
* @ORM\OneToMany(targetEntity=DestinationsTranslation::class, mappedBy="idDestination", indexBy="idLanguage.id")
*/
private $destinationsTranslations;
/**
* Constructor
*/
public function __construct(string $iata, TimeZones $tz, bool $mac = false)
{
$this->id = $iata;
$this->idTimeZone = $tz;
$this->mac = $mac;
$this->destinationsTranslations = new ArrayCollection();
}
}
Languages
的实体是:
/**
* Languages
*
* @ORM\Table(name="languages", uniqueConstraints={@ORM\UniqueConstraint(name="id_UNIQUE", columns={"id"})})
* @ORM\Entity
*/
class Languages
{
/**
* @var string
*
* @ORM\Column(name="id", type="string", length=2, nullable=false, options={"fixed"=true,"comment"="Two letter code from ISO 639-1."})
* @ORM\Id
*/
private string $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=64, nullable=false, options={"comment"="ISO 639-1 language name."})
*/
private string $name;
/**
* @ORM\OneToMany(targetEntity=DestinationsTranslation::class, mappedBy="idLanguage")
*/
private $destinationsTranslations;
/**
* Constructor
*/
public function __construct(string $code, string $name)
{
$this->id = $code;
$this->name = $name;
$this->destinationsTranslations = new ArrayCollection();
}
}
DestinationsTranslation
的实体是:
/**
* @ORM\Entity(repositoryClass=DestinationsTranslationRepository::class)
*/
class DestinationsTranslation
{
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity=Languages::class, inversedBy="destinationsTranslations")
* @ORM\JoinColumn(nullable=false)
*/
private Languages $idLanguage;
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity=Destinations::class, inversedBy="destinationsTranslations")
* @ORM\JoinColumn(nullable=false)
*/
private Destinations $idDestination;
/**
* @ORM\Column(type="string", length=64)
*/
private string $translation;
public function __construct(Languages $lang, Destinations $dest, string $translation)
{
$this->idLanguage = $lang;
$this->idDestination = $dest;
$this->translation = $translation;
}
}
问题出在命名关系的方式上,我重新生成了带有关系字段的实体,没有在它们的名称中设置 id
,学说命令自动生成它并可以执行 indexBy。
注释是这样的:
/**
* @ORM\OneToMany(targetEntity=DestinationsTranslation::class, mappedBy="destination", indexBy="language_id")
*/
private Collection $destinationsTranslations;