简体   繁体   English

原则2的DQL错误

[英]DQL Error with doctrine 2

I have this simple controller: 我有这个简单的控制器:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        if (!is_null($this->doctrine->em))
        {
            $em = $this->doctrine->em;

            $categorias = $em->getRepository('Entities\Categoria')->findBy([], ['categoria' => 'ASC']); (*)
            $data['categorias'] = $categorias;

            $dql = "SELECT r FROM Entities\Receita r"; 
            $query = $em->createQuery($dql); 
            $query->setMaxResults(4);
            $receitas = $query->getResult(); (**)

            $data2['chamadas'] = $receitas;

            //Defino aqui o indice do menu que ficará ativo
            $this->session->set_userdata('menu_active', 0);

            $this->load->view('html_header'); 
            $this->load->view('menu_categorias', $data);
            $this->load->view('conteudo', $data2);
            $this->load->view('html_footer');

        }
    }
}

In line (*), it is working fine. 在(*)行中,它工作正常。 But in line (**), I am getting the error below. 但是在(**)行中,我得到了下面的错误。

( ! ) Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'r0_.slug_categoria' in 'field list'' in C:\\wamp\\www\\ControleReceitas_DoctrineORM\\application\\libraries\\Doctrine\\DBAL\\Driver\\AbstractMySQLDriver.php on line 71 (!)致命错误:消息为'SQLSTATE [42S22]的未捕获异常'PDOException':找不到列:1054 C:\\ wamp \\ www \\ ControleReceitas_DoctrineORM \\ application \\ libraries中的'field list'中的未知列'r0_.slug_categoria'第71行的\\ Doctrine \\ DBAL \\ Driver \\ AbstractMySQLDriver.php

PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'r0_.slug_categoria' in 'field list' in C:\\wamp\\www\\ControleReceitas_DoctrineORM\\application\\libraries\\Doctrine\\DBAL\\Driver\\PDOConnection.php on line 104 PDOException:SQLSTATE [42S22]:找不到列:1054行104上C:\\ wamp \\ www \\ ControleReceitas_DoctrineORM \\ application \\ libraries \\ Doctrine \\ DBAL \\ Driver \\ PDOConnection.php的“字段列表”中的未知列'r0_.slug_categoria'

It´sa simple query. 这是一个简单的查询。 I wanna get 4 records from the above query, but I am getting this error. 我想从上面的查询中获取4条记录,但我收到此错误。 I really don´t know what is wrong. 我真的不知道怎么了。 It is warning about r0_.slug_categoria field, but this field is in the other table. 警告有关r0_.slug_categoria字段,但该字段在另一个表中。

My intention is to get 4 random different records in every page load. 我的意图是在每个页面加载中获得4个随机的不同记录。 I know that my query here do not that. 我知道我的查询不是这样。 But I can´t do this simple one works... So, first of all, I need to understand what is going wrong to be able to go forward... 但是我做不到这个简单的作品...所以,首先,我需要了解发生了什么问题才能继续前进...

Below, are my tables structure. 下面是我的表格结构。

CREATE TABLE IF NOT EXISTS `categorias` (
  `id_categoria` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `categoria` varchar(45) NOT NULL,
  `slug_categoria` varchar(255) NOT NULL,
  PRIMARY KEY (`id_categoria`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=29 ;

CREATE TABLE IF NOT EXISTS `receitas` (
  `id_receita` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `receita` varchar(255) NOT NULL,
  `slug_receita` varchar(255) NOT NULL,
  `texto` text NOT NULL,
  `categoria` int(10) unsigned NOT NULL,
  `foto` varchar(45) DEFAULT NULL,
  `adicionada_quando` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id_receita`),
  KEY `FK_receita_categoria` (`categoria`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

What am I doing wrong ? 我究竟做错了什么 ?

Thanks, Marcelo. 谢谢,马塞洛。

I found my problem.... the error was fired because I made a mistake inside my entity and mapping files. 我发现了问题...。由于我在实体和映射文件中犯了一个错误,因此引发了错误。

Below, is my mapping file: 下面是我的映射文件:

Entities\Receita:
  type: entity
  table: receitas
  fields:
    idReceita:
      type: integer
      id: true
      generator:
        strategy: IDENTITY
      column: id_receita
    receita:
      type: string
      length: 45
      nullable: false
    slugReceita:
      type: string
      length: 255
      nullable: false
      column: slug_receita (*)
    texto:
      type: text
      nullable: false
    foto:
      type: string
      length: 45
      nullable: true
    adicionadaQuando:
      type: datetimetz
      columnDefinition: TIMESTAMP DEFAULT CURRENT_TIMESTAMP
      nullable: false
      column: adicionada_quando
  manyToOne:
      categoria:
        targetEntity: Categoria
        joinColumn:
          name: categoria
          referencedColumnName: id_categoria

The error was in line (*), where it was slug_categoria, and the correct is slug_receita. 错误在(*)行中,它是slug_categoria,正确的是slug_receita。

The second error, was found at my entity: 第二个错误是在我的实体中发现的:

class Receita
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id_receita", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $idReceita;

    /**
     * @var string
     *
     * @ORM\Column(name="receita", type="string", length=45, nullable=false)
     */
    private $receita;

    /**
     * @var string
     *
     * @ORM\Column(name="slug_receita", type="string", length=255, nullable=false) (*)
     */
    private $slugReceita;

In the line (*), it was name="slug_categoria", and the correct is name="slug_receita". 在(*)行中,该名称为name =“ slug_categoria”,而正确的名称为name =“ slug_receita”。

I made a mapping mistake, and the doctrine entity generator copies the mapping error to the entity. 我犯了一个映射错误,该学说的实体生成器将映射错误复制到了该实体。

By the way... I really know now, that I need to have more attention when doing "copy and paste" !!! 顺便说一句...我现在真的知道,在进行“复制和粘贴”时我需要更多的注意! :) :)

Best regards. 最好的祝福。 Marcelo. 马塞洛。

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

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