Symfony2找不到映射文件

时间:2013-09-13 17:56:08

标签: symfony

正在研究symfony2并且发现了这个错误:

  

找不到名为' Acme.BlogBu​​ndle.Entity.Posts.php'的映射文件。对于   class' Acme \ BlogBu​​ndle \ Entity \ Posts'。 500内部服务器错误 -   MappingException

我生成实体php app / console doctrine:generate:entity

实体名称: AcmeBlogBu​​ndle:发布

格式: php

我放入 Acme:BlogBu​​ndle: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;
    }
}

在我的控制器中,我首先在definig控制器命名空间后设置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:没有映射   找到名为&#39; Acme.BlogBu​​ndle.Entity.Posts.php&#39;上课   &#39; Acme公司\ BlogBu​​ndle \实体\帖子&#39 ;.   在n / a   在/var/www/html/Symfony/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php   第74行

     

在Doctrine \ Common \ Persistence \ Mapping \ MappingException :: mappingFileNotFound(&#39; Acme \ BlogBu​​ndle \ Entity \ Posts&#39;,   &#39; Acme.BlogBu​​ndle.Entity.Posts.php&#39;)   在/var/www/html/Symfony/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/Driver/DefaultFileLocator.php   第117行

     

在Doctrine \ Common \ Persistence \ Mapping \ Driver \ DefaultFileLocator-&gt; findMappingFile(&#39; Acme \ BlogBu​​ndle \ Entity \ Posts&#39;)   在/var/www/html/Symfony/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/Driver/PHPDriver.php   第59行

     

在Doctrine \ Common \ Persistence \ Mapping \ Driver \ PHPDriver-&gt; loadMetadataForClass(&#39; Acme \ BlogBu​​ndle \ Entity \ Posts&#39;,   对象(ClassMetadata))   in /var/www/html/Symfony/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/Driver/MappingDriverChain.php   第104行

     

在Doctrine \ Common \ Persistence \ Mapping \ Driver \ MappingDriverChain-&gt; loadMetadataForClass(&#39; Acme \ BlogBu​​ndle \ Entity \ Posts&#39;,   对象(ClassMetadata))   在/var/www/html/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php   第113行

at Doctrine\ORM\Mapping\ClassMetadataFactory->doLoadMetadata(object(ClassMetadata),
     

null,false,array())           在/var/www/html/Symfony/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php   第302行

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行

     

在Doctrine \ Common \ Persistence \ Mapping \ AbstractClassMetadataFactory-&gt; getMetadataFor(&#39; Acme \ BlogBu​​ndle \ Entity \ Posts&#39;)   在/var/www/html/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php   第268行

     

在Doctrine \ ORM \ EntityManager-&gt; getClassMetadata(&#39; Acme \ BlogBu​​ndle \ Entity \ Posts&#39;)   在/var/www/html/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php   第1580行

     

在Doctrine \ ORM \ UnitOfWork-&gt; doPersist(对象(帖子),数组(&#39; 000000000d824498000000009cdc8511&#39; =&gt;对象(帖子)))   在/var/www/html/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php   第1555行

     

在Doctrine \ ORM \ UnitOfWork-&gt; persist(object(Posts))   在/var/www/html/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php   第565行

     

在Doctrine \ ORM \ EntityManager-&gt; persist(对象(帖子))           在/var/www/html/Symfony/src/Acme/BlogBu​​ndle/Controller/DefaultController.php中   第23行

     

在Acme \ BlogBu​​ndle \ Controller \ DefaultController-&gt; indexAction()   在行

     

at call_user_func_array(array(object(DefaultController),&#39; indexAction&#39;),array())   在/var/www/html/Symfony/app/bootstrap.php.cache第2815行

     

在Symfony \ Component \ HttpKernel \ HttpKernel-&gt; handleRaw(object(Request),   &#39; 1&#39;)   在/var/www/html/Symfony/app/bootstrap.php.cache第2789行

     

在Symfony \ Component \ HttpKernel \ HttpKernel-&gt;句柄(对象(请求),&#39; 1&#39;,   真正)   在/var/www/html/Symfony/app/bootstrap.php.cache第2918行

     

在Symfony \ Component \ HttpKernel \ DependencyInjection \ ContainerAwareHttpKernel-&gt;句柄(对象(请求),   &#39; 1&#39;,true)   在/var/www/html/Symfony/app/bootstrap.php.cache第2220行

     

在Symfony \ Component \ HttpKernel \ Kernel-&gt;句柄(对象(请求))   在/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 app / console doctrine:generate:entities AcmeBlogBu​​ndle

  

为bundle&#34; AcmeBlogBu​​ndle&#34;

生成实体
                                                                [RuntimeException]                                               
     

捆绑&#34; AcmeBlogBu​​ndle&#34;不包含任何映射实体。

     

doctrine:generate:entities [--path =&#34; ...&#34;] [ - no-backup] name

4 个答案:

答案 0 :(得分:2)

您应该添加字段的映射信息。 Read more

像这样:

/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

答案 1 :(得分:1)

我错误地通过控制台添加了一个新的实体,其映射类型为'php'而不是我的默认'注释'。立即没有触及生成的类我的网站将无法加载。它为我的用户类抛出了这个MappingException。

我删除了新的Entity类,它会修复它,以及我能想到的其他一切。然后我查看了我的app / Resources / config目录。有一个名为Doctrine的新目录。它包含导致该问题的实体的映射文件。当我删除这个目录和文件时,异常就消失了。

答案 2 :(得分:0)

如果您想使用注释来定义映射,只需在生成实体时选择“注释”选项。

答案 3 :(得分:0)

虽然这个问题很老,但我并不想保守秘密,我是如何解决它的。似乎我在缓存中有一个旧的映射文件引用,因为我将它移动到另一个文件夹。清除缓存为我解决了这个问题。