我有三个实体:
FeatureValue.php
<?php
namespace WebmuchProductBundleEntity;
use DoctrineORMMapping as ORM;
/**
* @ORMEntity
*/
class FeatureValue
{
/**
* @ORMId
* @ORMColumn(type="integer")
* @ORMGeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORMColumn(type="string", length="100")
*/
protected $name;
/**
* @ORMOneToMany(targetEntity="FeatureType", mappedBy="name")
*/
private $featuretype;
public function __construct()
{
$this->featuretype = new DoctrineCommonCollectionsArrayCollection();
}
FeatureType.php
<?php
namespace WebmuchProductBundleEntity;
use DoctrineORMMapping as ORM;
use WebmuchProductBundleEntityFeatureValue;
/**
* @ORMEntity
*/
class FeatureType
{
/**
* @ORMId
* @ORMColumn(type="integer")
* @ORMGeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORMColumn(type="string", length=255, nullable=true)
*/
protected $title;
/**
* @ORMManyToOne(targetEntity="FeatureValue", inversedBy="featuretype",cascade={"persist"})
* @ORMJoinColumn(name="feature_value_id", referencedColumnName="id")
*/
protected $name;
public function __construct()
{
$this->name = new DoctrineCommonCollectionsArrayCollection();
}
产品.php
<?php
namespace WebmuchProductBundleEntity;
use GedmoMappingAnnotation as Gedmo;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection as ArrayCollection;
/**
* @ORMEntity
* @ORMTable()
* @ORMHasLifecycleCallbacks
*/
class Product
{
/**
* @ORMId
* @ORMColumn(type="integer")
* @ORMGeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORMManyToMany(targetEntity="Store")
*/
protected $store;
/**
* @ORMManyToMany(targetEntity="WebmuchCategoryBundleEntityCategory")
*/
protected $category;
/**
* @GedmoSluggable
* @ORMColumn(type="string", length=255)
*/
protected $title;
/**
* @GedmoSlug(updatable=false, unique=true)
* @ORMColumn(type="string", length=255)
*/
protected $slug;
/**
* @ORMColumn(type="integer")
*/
protected $quantity;
/**
* @ORMColumn(type="boolean")
*/
protected $active;
/**
* @ORMColumn(type="text", length="4000", nullable="true")
*/
protected $preview;
/**
* @ORMManyToMany(targetEntity="FeatureType")
* @ORMJoinColumn(name="feature_type_id", referencedColumnName="id")
*/
protected $features;
public function __toString()
{
return $this->getTitle();
}
}
我的问题是,当我在FeatureType中添加FeatureValue时,会向我显示错误Class"Doctrine\Common\Collections\ArrayCollection不是有效的实体或映射的超类。"
在我的项目中,我希望FeatureType和FeatureValue一起出现在我的产品实体中。
假设在FeatureType中有两个属性Size和Colour。现在在FeatureType Size中给出了三个FeatureValue Small、Medium、Large,还假设在FeatureTypeColour中给出了红、绿、黄三个FeatureValue。现在我想在我的产品实体中显示FeatureType和它们的FeatureValue。
所以任何人都可以告诉我怎么做。我尝试了很多,但没有成功。
在WebmuchProductBundleEntityFeatureType::__construct
中,将ArrayCollection
分配给$this->name
,这是错误的,因为这是ToOne而不是ToMany。
只要去掉这条线。