Symfony2 findBy+indexBy - symfony

In query builder you can specify which field should be used as array index:
$qb = $em->createQueryBuilder();
$qb->select('s');
$qb->from('models\Settings', 's', 's.key'); // here the magic
$result = $qb->getQuery()->getResult();
//here $result[key] is entity
Anybody knows how to add indexBy(not in DB, but index of array of results) to the findBy method, or make findBy-like method for repository, something like:
$clients = $clientRepository->findBy(['id' => $clients], [indexBy=>id]);
OR
$clients = $clientRepository->myFindByWithIndexBy(['id' => $clients], 'id');
I need a method with filter criteria (where) and it should return entities indexed by id.

Second argument to findBy is for ordering results (indexing in database context is another thing).
$repo->findBy(['id'=>$clients], ['id'=>'asc']); // reverse by changing 'asc' to 'desc'

Related

Symfony Doctrine findOneBy with not equal to

I need to find a phone number in a recording where the contract ID is not equal to current contract ID.
It is easy to find in a specific contract ID. $value is the entity instance in my custom validator.
$existingPhone = $this->contractRepository->findOneBy(['phone' => $value->getPhone(), 'contractId' => $value->getContractId()]);
but how to find in other than the current contract ID?
You need to create a method in your contractRepository, and use the Doctrine QB.
$qb = $this->createQueryBuilder('c');
$qb
->where('c.phone = :phone')
->andWhere(
$qb->expr()->neq('c.contractId', 'contractId')
)
->setParameters([
'phone' => $phone,
'contractId' => $contractId,
])
->setMaxResults(1)
->getQuery()
->getOneOrNullResult();

DQL findBy default query

What is DQL findBy() query syntax?
Like this:
$this->getDoctrine()->getRepository("AppBundle:Users")->findBy($queryWhere);
It will create criteria for WHERE CLAUSE
$repository->findBy(['email' => 'test#test.com', 'city' => 'Berlin'])
SELECT * FROM table WHERE email = "test#test.com" AND city = "Berlin"
if you are interested, you can take a look in the method getSelectSQL under the following link
http://www.doctrine-project.org/api/orm/2.5/source-class-Doctrine.ORM.Persisters.Entity.BasicEntityPersister.html#877-891
This is typically used with a property of the entity. It takes an array with the key as the property name on the entity and the value as what you're searching for.
Examples:
$this->getDoctrine()
->getRepository('AppBundle:Users')
->findBy(['id' => $id])
;
$this->getDoctrine()
->getRepository('AppBundle:Users')
->findBy(['userName' => $userName])
;
$this->getDoctrine()
->getRepository('AppBundle:Users')
->findBy(['email' => $email])
;
You can read more about it here: https://symfony.com/doc/current/doctrine.html#fetching-objects-from-the-database

A reference to the object element Symfony2

I try to get to "content" from message entity.
This is my dump($messages):
http://s13.postimg.org/96ytd93vb/message.png
and my code in controller:
$em = $this->getDoctrine()->getEntityManager();
$messages = $em->getRepository('DashboardMainBundle:Message')->findBy(
array(
'receiver'=> $UserId,
'id' => $Id
),
array('createdAt' => 'ASC')
);
How can I take "content" from this array in controller?
It looks simple but I tried many methods but each failed...
For what you've currently shown, you got an array result. Currently your Message instance is being stored in $messages[0]. So you either have to iterate over your result (if you expect more than one record) or replace your findBy with findOneBy if you expect one record.

Symfony2 simple query: can't fetch an object, only array

The following query in Symfony2.6 works with getArrayResult(); BUT doesn't work with getResult(); or getOneOrNullResult();. This means that I can't fetch an object but only an array.
If I use this query, all I get is a white/blank page (not even the symfony debug toolbar). Note that in my twig template I just do a {{ dump() }} of the query result.
The table structure is easy (it's a list of books read by the users):
id, user, book, status, vote, review
(user, book and status are foreign keys)
public function selectOneRecordBy2Ids($user_id, $book_id)
{
/* #var $qb QueryBuilder */
$qb = $this->createQueryBuilder('l');
$qb->select('l');
$qb = $qb->Where($qb->expr()->eq('l.user', ':first'));
$qb = $qb->andWhere($qb->expr()->eq('l.book', ':second'));
$qb = $qb->setParameters(array('first' => $user_id, 'second' => $book_id));
return $qb->getQuery()->getOneOrNullResult();
}
I've noticed a few bad practices here, so let me correct them:
public function selectOneRecordBy2Ids(User $user, Book $book)
{
/* #var $qb QueryBuilder */
$qb = $this->createQueryBuilder('l');
$qb
->andWhere($qb->expr()->eq('l.user', ':first'))
->andWhere($qb->expr()->eq('l.book', ':second'))
->setParameters(array('first' => $user, 'second' => $book));
return $qb->getQuery()->getResult();
}
Select is not necessary if you work only with one entity and you don't fetch any relations. QB returns $this so you can chain the method calls.
Try to use entities as parameters instead of primitive types (if it is possible). If not, then you have to use primitive types as primitives in QB. In this case you'll need a few joins:
->select('l')
->join('l.user', 'u')
->join('l.book', 'b')
->andWhere($qb->expr()->eq('u.id', ':first'))
->andWhere($qb->expr()->eq('b.id', ':second'))
->setParameters(array('first' => $user_id, 'second' => $book_id));
If you want to fetch only one record, then you may have to limit the results by setting the max results:
->setMaxResults(1)
I hope this helps and solves your original problem as well.
getOneOrNullResult() does not return the record, but tells you if any record in database is found, or not.
You might want to use getSingleResult()

Doctrine / Symfony: convert custom type to database value before using QueryBuilder

I have defined a custom Doctrine data type for Uuid. When I search for an object using find($uuid), it works correctly, i.e. the attribute is converted using convertToDatabaseValue() before executing the query, and converted back with convertToPhpValue() when value is retrieved.
The conversion doesn't work if I use the QueryBuilder. Example:
$qb = $this->createQueryBuilder('s');
$qb = $qb->where( //some conditions...
$qb->expr()->eq( 's.uuid', ':uuid' ))->setParameter( 'uuid', $uuid );
I found two similar unanswered questions:
Symfony Doctrine datatype only works in findBy not querybuilder
Doctrine 2 Custom Types
It looks like that the conversion is in fact ignored.
How can I force the conversion of the parameter before executing the query? Is there a way to access the convertToDatabaseValue() function of the custom data type from the repository?
Thanks
Yes setParameter() has third parameter, but the type of third param as string is worked for me not the object.
You can do it in following way.
$qb = $this->createQueryBuilder('s');
$qb = $qb->where( //some conditions...
$qb->expr()->eq( 's.uuid', ':uuid' ))->setParameter( 'uuid', $uuid, 'uuid' );
If you dont know what exactly key is for datatype 'uuid' is.
Then use print_r(Type::getTypesMap()); to get list of all dataypes added.
In my case it was
Array
(
[array] => Doctrine\DBAL\Types\ArrayType
[simple_array] => Doctrine\DBAL\Types\SimpleArrayType
[json_array] => Doctrine\DBAL\Types\JsonArrayType
[object] => Doctrine\DBAL\Types\ObjectType
[boolean] => Doctrine\DBAL\Types\BooleanType
[integer] => Doctrine\DBAL\Types\IntegerType
[smallint] => Doctrine\DBAL\Types\SmallIntType
[bigint] => Doctrine\DBAL\Types\BigIntType
[string] => Doctrine\DBAL\Types\StringType
[text] => Doctrine\DBAL\Types\TextType
[datetime] => Doctrine\DBAL\Types\DateTimeType
[datetimetz] => Doctrine\DBAL\Types\DateTimeTzType
[date] => Doctrine\DBAL\Types\DateType
[time] => Doctrine\DBAL\Types\TimeType
[decimal] => Doctrine\DBAL\Types\DecimalType
[float] => Doctrine\DBAL\Types\FloatType
[binary] => Doctrine\DBAL\Types\BinaryType
[blob] => Doctrine\DBAL\Types\BlobType
[guid] => Doctrine\DBAL\Types\GuidType
[geometry] => CrEOF\Spatial\DBAL\Types\GeometryType
[point] => CrEOF\Spatial\DBAL\Types\Geometry\PointType
[polygon] => CrEOF\Spatial\DBAL\Types\Geometry\PolygonType
[linestring] => CrEOF\Spatial\DBAL\Types\Geometry\LineStringType
)
And my doctrine code was something like this.
$queryBuilder = $this->createQueryBuilder('c');
$queryBuilder
->where('st_contains(:polygon, point(c.latitude, c.longitude) ) = 1')
->setParameter('polygon', $city->getPolygon(), 'polygon');
Here's the solution: the function setParameter() has a third argument $type which is used to declare the typology of the parameter. The custom declared type can be retrieved with the getType() function of the Doctrine Type class:
$qb = $this->createQueryBuilder('s');
$qb = $qb->where( //some conditions...
$qb->expr()->eq( 's.uuid', ':uuid' ))->setParameter( 'uuid', $uuid, Type::getType('uuid') );

Resources