“No mapped field” when using partial query and com

2019-02-21 17:30发布

问题:

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