Symfony/Doctrine: fetching data as object , still get array - symfony

I have in my controller $id it's a foreign key
$query = $em->getRepository('SurgeryPatientBundle:Patients')->findPatientByUserID($id);
And in my repository file this function
public function findPatientByUserID($id)
{
return $this->getEntityManager()
->createQuery('SELECT p FROM SurgeryPatientBundle:Patients p WHERE p.user ='.$id.'')
->execute();
}
I want get an instance of object but still get an array. Query with find($id) works good
edit
Problem solves , I'm so stupid , I had invoked to $query[0]

You can use $query->getSingleResult(); as well
see here
http://docs.doctrine-project.org/en/2.1/reference/dql-doctrine-query-language.html#query-result-formats

If you want to grab the object, you shouldn't be using DQL. Doctrine entities have a find function that takes care of this for you.
Instead of all that code, you can just use (in your controller):
$em->getRepository('SurgeryPatientBundle:Patients')->find($id);
DQL is very powerful, but for simple lookups like this using the built in find methods will be more efficient & provide the entities as doctrine objects.

Related

link between QueryBuilder parameters

public function Bycategorie($categorie)
{
$qb = $this->createQueryBuilder('u')
->select('u')
->where('u.categorie = :categorie')
->orderBy('u.id')
->setParameter('categorie',$categorie);
return $qb->getQuery()->getResult();
}
I want to know what the link is between:
$categorie, u and the entity in which I'm working,
Your question is related to Doctrine. About association mapping you can read here
In your example relation can be One-to-One (in theory) or Many-to-One (many users can obtain the same category). Many-to-One relation is more preferable variant and has a logical explanation.
helo, i have found what i was searching for.
my question was : what is the link between querybuilder parameters.
in my example i find that in the where clause (where('u.categorie =:categorie'))
'categorie' of the left(in the parenthesis) must be a colum of a table in my database.
thanks for your helps.

PHP: Symfony2 Doctrine "The discriminator column "discr" is missing" HydrationException with filter

fellow programmers :)
I'm having a bad case of no idea what's going on.
This concerns two entities: Ware and File.
In my repository, I have a function that returns File objects along with wares to avoid lazy loading:
The relevant part of this function ( because the exception happens even if i trigger this bit only ) is this:
public function findByWithFilesTotal($params, $page = false, $per_page = false){
$res = $this->_em->createQuery('SELECT w, f FROM ShopBundle:Ware w JOIN w.files f');
// same result occurs with LEFT JOIN
return count($res->getResult());
}
Important stuff:
1) Ware and File classes are direct descendants of class Data. Discriminators are all right.
2) Ware has OneToMany relation with File - that means File table has ware_id column.
3) This is the most important part ( IMHO ). I use this filter to separate deleted items in all Data descendants.
class UndeletedFilter extends SQLFilter
{
public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias)
{
if(!$targetEntity->hasField('deleted'))
return "";
return "( $targetTableAlias.deleted = 0 OR $targetTableAlias.deleted IS NULL)";
}
}
It works for all the other entities, but causes a HydrationException with message 'The discriminator column "discr" is missing for "Acme\CoreBundle\Entity\File" using the DQL alias "f".' for this one query.
This, however, DOES NOT happen if I either remove JOIN w.files from DQL query, and leave it to lazy loading, or return an empty string from addFilterConstraint() method even if it does have the 'deleted' field.
So, if anyone knows: What exactly causes this, and how do solve it?
Thank you in advance :)
One reason for trigger this exception in when you are working inherent classes, then for example, if your discriminator column has null values the QueryBuilder will do not know how to convert this unknown type based in type.

Share logic between queries in Doctrine

