1

this is my custome query:

$query = $em->createQuery("SELECT max(d.id) FROM MyBundle:DBTableEntity d ");
$max_incoming_id = $query->execute(); 

I want it to return the Entity Object, just like this one below:

$EntityObj = $resource->getRepository("MyBundle:DBTableEntity")->findAll();

Any idea how to do that?

1
  • 1
    Solution: $query = $resource->createQuery("SELECT d FROM MyBundle:MyEntity d where d.id > 5); $entities = $query->execute(); foreach ($entities as $i=>$entity): //get entity's ID $entity->getId(); endforeach; That's it! Commented Nov 23, 2011 at 14:08

4 Answers 4

1

Something like this should work

$EntityObjects = $resource->getRepository('MyBundle:DBTableEntity')
                         ->createQuery()
                         ->orderBy('id', 'DESC')
                         ->getResult();

$EntityObject = array_pop($EntityObjects);
Sign up to request clarification or add additional context in comments.

2 Comments

I've found the solution, its simple with foreach loop: $query = $resource->createQuery("SELECT d FROM MyBundle:MyEntity d where d.id > 5); $entities = $query->execute(); foreach ($entities as $i=>$entity): //get entity's ID $entity->getId(); endforeach; That's it!
I just ran your code and realized getRepository doesn't contain any CreateQuery() method
1

Try This

$query = sprintf("SELECT s FROM BundleName:EntityClass s where s.field1 = %d and s.field2=%d", $field1, $field2);
$em = $this->getDoctrine()->getEntityManager();
$queryObj = $em->createQuery($query);
$entities = $queryObj->execute();

Comments

0

With excute you receive a array of results (entities).

so you can do $entities = $query.excute();

return $entities[0]; //this is if you have one result;

array_pop($entities) will not work this gives a error in symfony 2.6

Comments

0

I think you will like this style:

$EntityObj = $resource->getRepository("MyBundle:DBTableEntity")
                      ->findOneBy(array(), array('id' => 'DESC'));

:)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.