9

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;
}
Nils
  • 780
  • 5
  • 16
  • did you find out any more about this error @Nils? – jah Feb 11 '13 at 15:43
  • 1
    @jah, unfortunately not. I had to split the Tag model into two objects to get the huge data chunks out of the way. That way, I can select the TagData objects when I need the data as well. Not a very elegant solution, but easy to understand and document. – Nils Feb 19 '13 at 17:52

1 Answers1

8

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

Lee Davis
  • 4,685
  • 3
  • 28
  • 39