【发布时间】:2014-07-28 14:52:48
【问题描述】:
我为两个相关实体创建了一个固定装置。尝试使用
加载固定装置时php app/console doctrine:fixtures:load
我总是收到这个错误:
Fatal error: Class '[MyNamespace]\Entity\ArrayCollection' not found in [...]
它在我的实体中引用了这段代码:
public function __construct()
{
$this->products = new ArrayCollection();
}
当我取消注释函数中的行时,所有数据库记录及其关系都会在我的数据库 (MySQL) 中正常创建。
这里出了什么问题,可以做些什么来解决这个问题?
TIA 马特
附加信息:
实体“提供者”<?php
namespace VS\Journeyro\WebBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Provider
*/
class Provider
{
/**
* @var integer
*/
private $id;
/**
* @var string
*/
private $name;
/**
* @var string
*/
private $slug;
/**
* @var string
*/
private $url;
/**
* @var string
*/
private $email;
/**
* @var string
*/
private $phone;
/**
* @var string
*/
private $image;
/**
* @var string
*/
private $description;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Provider
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set slug
*
* @param string $slug
* @return Provider
*/
public function setSlug($slug)
{
$this->slug = $slug;
return $this;
}
/**
* Get slug
*
* @return string
*/
public function getSlug()
{
return $this->slug;
}
/**
* Set url
*
* @param string $url
* @return Provider
*/
public function setUrl($url)
{
$this->url = $url;
return $this;
}
/**
* Get url
*
* @return string
*/
public function getUrl()
{
return $this->url;
}
/**
* Set email
*
* @param string $email
* @return Provider
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set phone
*
* @param string $phone
* @return Provider
*/
public function setPhone($phone)
{
$this->phone = $phone;
return $this;
}
/**
* Get phone
*
* @return string
*/
public function getPhone()
{
return $this->phone;
}
/**
* Set image
*
* @param string $image
* @return Provider
*/
public function setImage($image)
{
$this->image = $image;
return $this;
}
/**
* Get image
*
* @return string
*/
public function getImage()
{
return $this->image;
}
/**
* Set description
*
* @param string $description
* @return Provider
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
public function __toString()
{
return $this->getName();
}
/**
* Add collection of related products
*/
protected $products;
public function __construct()
{
$this->products = new ArrayCollection();
}
/**
* Add products
*
* @param \VS\Journeyro\WebBundle\Entity\Product $products
* @return Provider
*/
public function addProduct(\VS\Journeyro\WebBundle\Entity\Product $products)
{
$this->products[] = $products;
return $this;
}
/**
* Remove products
*
* @param \VS\Journeyro\WebBundle\Entity\Product $products
*/
public function removeProduct(\VS\Journeyro\WebBundle\Entity\Product $products)
{
$this->products->removeElement($products);
}
/**
* Get products
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getProducts()
{
return $this->products;
}
}
为实体提供者编写固定装置的类
<?php
namespace VS\Journeyro\WebBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\Collections\ArrayCollection;
use VS\Journeyro\WebBundle\Entity\Provider;
class LoadProviderData extends AbstractFixture implements OrderedFixtureInterface
{
/**
* {@inheritDoc}
*/
public function load(ObjectManager $manager)
{
$providers = array();
// defining fixtures in $providers here
foreach ($providers as $record) {
$provider= new Provider();
$provider->setName($record['name']);
$provider->setSlug($record['slug']);
$provider->setUrl($record['url']);
$provider->setEmail($record['email']);
$provider->setImage($record['image']);
$provider->setDescription($record['description']);
$manager->persist($provider);
$manager->flush();
$this->addReference('provider-'.$record['slug'], $provider);
}
}
/**
* {@inheritDoc}
*/
public function getOrder()
{
return 1;
}
}
【问题讨论】:
-
你在使用use语句吗?像这样:“使用 Doctrine\Common\Collections\ArrayCollection;”
-
我不是。我只是按照您建议的方式添加了它,但问题仍然存在。但是,我的 IDE 似乎无法识别该路径。我在这个位置找到了似乎相关的电话:
vendor\doctrine\collections\lib\Doctrine\Common\Collections\ArrayCollection.php -
你能提供你课堂上的所有代码吗?
-
将实体和编写相关装置的类添加到我的原始帖子中。这是你要求的吗?
-
您似乎没有将
use Doctrine\Common\Collections\ArrayCollection;语句添加到正确的位置:这应该在Provider类的顶部,因为您在构造函数中引用了类名.尝试将其添加到那里,而不是在您的灯具中。
标签: symfony doctrine-orm fixtures