简体   繁体   English

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

[英]Symfony2 cannot find my doctrine2 entity class in the controller - how to fix?

I'm trying to run a simple SQL statement (something like select * from table) in my Symfony2 controller but it's not working. 我试图在我的Symfony2控制器中运行一个简单的SQL语句(类似select * from table),但是它不起作用。 Somehow Symfony cannot find the class. Symfony不知何故找不到课程。

some info: 一些信息:

  1. I've tried providing the full namespace + class name and just class name in the FROM clause 我试图在FROM子句中提供完整的名称空间+类名,而仅提供类名
  2. I've tried DQL and QueryBuilder (see code below. option1 and option2) 我已经尝试过DQL和QueryBuilder(请参见下面的代码。option1和option2)
  3. AppKernel is loading my DoctrineBundle. AppKernel正在加载我的DoctrineBundle。 This was already there when I use composer to create my project 当我使用作曲家创建项目时已经存在
  4. I've tried auto_mapping true and false in settings.yml 我在settings.yml中尝试过auto_mapping是非
  5. snippets of my codes are below 我的代码段如下

error message: 错误信息:

[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 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/

my controller 我的控制器

<?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()));
    }
}

my entity class 我的实体班

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

}
  1. use the full namespace if you make a query directly with entitymanager or just MyAppMyBundle:Job 如果直接通过entitymanager或仅通过MyAppMyBundle:Job进行查询,则使用完整的名称空间
  2. be sure that your bundle is present in AppKernel 确保您的捆绑软件存在于AppKernel中
  3. prefer to use $em->getRepository('MyAppMyBundle:Job')->createQueryBuilder('j') or $em->getRepository('MyAppMyBundle:Job')->findBy(array(),array('name' => 'ASC') 倾向于使用$em->getRepository('MyAppMyBundle:Job')->createQueryBuilder('j')$em->getRepository('MyAppMyBundle:Job')->findBy(array(),array('name' => 'ASC')
  4. validate your model with php app/console doctrine:mapping:info and php app/console doctrine:schema:validate 使用php app/console doctrine:mapping:info和php应用程序/控制台学说:schema:validate验证模型

Exceptions in symfony are always perfect so keep the focus on what your exception says symfony中的异常总是很完美,因此请集中精力处理您的异常所说的内容

verify namespace of entity class. 验证实体类的名称空间。 Because no generate error when write wrong namespace, but no find entity 因为在写入错误的名称空间时没有生成错误,但是没有找到实体

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM