gpt4 book ai didi

Symfony2 找不到映射文件

转载 作者:行者123 更新时间:2023-12-02 00:05:18 25 4
gpt4 key购买 nike

我正在使用 symfony2 并收到此错误:

No mapping file found named 'Acme.BlogBundle.Entity.Posts.php' for class 'Acme\BlogBundle\Entity\Posts'. 500 Internal Server Error - MappingException

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

实体名称:AcmeBlogBu​​ndle:Post

格式: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;
}
}

在我的 Controller 中,我首先在定义 Controller 的命名空间后设置 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: No mapping file found named 'Acme.BlogBundle.Entity.Posts.php' for class 'Acme\BlogBundle\Entity\Posts'. at n/a in /var/www/html/Symfony/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php line 74

at Doctrine\Common\Persistence\Mapping\MappingException::mappingFileNotFound('Acme\BlogBundle\Entity\Posts', 'Acme.BlogBundle.Entity.Posts.php') in /var/www/html/Symfony/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/Driver/DefaultFileLocator.php line 117

at Doctrine\Common\Persistence\Mapping\Driver\DefaultFileLocator->findMappingFile('Acme\BlogBundle\Entity\Posts') in /var/www/html/Symfony/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/Driver/PHPDriver.php line 59

at Doctrine\Common\Persistence\Mapping\Driver\PHPDriver->loadMetadataForClass('Acme\BlogBundle\Entity\Posts', object(ClassMetadata)) in /var/www/html/Symfony/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/Driver/MappingDriverChain.php line 104

at Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain->loadMetadataForClass('Acme\BlogBundle\Entity\Posts', object(ClassMetadata)) in /var/www/html/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php line 113

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

null, false, array()) in /var/www/html/Symfony/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php line 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

line 205

at Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory->getMetadataFor('Acme\BlogBundle\Entity\Posts') in /var/www/html/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php line 268

at Doctrine\ORM\EntityManager->getClassMetadata('Acme\BlogBundle\Entity\Posts') in /var/www/html/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php line 1580

at Doctrine\ORM\UnitOfWork->doPersist(object(Posts), array('000000000d824498000000009cdc8511' => object(Posts))) in /var/www/html/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php line 1555

at Doctrine\ORM\UnitOfWork->persist(object(Posts)) in /var/www/html/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php line 565

at Doctrine\ORM\EntityManager->persist(object(Posts)) in /var/www/html/Symfony/src/Acme/BlogBundle/Controller/DefaultController.php line 23

at Acme\BlogBundle\Controller\DefaultController->indexAction() in line

at call_user_func_array(array(object(DefaultController), 'indexAction'), array()) in /var/www/html/Symfony/app/bootstrap.php.cache line 2815

at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), '1') in /var/www/html/Symfony/app/bootstrap.php.cache line 2789

at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), '1', true) in /var/www/html/Symfony/app/bootstrap.php.cache line 2918

at Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel->handle(object(Request), '1', true) in /var/www/html/Symfony/app/bootstrap.php.cache line 2220

at Symfony\Component\HttpKernel\Kernel->handle(object(Request)) in /var/www/html/Symfony/web/app_dev.php line 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

Generating entities for bundle "AcmeBlogBundle"

                                                                [RuntimeException]                                               

Bundle "AcmeBlogBundle" does not contain any mapped entities.

doctrine:generate:entities [--path="..."] [--no-backup] name

最佳答案

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

像这样:

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

关于Symfony2 找不到映射文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18792426/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com