【问题标题】:Class Doctrine\Common\Collections\ArrayCollection is not a valid entity or mapped super class类 Doctrine\Common\Collections\ArrayCollection 不是有效的实体或映射的超类
【发布时间】:2012-09-08 11:39:28
【问题描述】:

我有三个实体:

FeatureValue.php

<?php

namespace Webmuch\ProductBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class FeatureValue
{
/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\Column(type="string", length="100")
 */
protected $name;

/** 
 * @ORM\OneToMany(targetEntity="FeatureType", mappedBy="name")
 */
private $featuretype;



public function __construct()
{
    $this->featuretype = new \Doctrine\Common\Collections\ArrayCollection();
}

FeatureType.php

<?php

namespace Webmuch\ProductBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Webmuch\ProductBundle\Entity\FeatureValue;

/**
 * @ORM\Entity
 */
class FeatureType
{
/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\Column(type="string", length=255, nullable=true)
 */   
protected $title;

/** 
 * @ORM\ManyToOne(targetEntity="FeatureValue", inversedBy="featuretype",cascade={"persist"})
 * @ORM\JoinColumn(name="feature_value_id", referencedColumnName="id")
 */    
protected $name;

 public function __construct() 
{
    $this->name = new \Doctrine\Common\Collections\ArrayCollection();
}

产品.php

<?php
namespace Webmuch\ProductBundle\Entity;

use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection as ArrayCollection;

/**
 * @ORM\Entity
 * @ORM\Table()
 * @ORM\HasLifecycleCallbacks
 */
class Product 
{
/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\ManyToMany(targetEntity="Store")
 */
protected $store;

/**
 * @ORM\ManyToMany(targetEntity="Webmuch\CategoryBundle\Entity\Category")
 */
protected $category;

/**
 * @Gedmo\Sluggable
 * @ORM\Column(type="string", length=255)
 */
protected $title;

/**
 * @Gedmo\Slug(updatable=false, unique=true)
 * @ORM\Column(type="string", length=255)
 */
protected $slug;

/**
 * @ORM\Column(type="integer")
 */
protected $quantity;

/**
 * @ORM\Column(type="boolean")
 */
protected $active;

/**
 * @ORM\Column(type="text", length="4000", nullable="true")
 */
protected $preview;

/**
 * @ORM\ManyToMany(targetEntity="FeatureType")
 * @ORM\JoinColumn(name="feature_type_id", referencedColumnName="id")
 */
protected $features;

public function __toString()
{
    return $this->getTitle();
}

}

我的问题是,当我在 FeatureType 中添加 FeatureValue 时,会显示错误类“Doctrine\Common\Collections\ArrayCollection 不是有效的实体或映射的超类。”

在我的项目中,我希望 FeatureType 以及我的 Product 实体中的 FeatureValue。

假设在 FeatureType 中有两个属性 Size 和 Colour。现在在 FeatureType Size 中给出了三个 FeatueValue Small、Medium、Large,还假设在 FeatureType Colour 中给出了三个 FeatureValue Red、Green、Yello。 现在我想在我的产品实体中同时显示 FeatureType 及其 FeatureValue。

所以任何人都告诉我如何做到这一点。我尝试了很多但没有成功。

【问题讨论】:

    标签: php doctrine-orm


    【解决方案1】:

    Webmuch\ProductBundle\Entity\FeatureType::__construct 中,您将ArrayCollection 分配给$this-&gt;name,这是错误的,因为这是ToOne 而不是ToMany。

    只需删除此行。

    【讨论】:

      猜你喜欢
      • 2016-09-09
      • 1970-01-01
      • 2016-01-20
      • 1970-01-01
      • 1970-01-01
      • 2013-02-12
      • 1970-01-01
      • 2013-02-05
      • 1970-01-01
      相关资源
      最近更新 更多