V
V
Vadim Ivanenko2014-12-27 00:11:23
Doctrine ORM
Vadim Ivanenko, 2014-12-27 00:11:23

How to select all fields of a table using DQL in HYDRATE_ARRAY mode?

There is an Entity Treatment whose property names are different from the names of the table columns in the database.
In a select query, all fields are selected, except for the "contract" field, which is a foreign key to the dictionary.
Here it is:

/**
     * @var \Medcard\Entity\Contract
     *
     * @ORM\ManyToOne(targetEntity="Medcard\Entity\Contract")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="contract_id", referencedColumnName="id")
     * })
     */
    private $contract;

I used the following query to select:
$data = $this->entityManager->createQuery('SELECT e FROM ' . self::ENTITY_NAME 
        . ' e WHERE e.medcard = ?1')
        ->setParameter(1, $id)->getSingleResult(\Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY);

But it didn't return the contract field to me.
Then I rewrote it like this:
$data = $this->entityManager->createQuery('SELECT e, c.id as contract FROM ' 
        . self::ENTITY_NAME . ' e ' . ' JOIN e.contract c WHERE e.medcard = ?1')
        ->setParameter(1, $id)->getSingleResult(\Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY);

But it seems to me that using JOIN in this case is superfluous, because treatment already has a value that I want to select (contract_id).
There are similar problems with other Entities.
I just need to get all the data from the table, but with field names that correspond to properties in the Entity, and not the DB, so that later hydration can be performed. (That is, not contract_id, but contract, medcard_id -> medcard)
If you get an object instead of an array, and then extract it, then the contract object is returned, not its ID, and many more unnecessary objects associated with this Entity are returned.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
neolink, 2014-12-27
@supra7sky

doctrine-orm.readthedocs.org/en/latest/reference/d...
try this:
IDENTITY(e.contract) as contract

Y
yuklia_1, 2015-03-02
@yuklia_1


I just need to get all the data from the table, but with field names that correspond to properties in the Entity, and not the DB, so that later hydration can be performed. (i.e. not contract_id, but contract, medcard_id -> medcard)
To do this, in Entity, simply specify the field name that you need, and for database fields, redefine it
. For example:
/**
     * @var string
     * @ORM\Column(type="string",name="last_name")
     */
    protected $lastName;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question