Doctrine2 HYDRATE_ARRAY, select Relation Primaries?

im trying to get the IDs (primaries) from related entities in an “HYDRATE_ARRAY” result with Doctrine2 QueryBuilder. setting the fields somerel_id and anotherrel_id to primary would give me the result i need, but i cant do this 🙂

this is my entity:

/**
 * SomeEntity
 *
 * @ORMTable(name="someentity", uniqueConstraints={@ORMUniqueConstraint(name="name_f", columns={"name", "somerel_id"})}, indexes={@ORMIndex(name="name", columns={"name"}), @ORMIndex(name="fk_something_idx", columns={"anotherrel_id"}), @ORMIndex(name="fk_something2_idx", columns={"somerel_id"})})
 * @ORMEntity
 */
class SomeEntity extends Entity
{
    /**
     * @var integer
     *
     * @ORMColumn(name="id", type="bigint", nullable=false)
     * @ORMId
     * @ORMGeneratedValue(strategy="IDENTITY")
     */
    protected $id;

    /**
     * @var DateTime
     *
     * @ORMColumn(name="created", type="datetime", nullable=false)
     */
    protected $created;

    /**
     * @var DateTime
     *
     * @ORMColumn(name="modified", type="datetime", nullable=true)
     */
    protected $modified;

    /**
     * @var string
     *
     * @ORMColumn(name="name", type="string", length=255, nullable=false)
     */
    protected $name;

    /**
     * @var string
     *
     * @ORMColumn(name="xname", type="string", length=255, nullable=false)
     */
    protected $xname;

    /**
     * @var boolean
     *
     * @ORMColumn(name="lock", type="boolean", nullable=false)
     */
    protected $lock = '0';

    /**
     * @var integer
     *
     * @ORMColumn(name="size", type="bigint", nullable=false)
     */
    protected $size;

    /**
     * @var string
     *
     * @ORMColumn(name="type", type="string", length=255, nullable=false)
     */
    protected $type;

    /**
     * @var SomeRelation
     *
     * @ORMManyToOne(targetEntity="SomeRelation")
     * @ORMJoinColumns({
     *   @ORMJoinColumn(name="somerel_id", referencedColumnName="id")
     * })
     */
    protected $someRelation;

    /**
     * @var AnotherRelation
     *
     * @ORMManyToOne(targetEntity="AnotherRelation")
     * @ORMJoinColumns({
     *   @ORMJoinColumn(name="anotherrel_id", referencedColumnName="id")
     * })
     */
    protected $anotherRelation;

...

}

the query:

$qb
    ->select( partial entity.{created,modified,name,type,id,someRelation,anotherRelation} )
    ->from( $this->getEntityName(), 'entity' )
    ->getQuery()
    ->getSingleResult( DoctrineORMQuery::HYDRATE_ARRAY )
;

result:

{
    "id": "1551",
    "created": 1349895795,
    "modified": 1349895795,
    "name": "somename",
    "type": "sometype"
}

result i need:

{
    "id": "1551",
    "created": 1349895795,
    "modified": 1349895795,
    "name": "somename",
    "type": "sometype",
    "somerel_id": "123",
    "anotherrel_id": "321"
}

do you have any suggestions? googlin for hours now… cant find a similar question.

Source: New Questions

Leave a Reply