Class Doctrine\Common\Collections\ArrayCollection

2019-07-17 16:27发布

I have Three entities:

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();
}

Product.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();
}

}

My problem is that when i add FeatureValue in FeatureType then show me error Class "Doctrine\Common\Collections\ArrayCollection is not a valid entity or mapped super class."

In my project i want FeatureType along with FeatureValue in my Product entity.

suppose in FeatureType two property Size and Colour.Now in FeatureType Size- Three FeatueValue Small,Medium,Large have been given and also suppose in FeatureType Colour- Three FeatureValue Red,Green,Yello have been given. Now i want to show both the FeatureType along with their FeatureValue in my Product Entity.

So any one tell me how it can be done.I have tried a lot of but not succeed.

1条回答
▲ chillily
2楼-- · 2019-07-17 16:48

In Webmuch\ProductBundle\Entity\FeatureType::__construct you assign an ArrayCollection to $this->name, this is wrong, as this is a ToOne and not a ToMany.

Just remove this line.

查看更多
登录 后发表回答