使用错误的表别名

时间:2015-10-01 14:11:19

标签: php mysql symfony doctrine-orm

我正在尝试使用doctrine / orm 2.5.1进行简单的->find()

查询非常简单:

$this->get('order_repository')->find(10);

但这会产生一个复杂的查询:

  

SELECT s0_.number AS number_0,s0_.state AS state_1,s0_.completed_at AS completed_at_2,s0_.items_total AS items_total_3,s0_.adjustments_total AS adjustment_total_4,s0_.total AS total_5,s0_.created_at AS created_at_6,s0_.updated_at AS updated_at_7,s0_.deleted_at AS deleted_at_8,s0_.id AS id_9,s0_.expires_at AS expires_at_10,s1_.currency AS currency_11,s1_.checkout_state AS checkout_state_12,s1_.payment_state AS payment_state_13,s1_.shipping_state AS shipping_state_14,s2_.quantity AS quantity_15, s2_.unit_price AS unit_price_16,s2_.adjustments_total AS adjustment_total_17,s2_.total AS total_18,s2_.is_immutable AS is_immutable_19,s2_.id AS id_20,s1_.channel_id AS channel_id_21,s1_.shipping_address_id AS shipping_address_id_22,s1_.billing_address_id AS billing_address_id_23,s1_。 customer_id AS customer_id_24,s1_.offer_id AS offer_id_25,s2_.order_id AS order_id_26,s2_.variant_id AS variant_id_27 FROM sylius_order s1_ LEFT JOIN sylius_order_item s2_ ON s1_.id = s 2_.order_id WHERE(s1_.id = 10)AND(s1_.deleted_at IS NULL)'

出现以下错误:

  

SQLSTATE [42S22]:找不到列:1054'字段列表'中的未知列's0_.number'

我可以看到s0_没有在查询的任何地方定义。这些别名是从学说中自动生成的,我只是不明白为什么它使用s0_然后将其跳到FROM上的s1_,其中包含s0 _上提到的列。*

2 个答案:

答案 0 :(得分:3)

我在同一个问题上花了一个小时;你可能犯了和我一样的错误。

我遇到的问题是.yml模式定义。在我定义表格之间关系的部分中,我有这个目标实体:

targetEntity: mlEmailNotif

而不是

targetEntity: MlEmailNotif

所以小写' m' make doctrine为新表创建一个新别名(php比较区分大小写)。

答案 1 :(得分:0)

问题出在这里:SELECT s0_.number as number_0 - 看来列号没有定义,或者映射(注释,yml,php)和架构(mysql)之间存在差异。

如果运行php app / console doctrine,那么是否有任何SQL命令:schema:update --dump-sql?如果是,则必须根据映射的实体更新架构。