我正在研究 symfony2 并收到此错误:
没有为类“Acme\BlogBundle\Entity\Posts”找到名为“Acme.BlogBundle.Entity.Posts.php”的映射文件。500 内部服务器错误 - MappingException
我生成实体 php应用程序/控制台原则:生成:实体
实体名称:AcmeBlogBundle:Post
格式:php
我放在Acme:BlogBundle:Entity目录中的所有内容。
这是我的带有 getter 和 setter 方法的 Entity Post 类:
<?php
namespace Acme\BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Posts
*/
class Posts
{
/**
* @var integer
*/
private $id;
/**
* @var string
*/
private $title;
/**
* @var string
*/
private $shortText;
/**
* @var string
*/
private $longText;
/**
* @var string
*/
private $author;
/**
* @var \DateTime
*/
private $dateCreated;
/**
* @var \DateTime
*/
private $dateModified;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* @param string $title
* @return Posts
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set shortText
*
* @param string $shortText
* @return Posts
*/
public function setShortText($shortText)
{
$this->shortText = $shortText;
return $this;
}
/**
* Get shortText
*
* @return string
*/
public function getShortText()
{
return $this->shortText;
}
/**
* Set longText
*
* @param string $longText
* @return Posts
*/
public function setLongText($longText)
{
$this->longText = $longText;
return $this;
}
/**
* Get longText
*
* @return string
*/
public function getLongText()
{
return $this->longText;
}
/**
* Set author
*
* @param string $author
* @return Posts
*/
public function setAuthor($author)
{
$this->author = $author;
return $this;
}
/**
* Get author
*
* @return string
*/
public function getAuthor()
{
return $this->author;
}
/**
* Set dateCreated
*
* @param \DateTime $dateCreated
* @return Posts
*/
public function setDateCreated($dateCreated)
{
$this->dateCreated = $dateCreated;
return $this;
}
/**
* Get dateCreated
*
* @return \DateTime
*/
public function getDateCreated()
{
return $this->dateCreated;
}
/**
* Set dateModified
*
* @param \DateTime $dateModified
* @return Posts
*/
public function setDateModified($dateModified)
{
$this->dateModified = $dateModified;
return $this;
}
/**
* Get dateModified
*
* @return \DateTime
*/
public function getDateModified()
{
return $this->dateModified;
}
}
在我的控制器中,我首先在定义控制器的命名空间之后设置 Post Entity。
use Acme\BlogBundle\Entity\Posts;
之后我创建方法
public function AddAction()
{
// $post = Acme\BlogBundle\Entity\Posts()
$posts = new Posts();
$posts->setTitle('Test Title');
$em = $this->getDoctrine()->getManager();
$em->persist($posts);
$em->flush();
}
这是堆栈跟踪输出
[1] Doctrine\Common\Persistence\Mapping\MappingException:找不到名为“Acme.BlogBundle.Entity.Posts.php”的类“Acme\BlogBundle\Entity\Posts”的映射文件。在 /var/www/html/Symfony/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php 第 74 行中的不适用
在 /var/www/html/Symfony/vendor/doctrine/ 中的 Doctrine\Common\Persistence\Mapping\MappingException::mappingFileNotFound('Acme\BlogBundle\Entity\Posts', 'Acme.BlogBundle.Entity.Posts.php') common/lib/Doctrine/Common/Persistence/Mapping/Driver/DefaultFileLocator.php 第 117 行
在 /var/www/html/Symfony/vendor/doctrine/common/lib/Doctrine/Common/Persistence/ 中的 Doctrine\Common\Persistence\Mapping\Driver\DefaultFileLocator->findMappingFile('Acme\BlogBundle\Entity\Posts')映射/驱动程序/PHPDriver.php 第 59 行
在 Doctrine\Common\Persistence\Mapping\Driver\PHPDriver->loadMetadataForClass('Acme\BlogBundle\Entity\Posts', object(ClassMetadata)) 在 /var/www/html/Symfony/vendor/doctrine/common/lib/Doctrine /Common/Persistence/Mapping/Driver/MappingDriverChain.php 第 104 行
在 /var/www/html/Symfony/vendor/doctrine/orm/lib/Doctrine 中的 Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain->loadMetadataForClass('Acme\BlogBundle\Entity\Posts', object(ClassMetadata)) /ORM/Mapping/ClassMetadataFactory.php 第 113 行
at Doctrine\ORM\Mapping\ClassMetadataFactory->doLoadMetadata(object(ClassMetadata),
/var/www/html/Symfony/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php 第 302 行中的 null、false、array())
at Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory->loadMetadata('Acme\BlogBundle\Entity\Posts') in /var/www/html/Symfony/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php
205行
在 /var/www/html/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php 行中的 Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory->getMetadataFor('Acme\BlogBundle\Entity\Posts') 268
在 Doctrine\ORM\EntityManager->getClassMetadata('Acme\BlogBundle\Entity\Posts') 在 /var/www/html/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php 行 1580
在 Doctrine\ORM\UnitOfWork->doPersist(object(Posts), array('000000000d824498000000009cdc8511' => object(Posts))) 在 /var/www/html/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/ UnitOfWork.php 第 1555 行
在 /var/www/html/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php 第 565 行中的 Doctrine\ORM\UnitOfWork->persist(object(Posts))
在 /var/www/html/Symfony/src/Acme/BlogBundle/Controller/DefaultController.php 第 23 行中的 Doctrine\ORM\EntityManager->persist(object(Posts))
在 Acme\BlogBundle\Controller\DefaultController->indexAction() 中
在 /var/www/html/Symfony/app/bootstrap.php.cache 第 2815 行中的 call_user_func_array(array(object(DefaultController), 'indexAction'), array())
在 Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), '1') 在 /var/www/html/Symfony/app/bootstrap.php.cache 第 2789 行
在 Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), '1', true) 在 /var/www/html/Symfony/app/bootstrap.php.cache 行 2918
在 Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel->handle(object(Request), '1', true) 在 /var/www/html/Symfony/app/bootstrap.php.cache 行 2220
在 Symfony\Component\HttpKernel\Kernel->handle(object(Request)) 在 /var/www/html/Symfony/web/app_dev.php 第 28 行
更新:
新实体测试:
<?php
// src/Acme/BlogBundle/Entity/Test.php
namespace Acme\BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table()
* @ORM\Entity
*/
class Test
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* @var string
*
* @ORM\Column(name="short_text", type="string", length=255)
*/
private $shortText;
/**
* @var string
*
* @ORM\Column(name="long_text", type="text")
*/
private $longText;
/**
* @var \DateTime
*
* @ORM\Column(name="date_created", type="datetime")
*/
private $dateCreated;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* @param string $title
* @return Test
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set shortText
*
* @param string $shortText
* @return Test
*/
public function setShortText($shortText)
{
$this->shortText = $shortText;
return $this;
}
/**
* Get shortText
*
* @return string
*/
public function getShortText()
{
return $this->shortText;
}
/**
* Set longText
*
* @param string $longText
* @return Test
*/
public function setLongText($longText)
{
$this->longText = $longText;
return $this;
}
/**
* Get longText
*
* @return string
*/
public function getLongText()
{
return $this->longText;
}
/**
* Set dateCreated
*
* @param \DateTime $dateCreated
* @return Test
*/
public function setDateCreated($dateCreated)
{
$this->dateCreated = $dateCreated;
return $this;
}
/**
* Get dateCreated
*
* @return \DateTime
*/
public function getDateCreated()
{
return $this->dateCreated;
}
}
再次出现一些错误
$ php 应用程序/控制台原则:生成:实体 AcmeBlogBundle
为包“AcmeBlogBundle”生成实体
[RuntimeException]
捆绑“AcmeBlogBundle”不包含任何映射实体。
学说:生成:实体 [--path="..."] [--no-backup] 名称