Doctrine - SQLSTATE [42S22]:找不到列:1054未知列

时间:2012-08-22 18:21:47

标签: php mysql doctrine-orm

我收到此错误:SQLSTATE [42S22]:找不到列:1054'字段列表'中的未知列'a1_.brand_id'

调用此方法时出现此错误:

public function getBrandsWithArticles() {        

    $query = $this->_em->createQuery('SELECT b, a FROM Entities\Brand b JOIN b.articles a');

    return $query->getResult();

}

这是我的文章实体:

<?php

namespace Entities;

use Doctrine\Common\Collections\ArrayCollection;

/**
 * @Entity (repositoryClass="Repositories\Article")
 * @Table(name="articles") 
 * @HasLifecycleCallbacks
 */
class Article {

    /**
     * @Id @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    private $id;

    /** @Column(type="string", length=255) */
    private $name;

    /** @Column(type="string", length=255) */
    private $thumb;

    /** @Column(type="string", length=255) */
    private $big;

    /** @Column(type="text",nullable=true) */
    private $description;

    /** @Column(type="datetime") */
    private $created;

    /** @Column(type="datetime") */
    private $updated;

    /**
     * @ManyToOne(targetEntity="Category", inversedBy="articles", cascade={"persist"})
     * @JoinColumn(name="category_id", referencedColumnName="id")
     */
    private $category;
    /**
     * @ManyToOne(targetEntity="Brand", inversedBy="articles", cascade={"persist"})
     * @JoinColumn(name="brand_id", referencedColumnName="id")
     */
    private $brand;

    /**     
     * @OneToMany(targetEntity="ArticlesQuoteItems", mappedBy="articles")
     */
    private $items;

    /** @Column(type="string", length=1) */
    private $status;

    /** @Column(type="text",nullable=true) */
    private $tips;

    /** @Column(type="text",nullable=true) */
    private $features;

    /** @Column(type="text",nullable=true) */
    private $media;

    public function __construct() {
        $this->created = $this->updated = new \DateTime("now");        
        $this->details = new ArrayCollection();
    }

    /**
     * @PreUpdate
     */
    public function updated()
    {
        $this->updated = new \DateTime("now");
    }

    public function created()
    {
      $this->created = new \DateTime("now");
    }  

    public function setUpdated()
    {
        $this->updated = new \DateTime("now");
    }    
    public function setCreated()
    {
        $this->created = new \DateTime("now");
    }
?>

这是文章表的MySQL转储:

CREATE TABLE IF NOT EXISTS `articles` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`category_id` int(11) DEFAULT NULL,
`name` varchar(255) COLLATE utf8_spanish2_ci NOT NULL,
`description` longtext COLLATE utf8_spanish2_ci,
`features` longtext COLLATE utf8_spanish2_ci,
`big` varchar(255) COLLATE utf8_spanish2_ci NOT NULL,
`thumb` varchar(255) COLLATE utf8_spanish2_ci NOT NULL,
`tips` longtext COLLATE utf8_spanish2_ci,
`media` longtext COLLATE utf8_spanish2_ci,
`created` datetime NOT NULL,
`updated` datetime NOT NULL,
`status` varchar(1) COLLATE utf8_spanish2_ci NOT NULL,
`brand_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `IDX_BFDD316812469DE2` (`category_id`),
KEY `IDX_BFDD316844F5D008` (`brand_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_spanish2_ci AUTO_INCREMENT=24 ;

我不知道出了什么问题。

提前致谢...

1 个答案:

答案 0 :(得分:0)

我为了它而在symfony环境中添加了你的Article类,提供了以下最小的Brand实现:

use Doctrine\ORM\Mapping as ORM;

class Brand {

    /**
     * @ORM\Id @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;    

    /**
     * @ORM\OneToMany(targetEntity="Article", mappedBy="brand")
     */
    private $articles;  

}

然后我在数据库中添加了几个条目,我在repo方法中使用您的DQL查询获取它们:一切都很顺利,所以请发布您的Brand代码以查明是否存在任何问题。