[英]How to extend entity with internal relation?
I hope you will be able to help because I have been searching for a few days now. 希望您能有所帮助,因为我已经搜索了几天。 I have a class called "Attributes". 我有一个叫做“属性”的类。 In it, there is a parentId
which reference a other entry of Attributes
. 其中有一个parentId
,它引用Attributes
的其他条目。
I want to extend the entity "Attributes" as "Products" with a few extra fields. 我想用一些额外的字段将实体“属性”扩展为“产品”。 All the fields are being extended except for the parentId
relation. 除parentId
关系外,所有字段均得到扩展。
When I add parent to Products
with getter and setter I get a error: 当我使用getter和setter将parent添加到Products
,出现错误:
`"Compile Error: Declaration of Products::setParent(?Attributes $parent): Products must be compatible with Attributes::setParent(?Attributes $parent)
: Attributes"`
I have tried a standalone Entity Products
with all the fields but the relationship to Attributes
causes database relation issues. 我已经尝试了具有所有字段的独立实体Products
,但是与“ Attributes
的关系导致数据库关系问题。 And it is an extension of Attributes
. 它是Attributes
的扩展。
Tried different kinds of getters and setters and leaving it out to be extended from the parent. 尝试了各种类型的吸气剂和吸气剂,并将其从父级扩展出去。
Have been searching the web for a answer but have not seen inheritance with a internal relationship. 一直在网上寻找答案,但没有看到具有内部关系的继承。
Attributes class: 属性类:
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
*/
class Attributes
{
/**
* @ORM\Id()
* @ORM\Column(type="string", length=255)
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $val;
/**
* @ORM\Column(type="string", length=255)
*/
private $category;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Attributes")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
*/
private $parent;
}
Products class: 产品类别:
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
*/
class Products extends Attributes
{
/**
* @ORM\Column(type="string", length=255)
*/
private $newField1;
/**
* @ORM\Column(type="string", length=255)
*/
private $newField2;
}
I would like to know why the internal relationship "Parent" isn't extended and I would love to know how to get the parentId in the extended class. 我想知道为什么内部关系“ Parent”没有扩展,并且我想知道如何在扩展类中获取parentId。
Your parent setters seems to be like this: 您的父母二传手似乎是这样的:
/** Product **/
public function setParent(?Attributes $parent): Products
{
$this->parent = $parent;
return $this;
}
/** Attribute **/
public function setParent(?Attributes $parent)
{
$this->parent = $parent;
return $this;
}
You can fix it simply by replacing :Products
by :Attributes
at the end of setParent line in product class. 您可以通过在产品类的setParent行末尾用:Attributes
替换:Products
来解决此问题。
But you should use an interface 但是你应该使用一个接口
interface AttributeInterface
{
public function setParent(?AttributeInterface $parent): AttributeInterface
}
and each of your entity should implement it: 并且您的每个实体都应实施:
Attribute: 属性:
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
*/
class Attributes implements AttributesInterface
{
/*****/
public function setParent(?AttributeInterface $parent): AttributeInterface
{
$this->parent = $parent;
return $this;
}
}
Product: 产品:
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
*/
class Products extends Attributes implements AttributesInterface
{
/*****/
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.