0
我想在我的Symfony2控制器中運行一個簡單的SQL語句(類似select * from table),但它不工作。 Symfony無法找到該類。Symfony2無法在控制器中找到我的doctrine2實體類 - 如何解決?
一些信息:
- 我試過在提供完整的命名空間+類名和公正的類名FROM子句
- 我試過DQL和QueryBuilder的(見下面的代碼選項1和選項。 )
- AppKernel正在加載我的DoctrineBundle。這是已經在那裏,當我用作曲家創建我的項目
- 我試過在settings.yml中auto_mapping真假我的代碼
- 片段低於
錯誤消息:
[Semantical Error] line 0, col 14 near 'Job j ORDER BY': Error: Class 'Job' is not defined.
500 Internal Server Error - QueryException
1 linked Exception:
QueryException »
[2/2] QueryException: [Semantical Error] line 0, col 14 near 'Job j ORDER BY': Error: Class 'Job' is not defined. +
[1/2] QueryException: SELECT u FROM Job j ORDER BY j.name ASC +
settings.yml中
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
# if using pdo_sqlite as your database driver, add the path in parameters.yml
# e.g. database_path: "%kernel.root_dir%/data/data.db3"
# path: "%database_path%"
orm:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: true
#auto_mapping: false
#mappings:
# MyAppMyBundle:
# type: annotation
# dir: Entity/
我控制器
<?php
// src/MyApp/MyBundle/Controller/JobsController.php
namespace MyApp\MyBundle\Controller;
use MyApp\MyBundle\Entity\Job;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class JobsController extends Controller {
public function listAction() {
$em = $this->getDoctrine()->getEntityManager();
//$qb = $em->createQueryBuilder();
//option1
//$qb ->select("j")
// ->from("Job", "j")
// ->orderBy("j.name", "ASC");*/
//return $this->render('MyBundle:Jobs:list.html.twig', array('jobs' => $qb->getQuery()->getResult()));
//option2
$qb = $em->createQuery("SELECT u FROM Job j ORDER BY j.name ASC");
return $this->render('MyBundle:Jobs:list.html.twig', array('jobs' => $qb->getResult()));
}
}
我的實體類
<?php
// src/MyApp/MyBundle/Entity/Job.php
namespace MyApp\MyBundle\Entity;
use Doctrine\ORM\Mapping;
/**
* @Mapping\Entity
* @Mapping\Table(name="jobs")
*/
class Job {
/**
* @Mapping\Column(name="job_id", type="integer")
* @Mapping\Id
* @Mapping\GeneratedValue(strategy="AUTO")
*/
protected $jobId;
/**
* @Mapping\Column(name="name", type="text")
*/
protected $name;
/**
* @Mapping\Column(name="job_desc", type="text")
*/
protected $description;
/**
* @Mapping\Column(name="personal_req", type="text")
*/
protected $requirements;
/**
* Get jobid
*
* @return integer
*/
public function getJobId() {
return $this->applicationId;
}
/**
* Set name
*
* @param \text $name
* @return Job
*/
public function setName($name) {
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return text
*/
public function getName() {
return $this->name;
}
/**
* Set description
*
* @param \text $description
* @return Job
*/
public function setDescription($description) {
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return text
*/
public function getDescription() {
return $this->description;
}
/**
* Set requirements
*
* @param \text $requirements
* @return Job
*/
public function setRequirements($requirements) {
$this->requirements = $requirements;
return $this;
}
/**
* Get requirements
*
* @return text
*/
public function getRequirements() {
return $this->requirements;
}
}
您的表名是'jobs'(@Mapping \ Table(name =「jobs」)),爲什麼您嘗試從'Job'中選擇某些內容? – kapa89 2014-10-02 07:50:35
嘗試使用包名稱通過存儲庫引用表。檢查[doc](http://symfony.com/doc/current/book/doctrine.html#fetching-objects-from-the-database) – Matteo 2014-10-02 07:52:11
我使用「Job」而不是「jobs」,因爲「Job 「是班名嗎? – mrjayviper 2014-10-03 11:05:19