Symfony2在控制器中找不到我的doctrine2实体类 - 如何修复?

时间:2014-10-02 07:42:36

标签: php symfony doctrine-orm doctrine

我试图在我的Symfony2控制器中运行一个简单的SQL语句(类似于select * from table),但它无效。不知何故,Symfony找不到这门课。

一些信息:

  1. 我尝试在FROM子句中提供完整的命名空间+类名称和类名称
  2. 我尝试过DQL和QueryBuilder(参见下面的代码.option1和option2)
  3. AppKernel正在加载我的DoctrineBundle。当我使用composer创建我的项目时,这已经存在了
  4. 我在settings.yml
  5. 中尝试过auto_mapping true和false
  6. 我的代码片段在
  7. 下面

    错误消息:

    [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;
        }
    
    }
    

2 个答案:

答案 0 :(得分:2)

  1. 如果直接使用entitymanager或MyAppMyBundle:Job
  2. 进行查询,请使用完整的命名空间
  3. 确保您的捆绑包存在于AppKernel
  4. 更喜欢使用$em->getRepository('MyAppMyBundle:Job')->createQueryBuilder('j')$em->getRepository('MyAppMyBundle:Job')->findBy(array(),array('name' => 'ASC')
  5. 使用php app/console doctrine:mapping:info和php app / console doctrine:schema:validate
  6. 验证您的模型

    symfony中的异常总是完美的,所以要关注你的异常所说的

答案 1 :(得分:0)

验证实体类的命名空间。因为写错了命名空间时没有生成错误,但没有找到实体