I am looking to run a custom query inside my entity class. I dont want to use table mapping for this query I just want to return an array of results but I would like the query to still log to the logger. I have stripped down the class and renamed to try and illustrate what I'm trying to achieve
// src/Name/ExampleBundle/Entity/ExampleEntity.php
namespace Name\ExampleBundle\Entity;
class ExampleEntity
{
public function getArrayFromExample(){
$results = $this->getEntityManager()
->createQuery("SELECT * FROM exmapleTable LIMIT 50")
->getResult();
return $results;
}
}
The above returns something like
Fatal error: Call to undefined method {path}\ExampleEntity::getEntityManager()
Your queries should be in an entity repository instead of the entity itself. http://mackstar.com/blog/2010/10/04/using-repositories-doctrine-2
Then you could do something like:
public function getArrayFromExample(){
$results = $this->_em
->createQuery("SELECT * FROM exmapleTable LIMIT 50")
->getResult();
return $results;
}
Related
I have a User entity which has an ArrayCollection of Positions. Each Position has for sure a user_id property.
Now i want to get all positions from a user (to get all i would do $user->getPositions()) that are matching a specific query, for example have a date property that matches the current date. Therefor i want to do something like $user->getCurrentPositions() and it should return a subset of the positions related to that user.
How is that possible?
EDIT:
What i really wanna do is something like this in my controller:
$em = $this->getDoctrine()->getManager();
$users = $em->getRepository('fabianbartschWhereMyNomadsAtBundle:User')->findAll();
foreach ($users as $user) {
$positions = $user->getCurrentPositions();
foreach ($positions as $position) {
echo $position->getLatitude().'<br>';
}
}
I wanna iterate over all users and from each user i want to have the relevant positions. But that isnt possible from the repository i guess, as i get the following message: Attempted to call method "getCurrentPositions" on class ...
If you are using Doctrine you can use the built-in Criteria API which is meant for this purpose exactly.
Collections have a filtering API that allows you to slice parts of data from a collection. If the collection has not been loaded from the database yet, the filtering API can work on the SQL level to make optimized access to large collections.
Ok i found out, its for sure possible with Repositories:
Entity\User.php
/**
* #ORM\Entity(repositoryClass="fabianbartsch\WhereMyNomadsAtBundle\Entity\UserRepository")
* #ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
Entity\UserRepository.php
/**
* UserRepository
*/
class UserRepository extends EntityRepository
{
public function getCurrentPositions()
{
$query = $this->getEntityManager()
->createQuery(
"SELECT p
FROM xxx:Position p
WHERE p.start <= '2014-08-17' AND p.end >= '2014-08-17'"
);
try {
return $query->getResult();
} catch (\Doctrine\ORM\NoResultException $e) {
return null;
}
}
}
In the user object only related position entries are affected by the query, so is no need to join user entity with the position entity. Pretty simple, should just try out instead posting on stackoverflow, sry guys :P
In the Symfony "Book", they talk about Entities with references to other entities. Like, in my case, if I have a "Post" Entity with many "Comment" Entities referencing it, I can load the Post by its ID and then do $post->getComments().
The Comment are lazy-loaded, it seems, and I would have to either go one-by-one through them, loading each (which is clearly wrong) or load them all in a separate query (which I could do, but I don't know where, specifically, to put that query). The Book, however, suggests: "Of course, if you know up front that you'll need to access both objects, you can avoid the second query by issuing a join in the original query." It recommends I add a method to my (currently-empty) postRepository class. I do, and it looks like this:
public function loadPostFull($pid)
{
return $this->getEntityManager()
->createQuery('
SELECT p, c FROM BUNDLENAME:Post p
JOIN p.comments c
WHERE p.id = :id')
->setParameter('id', $pid);
try {
return $query->getSingleResult();
} catch (\Doctrine\ORM\NoResultException $e) {
return null;
}
}
And then in my controller I do:
$fullPost = $this->getDoctrine()
->getRepository('BUNDLENAME:Post')
->loadPostFull($id);
$id = $fullPost->getId();
But my simple request for $id gives a "Fatal error: Call to undefined method Doctrine\ORM\Query::getId()". I know the controller is finding the loadPostFull method correctly, because if I typo the name it will fail there. It's just not returning a proper Post entity. If I change the controller to call ->find($id) instead of ->loadPostFull($id) I can use all the internal methods of the Post id.
The manual seems to indicate I can do that! What's wrong?
I believe you have a typo :). Look at the first line of your code. You have a return statement! So you return the Query object and try { } catch() { } is never reached. You should put it like this:
$query = $this->getEntityManager()
->createQuery('
SELECT p, c FROM BUNDLENAME:Post p
JOIN p.comments c
WHERE p.id = :id')
->setParameter('id', $pid);
try {
return $query->getSingleResult();
} catch (\Doctrine\ORM\NoResultException $e) {
return null;
}
I am developing an application in symfony2 and using doctrine2. I created a custom repository class that has one function:
<?php
namespace Anotatzailea\AnotatzaileaBundle\Repository;
use Doctrine\ORM\EntityRepository;
/**
* InterpretatzeaRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class InterpretatzeaRepository extends EntityRepository
{
public function getInterpDesberdinak($value)
{
$qb = $this->createQueryBuilder('c')
->select('DISTINCT c.attribute')
->where('c.fer = :Value')
->setParameter('Value', $value);
$Emaitza = $qb->getQuery()->getResult();
return $Emaitza;
}
}
What I want to get with this function is an array of all the "Interpretatzea" objects that have a distinct c.attribute and all have c.fer = value. Is the query correct? I would also want to know how to pass the value parameter to the repository function. Thanks
A cursory look at your repository method suggests it looks okay :) IIRC, I think the use of DISTINCT there is fine. If you do have problems you can always do a GROUP BY instead.
As for calling the repo method in a controller and passing a $value variable to it, that's pretty straightforward; for example:
// in your controller
$value = 'foo';
// get doctrine connection from DI, etc.
$em = $this->getDoctrine()
->getEntityManager();
// get the repository object for your
// entity and call your repository method
$result = $em->getRepository('AnotatzaileaAnotatzaileaBundle:Interpretatzea')
->getInterpDesberdinak($value);
// ... do something with your $result here
Note you use a concatenated version of your namespace and bundle, followed by a colon and the entity; e.g: AcmeTestBundle:User
Hope this helps :)
I am developing an application using Symfony2 and DQL for building some queries in the repositories. I have the next code in the controller:
$emGalPak = $this->getDoctrine()->getEntityManager();
$OsatugabeKop = $emGalPak->getRepository('AnotatzaileaAnotatzaileaBundle:GalderaPaketea')
->getOsatugabeKop();
and this is the query I built in the repository corresponding to the entity mentioned above:
<?php
namespace Anotatzailea\AnotatzaileaBundle\Repository;
use Doctrine\ORM\EntityRepository;
/**
* GalderaPaketeaRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class GalderaPaketeaRepository extends EntityRepository
{
public function getOsatugabeKop()
{
$qb = $this->createQueryBuilder('c')
->select('c')
->where('c.Osatua = 0')
$Emaitza = $qb->getQuery()->getResult();
return sizeof($Emaitza);
}
}
When running the code it shows the next error:
Parse error: syntax error, unexpected T_VARIABLE in /var/www/Symfony/src/Anotatzailea/AnotatzaileaBundle/Repository/GalderaPaketeaRepository.php on line 20
Any idea on how I can solve this error?
This has nothing to do with your query not working.
When you see a "Parse error" that means your PHP code itself is improperly formatted and the PHP engine cannot even parse it, let alone run it.
In this particular case, you're missing a semicolon at the end of your expression creating the query builder.
public function getOsatugabeKop()
{
$qb = $this->createQueryBuilder('c')
->select('c')
->where('c.Osatua = 0'); // <--- right there
$Emaitza = $qb->getQuery()->getResult();
return sizeof($Emaitza);
}
When you get the unexpected T_VARIABLE error that's almost always because you omitted a semicolon and the parser encountered a variable before it thought it should. It's easier to see the mistake if you take out the whitespace.
// Bad Code, two lines
$now = time()
$one = 1;
// Bad Code, one line
$now = time()$one = 1;
// ----------^ Pretty obvious now that a semicolon is missing
// And that a variable was encountered unexpectedly
Cheers
Semicolon missing after where line.
I'm using Doctrine's QueryBuilder to build a query, and I want to get the total count of results from the query.
$repository = $em->getRepository('FooBundle:Foo');
$qb = $repository->createQueryBuilder('n')
->where('n.bar = :bar')
->setParameter('bar', $bar);
$query = $qb->getQuery();
//this doesn't work
$totalrows = $query->getResult()->count();
I just want to run a count on this query to get the total rows, but not return the actual results. (After this count query, I'm going to further modify the query with maxResults for pagination.)
Something like:
$qb = $entityManager->createQueryBuilder();
$qb->select('count(account.id)');
$qb->from('ZaysoCoreBundle:Account','account');
$count = $qb->getQuery()->getSingleScalarResult();
Some folks feel that expressions are somehow better than just using straight DQL. One even went so far as to edit a four year old answer. I rolled his edit back. Go figure.
Here is another way to format the query:
return $repository->createQueryBuilder('u')
->select('count(u.id)')
->getQuery()
->getSingleScalarResult();
It's better to move all logic of working with database to repositores.
So in controller you write
/* you can also inject "FooRepository $repository" using autowire */
$repository = $this->getDoctrine()->getRepository(Foo::class);
$count = $repository->count();
And in Repository/FooRepository.php
public function count()
{
$qb = $repository->createQueryBuilder('t');
return $qb
->select('count(t.id)')
->getQuery()
->getSingleScalarResult();
}
It's better to move $qb = ... to separate row in case you want to make complex expressions like
public function count()
{
$qb = $repository->createQueryBuilder('t');
return $qb
->select('count(t.id)')
->where($qb->expr()->isNotNull('t.fieldName'))
->andWhere($qb->expr()->orX(
$qb->expr()->in('t.fieldName2', 0),
$qb->expr()->isNull('t.fieldName2')
))
->getQuery()
->getSingleScalarResult();
}
Also think about caching your query result - http://symfony.com/doc/current/reference/configuration/doctrine.html#caching-drivers
public function count()
{
$qb = $repository->createQueryBuilder('t');
return $qb
->select('count(t.id)')
->getQuery()
->useQueryCache(true)
->useResultCache(true, 3600)
->getSingleScalarResult();
}
In some simple cases using EXTRA_LAZY entity relations is good
http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/tutorials/extra-lazy-associations.html
If you need to count a more complex query, with groupBy, having etc... You can borrow from Doctrine\ORM\Tools\Pagination\Paginator:
$paginator = new \Doctrine\ORM\Tools\Pagination\Paginator($query);
$totalRows = count($paginator);
Since Doctrine 2.6 it is possible to use count() method directly from EntityRepository. For details see the link.
https://github.com/doctrine/doctrine2/blob/77e3e5c96c1beec7b28443c5b59145eeadbc0baf/lib/Doctrine/ORM/EntityRepository.php#L161
Example working with grouping, union and stuff.
Problem:
$qb = $em->createQueryBuilder()
->select('m.id', 'rm.id')
->from('Model', 'm')
->join('m.relatedModels', 'rm')
->groupBy('m.id');
For this to work possible solution is to use custom hydrator and this weird thing
called 'CUSTOM OUTPUT WALKER HINT':
class CountHydrator extends AbstractHydrator
{
const NAME = 'count_hydrator';
const FIELD = 'count';
/**
* {#inheritDoc}
*/
protected function hydrateAllData()
{
return (int)$this->_stmt->fetchColumn(0);
}
}
class CountSqlWalker extends SqlWalker
{
/**
* {#inheritDoc}
*/
public function walkSelectStatement(AST\SelectStatement $AST)
{
return sprintf("SELECT COUNT(*) AS %s FROM (%s) AS t", CountHydrator::FIELD, parent::walkSelectStatement($AST));
}
}
$doctrineConfig->addCustomHydrationMode(CountHydrator::NAME, CountHydrator::class);
// $qb from example above
$countQuery = clone $qb->getQuery();
// Doctrine bug ? Doesn't make a deep copy... (as of "doctrine/orm": "2.4.6")
$countQuery->setParameters($this->getQuery()->getParameters());
// set custom 'hint' stuff
$countQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, CountSqlWalker::class);
$count = $countQuery->getResult(CountHydrator::NAME);
For people who are using only Doctrine DBAL and not the Doctrine ORM, they will not be able to access the getQuery() method because it doesn't exists. They need to do something like the following.
$qb = new QueryBuilder($conn);
$count = $qb->select("count(id)")->from($tableName)->execute()->fetchColumn(0);
To count items after some number of items (offset), $qb->setFirstResults() cannot be applied in this case, as it works not as a condition of query, but as an offset of query result for a range of items selected (i. e. setFirstResult cannot be used togather with COUNT at all). So to count items, which are left I simply did the following:
//in repository class:
$count = $qb->select('count(p.id)')
->from('Products', 'p')
->getQuery()
->getSingleScalarResult();
return $count;
//in controller class:
$count = $this->em->getRepository('RepositoryBundle')->...
return $count-$offset;
Anybody knows more clean way to do it?
Adding the following method to your repository should allow you to call $repo->getCourseCount() from your Controller.
/**
* #return array
*/
public function getCourseCount()
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb
->select('count(course.id)')
->from('CRMPicco\Component\Course\Model\Course', 'course')
;
$query = $qb->getQuery();
return $query->getSingleScalarResult();
}
You can also get the number of data by using the count function.
$query = $this->dm->createQueryBuilder('AppBundle:Items')
->field('isDeleted')->equals(false)
->getQuery()->count();