“No mapped field” when using partial query and composite keys in Doctrine2

喜你入骨 提交于 2019-12-30 17:38:44

问题


I have two models called Person and Tag. One Person has many Tags, and the Tag primary key is a composite key of person_id and tag (Person $person and $tag in Doctrine2).

There is a data field (BLOB) in the Tag model with a lot of data. I am setting up a query that does not require the data from that field, so I want to set up a query that does not retrieve that field.

I tried with the following query:

SELECT c, PARTIAL t.{tag} FROM Contact c LEFT JOIN c.tags

Here, I get the somewhat expected error The partial field selection of class Tag must contain the identifier. No problem, I add the contact field:

SELECT c, PARTIAL t.{contact,tag} FROM Contact c LEFT JOIN c.tags

But now, I get There is no mapped field named 'contact' on class Tag.

Does Doctrine2 not support partial queries on composite keys?

Here is the Tag class:

/** @Entity @Table(name="tag") **/
class Tag
{
    /** @Id @ManyToOne(targetEntity="Contact",inversedBy="tags") @var Contact **/
    protected $contact;
    /** @Id @Column(type="string",length=10,nullable=false) @var string **/
    protected $tag;
    /** @Column(type="blob") **/
    protected $data;
}

回答1:


Whenever performing a partial selection you need to include the primary key of the class you're selecting from.

You haven't actually detailed your "Contact" entity but i'm assuming the primary key field of that class is "id". If this was the case then the following query will acheive what you're after:

SELECT c, PARTIAL t.{id, tag} FROM Contact c LEFT JOIN c.tags

This doesn't appear to be documented :(

http://docs.doctrine-project.org/en/latest/reference/dql-doctrine-query-language.html#partial-object-syntax



来源:https://stackoverflow.com/questions/13844970/no-mapped-field-when-using-partial-query-and-composite-keys-in-doctrine2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!