我收到此错误: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");
    }
?>


这是articles表的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 ;


我不知道怎么了

提前致谢...

最佳答案

为此,我在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代码以查找是否存在任何问题。

关于php - 主义-SQLSTATE [42S22]:找不到列:1054未知列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12079122/

10-13 01:54