I'm developing a web app, and I might have logic such as "When Field A = value and Field B = other value, then this should be shown in the frontend." I might have different lists, which further elaborate on this. I might even have translated entities which have the translation in a different entity (example: Entity and EntityTranslation) and want to join them only on a specific locale.
How could I reuse all this logic, to avoid repeating the same QueryBuilder::andWhere() and QueryBuilder::join() (even QueryBuilder::select()) calls all over the place?
I've found http://www.whitewashing.de/2013/03/04/doctrine_repositories.html which talks on this, but I'm curious about solutions which involve also JOINs and SELECT.
EDIT:
Bad example of what I'd want:
$queryBuilder
->andWhere(FRONTEND_LOGIC)
->joinWithTranslationTable();
So I'd want to be able to compose "complex" queries from my, simpler, but Buisiness driven, parts.
You can use prepared statements in Doctrine's DBAL layer.
$dbal = $this->getDoctrine()->getConnection('default');
$stmt = 'SELECT name, birth from user where id = :id';
$user1 = $dbal->executeQuery( $stmt , array( 'id' => 1 ) )->fetchAll() ;
$user2 = $dbal->executeQuery( $stmt , array( 'id' => 2 ) )->fetchAll() ;
Refer to the documentation for other methods suitable for your task:
http://docs.doctrine-project.org/projects/doctrine-dbal/en/2.0.x/reference/data-retrieval-and-manipulation.html#using-prepared-statements
Doctrine does not allow you to specify the query builder class so you cannot directly extend it. You can however decorate it.
class MyQueryBuilder
{
public function __construct($doctrineQueryBuilder) { this->doctrineQueryBuilder = $doctrineQueryBuilder; }
// Your custom functions
public function addWhereFrontEndLogic ...
public function joinWithTranslationTable ...
// Also need to add the the regular methods that you use
public function addSelect($value) { return $this->doctrineQueryBuilder($value); }
// You would then
$qb = new MyQueryBuilder($this->createQueryBuilder());
Bit of a pain to have to add all the standard functions but once you have done it then you can add as much custom business logic as you want.
There are some other possible approaches. You could add the custom methods to a base repository class and then just call the methods with the doctrine $qb as an argument.

Symfony2 Select one column in doctrine

I'm trying to refine the query trying to select fewer possible values ​​..
For example I have an entity "Anagrafic" that contains your name, address, city, etc.,
and a form where I want to change only one of these fields, such as address.
I have created this query:
//AnagraficRepository
public function findAddress($Id)
{
$qb = $this->createQueryBuilder('r')
->select('r.address')
->where('r.id = :id')
->setParameter('id', $Id)
->getQuery();
return $qb->getResult();
}
there is something wrong with this query because I do not return any value, but if I do the query normally:
//Controller
$entity = $em->getRepository('MyBusinessBundle:Anagrafic')->find($id);
Return the right value.
How do I do a query selecting only one column?
Since you are requesting single column of each record you are bound to expect an array. That being said you should replace getResult with getArrayResult() because you can't enforce object hydration:
$data = $qb->getArrayResult();
Now, you have structure:
$data[0]['address']
$data[1]['address']
....
Hope this helps.
As for the discussion about performance in comments I generally agree with you for not wanting all 30 column fetch every time. However, in that case, you should consider writing named queries in order to minimize impact if you database ever gets altered.
You can use partial objects to only hydrate one field and still return a object.
This worked for me:
$qb = $repository->createQueryBuilder('i')
->select('i.name')
->...
Use partial objects like this to select fields
$qb = $this->createQueryBuilder('r')
->select(array('partial r.{id,address}'))
...
Put your field names between the brackets

How to list user based on their roles in Symfony2 + Doctrine effectively

I have done this with this code
$users = $this->getDoctrine()->getEntityManager()
->createQuery('SELECT tu FROM UserBundle:User tu')
->getResult();
$result = array();
foreach($users as $user){
if($user->hasRole('ROLE_CUSTOMER'))
$result[] = $user;
}
return $this->render('XBundle:Order:index.html.twig',
array('users' => $result));
But what is the simple way to achieve this same result ?
Thanks.
Update, additional info:
Our main problem is that user roles have a hierarchical structure.
So ROLE_EDITOR will automatically have ROLE_WRITER if I defined ROLE_WRITER under ROLE_EDITOR.
Thus I can't just use simple query, I think I also have to utilize security context component.
I don't know your schema, but I guess you could do an INNER JOIN and put your condition in the ON clause. Read this article about the query builder to figure out how to build your query.
Doctrine collections also have a filter method that takes a Closure as an argument.

Resources