希望你能帮助我。 我在Linux上使用symfony 3.4。 当我运行我的应用程序时,我有下一个错误:
致命错误:未捕获 Doctrine \ Common \ Persistence \ Mapping \ MappingException:该类 '的appbundle \实体\作业'在配置的链中找不到 名称空间 /var/www/html/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php:37 堆栈跟踪:#0 /var/www/html/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/Driver/MappingDriverChain.php(112): 学说\ COMMON \持久性\制图\ MappingException :: classNotFoundInNamespaces('的appbundle \ Entit ...&#39 ;, 数组)#1 /var/www/html/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php(151): 教义\共同\持久性\映射\驱动\ MappingDriverChain-> loadMetadataForClass('的appbundle \ Entit ...&#39 ;, 对象(Doctrine \ ORM \ Mapping \ ClassMetadata))#2 /var/www/html/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php(332): 教义\ ORM \映射\ ClassMetadataFactory-> doLoadMetadata(对象(学说\ ORM \映射\ ClassMetadata), NULL,false,Array)#3 / var / www / html / vendor / doctrine / orm / lib / Doctrine / 在/var/www/html/vendor/twig/twig/lib/Twig/Cache/Filesystem.php上 第57行
我使用命令
创建了一个名为Job的实体php bin / console doctrine:generate:entity
它创建了实体和存储库没有任何问题。 在此之后我更新了数据库
php bin / console doctrine:schema:update --force
到目前为止一切顺利。现在我开始使用控制器:
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity\Job;
use AppBundle\Entity\User;
use Symfony\Component\HttpFoundation\Response;
use Doctrine\ORM\EntityManagerInterface;
class DefaultController extends Controller
{
/**
* @Route("/", name="homepage")
*/
public function indexAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$job = new Job();
$job->setName('PHP');
$job->setDescription('Description');
$job->setCategory('IT');
$job->setLocation('London');
$em->persist($job);
$em->flush();
// replace this example code with whatever you need
return $this->render('default/index.html.twig', [
'base_dir' => realpath($this->getParameter('kernel.project_dir')).DIRECTORY_SEPARATOR,
]);
}
}
如果我在持久化之前尝试转储$ job,我会得到结果
对象(AppBundle \ Entity \ Job)#242(5){
[" ID":"的appbundle \实体\作业":私人] => NULL
["名称":"的appbundle \实体\作业":私人] => string(3)" PHP"
["描述":"的appbundle \实体\作业":私人] =>串(11) "说明" ["类别":"的appbundle \实体\作业":私人] =>
字符串(2)" IT" ["位置":"的appbundle \实体\作业":私人] =>
字符串(6)"伦敦"工作
一方面,它说它无法找到Job类。另一方面,它可以很好地创建对象。
任何人都知道它发生了什么?
尝试
php bin / console cache:清除 php bin / console cache:warmup
添加工作类
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Job
*
* @ORM\Table(name="job")
* @ORM\Entity(repositoryClass="AppBundle\Repository\JobRepository")
*/
class Job
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="description", type="string", length=255)
*/
private $description;
/**
* @var string
*
* @ORM\Column(name="category", type="string", length=255)
*/
private $category;
/**
* @var string
*
* @ORM\Column(name="location", type="string", length=255)
*/
private $location;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*
* @return Job
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set description
*
* @param string $description
*
* @return Job
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set category
*
* @param string $category
*
* @return Job
*/
public function setCategory($category)
{
$this->category = $category;
return $this;
}
/**
* Get category
*
* @return string
*/
public function getCategory()
{
return $this->category;
}
/**
* Set location
*
* @param string $location
*
* @return Job
*/
public function setLocation($location)
{
$this->location = $location;
return $this;
}
/**
* Get location
*
* @return string
*/
public function getLocation()
{
return $this->location;
}
}