2

我收到此错误:SQLSTATE[42S22]: Column not found: 1054 Unknown column 'a1_.brand_id' in 'field list'

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

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 ;

我不知道怎么了。

提前致谢...

4

1 回答 1

0

Article只是为了它在 symfony 环境中添加了你的类,提供以下最小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代码以找出是否存在任何问题。

于 2012-08-22T21:14:14.667 回答