簡體   English   中英

Symfony2在控制器中找不到我的doctrine2實體類-如何解決?

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

我試圖在我的Symfony2控制器中運行一個簡單的SQL語句(類似select * from table),但是它不起作用。 Symfony不知何故找不到課程。

一些信息:

  1. 我試圖在FROM子句中提供完整的名稱空間+類名,而僅提供類名
  2. 我已經嘗試過DQL和QueryBuilder(請參見下面的代碼。option1和option2)
  3. AppKernel正在加載我的DoctrineBundle。 當我使用作曲家創建項目時已經存在
  4. 我在settings.yml中嘗試過auto_mapping是非
  5. 我的代碼段如下

錯誤信息:

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

}
  1. 如果直接通過entitymanager或僅通過MyAppMyBundle:Job進行查詢,則使用完整的名稱空間
  2. 確保您的捆綁軟件存在於AppKernel中
  3. 傾向於使用$em->getRepository('MyAppMyBundle:Job')->createQueryBuilder('j')$em->getRepository('MyAppMyBundle:Job')->findBy(array(),array('name' => 'ASC')
  4. 使用php app/console doctrine:mapping:info和php應用程序/控制台學說:schema:validate驗證模型

symfony中的異常總是很完美,因此請集中精力處理您的異常所說的內容

驗證實體類的名稱空間。 因為在寫入錯誤的名稱空間時沒有生成錯誤,但是沒有找到實體

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM