【问题标题】:Doctrine query crashing教义查询崩溃
【发布时间】:2014-02-27 11:40:41
【问题描述】:

非常非常奇怪。我从教义中使用了这种方法数百次。我有一个简单的控制器,它以 id 作为参数。 Doctrine 生成的查询错误并崩溃。

/**
 * @Security("has_role('ROLE_ADMIN')")
 * @return Response
 */
public function editSellerAction($id)
{
    $em  = $this->getDoctrine()->getManager();
    $seller = $em->getRepository('SiteUserBundle:Seller')->find($id);

    // ...
    $form = $this->createForm(new SellerType(), $seller, array(
        'method' => 'POST'
    ));
    // ...
}

生成的查询如下

[2/2] DBALException: 执行 'SELECT t1.id AS id2, t1.username AS username3, t1.password AS password4, t1.firstname AS firstname5, t1.lastname AS lastname6 FROM Seller t1 WHERE 时发生异常t0.id = ? LIMIT 1' 带参数 ["2"]:

SQLSTATE[42S22]:找不到列:1054 'where 子句'中的未知列 't0.id' +

抛出的错误是有道理的,因为它在查看“WHERE t0.id”时应该查看“WHERE t1.id”。我使用 phpmyadmin 尝试使用 t1 进行查询,它可以工作。

知道什么可能导致这个问题吗?

/**
 * Seller have access to their customer and are able to RW access to the customers
 *
 * @ORM\Table("seller")
 * @ORM\Entity
 * @author Michael Villeneuve
 */
class Seller extends User
{

    /**
     * @var array
     * 
     * @ORM\OneToMany(targetEntity="Customer", mappedBy="seller", cascade={"persist", "remove"})
     * @ORM\JoinColumn(name="seller_id", referencedColumnName="id")
     **/
    protected $customers; 

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

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

    // Other attributes and only getters/setter


/**
 *
 * @ORM\Entity
 */
class User implements UserInterface
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255, unique=true)
     */
    private $username;

    /**
     * @ORM\Column(type="string", length=64)
     */
    private $password;

我有 3 个实体可以扩展用户(客户、管理员和卖家)。

【问题讨论】:

  • 您可以添加您所指的实体吗?
  • @byf-ferdy 我可以,但它就像 100 行一样长 + 它扩展了我的用户,这是另外 100 行。您希望看到的实体中有什么具体内容吗?
  • id 如果不寻常的话会很有趣。除此之外,您是否尝试过清除cache?我想不出更多了。
  • 通过扩展我的用户你的意思是你有一个数据库级别的关系?这两个实体在 mysql 级别上是如何表示的?
  • 好吧,既然你谈到了它,这是我第一次扩展 Doctrine 实体。所以这可能是我遇到问题的原因。给我一分钟,我会发布它的样子。

标签: symfony doctrine-orm


【解决方案1】:

更新链接:https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/reference/inheritance-mapping.html

阅读一下映射超类:http://docs.doctrine-project.org/en/latest/reference/inheritance-mapping.html。基本上,您的抽象基用户类本身不能是实体。

因此,将 @ORM\Entity 行从您的 User 类中取出。这就是表 0 (t0) 的来源。

【讨论】:

  • 这应该可以解决我的问题。请在这里查看.. stackoverflow.com/questions/47506319/…
  • 似乎链接已死@Cerad。
  • @JamshadAhmad 并不感到惊讶。最新的文档现在指向 Doctrine 3.0,它是一个完整的(在撰写本文时尚未发布)重写。我想这是不赞成仅链接答案的原因之一。但在这种情况下,我确实提供了实际的解决方案。
【解决方案2】:

尝试将id 字段添加到Seller 实体而不是用户

/**
 * Seller have access to their customer and are able to RW access to the customers
 *
 * @ORM\Table("seller")
 * @ORM\Entity
 */
class Seller extends User
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var array
     * 
     * @ORM\OneToMany(targetEntity="Customer", mappedBy="seller", cascade={"persist", "remove"})
     * @ORM\JoinColumn(name="seller_id", referencedColumnName="id")
     **/
    protected $customers; 

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

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

    // Other attributes and only getters/setter


/**
 *
 * @ORM\Entity
 * @author Michael Villeneuve<michael@panierdachat.com>
 */
class User implements UserInterface
{

    /**
     * @ORM\Column(type="string", length=255, unique=true)
     */
    private $username;

    /**
     * @ORM\Column(type="string", length=64)
     */
    private $password;

【讨论】:

  • 这不应该是解决方案。您可以扩展实体及其字段
  • 但是id 没有继承
  • 2 个实体混淆 DBAL 的问题仍然存在。
【解决方案3】:

你有两个选择:

  • 第一个是创建一个abstract User实体并继承它的所有值。如果您有许多具有相同行为的实体,这将很有用。我例如喜欢用ID 字段和一些基本的方法 创建一个BaseEntity。所有实体都可以extend 这个并自动拥有一个ID。 Cerad 解释了 in his answer 这是如何完成的。
  • 第二个选项是所谓的鉴别器字段。基本上,它们允许您为每个扩展实体拥有一个User 表和子表。您可以在official docs 中了解它们。

您最终使用哪一个可能取决于具体情况。

【讨论】:

    猜你喜欢
    • 2012-01-06
    • 2015-11-16
    • 1970-01-01
    • 2018-07-11
    • 2015-11-30
    • 2015-10-13
    • 1970-01-01
    • 2011-03-05
    • 2011-11-02
    相关资源
    最近更新 更多