Thilanka
Thilanka

Reputation: 1763

Get values from a doctrine collection with composite key

4 for on on my applications with Doctrine.

In there I'm using the following doctrine command to retrieve person object collection

//query
$people = $q->execute();

This return 20 objects. The primary key of the person object is a composite key with three attributes. Those are

id
department_id
name

I need to get person objects by searching in it as follows.

$id = 10;
$department_id = 1;
$name = "abc";

$people->get($id, $department_id, $name);

But this doesn't work and not give correct results. I tried with this and it gives null results which seems my collections primary key is not set.

$people->getKeyColumn();

I don't want to go through a foreach loop in collection and process it because when I deal with about 500 people, it slow down my application.

Can some one help me with this issue to get values from a doctrine collection.

Upvotes: 1

Views: 2335

Answers (2)

Michal Trojanowski
Michal Trojanowski

Reputation: 12322

You can also use Doctrine's magic finders findBy*():

$people = Doctrine_Core::getTable('Persons')
    ->findByIdAndDepartmentIdAndName($id, $department_id, $name);

Upvotes: 0

Sejanus
Sejanus

Reputation: 3323

Can you use something like this?

  $people = Doctrine::getTable('Persons')
    ->createQuery()
    ->where('id = ? AND department_id = ? AND name = ?', array($id, $department_id, $name))
    ->execute();

It will get you a DoctrineCollection already filtered by the parameters provided.

'Persons' here is a Doctrine model name, not a table name from mySQL.

Upvotes: 1

Related Questions