I have two entities in 1:n relations: Race and Day - a race may have more days. This is the simple model:
Race (id)
Day (id, race_id, is_active, is_deleted)
I want to access the superior one - Race - within a Symfony2 project via Doctrine and display results in a Twig template. For the direct Race attributes it is easy.
However, it becomes trickier, when I want to use a custom defined method (sort of flag, let's call it hasActiveDays()) in Race that reflects if the race had any active and not deleted days. Simple Doctrine relation would not be enough, so I need to use a query like this:
SELECT d FROM mtboLibBundle:Day d WHERE d.isActive = 1 AND d.isDeleted = 0 AND d.raceId = :id
My question is basicly where/how to implement this query and how to invoke it in a twig template? Anything I tried resulted various errors so far, so I'd be grateful if someone could help.
I.e. this was a try:
class RaceRepository extends EntityRepository {
public function hasActiveDays() {
$em = $this->getEntityManager();
$query = $em->createQuery('SELECT f FROM mtboLibBundle:Day d
WHERE d.isActive = 1
AND d.isDeleted = 0
AND d.raceId = :id')
->setParameter('id', $this->id)
;
$days = $query->getResult();
return (count($days) == 0) ? false : true;
}
}
Method does not exist - when called from the template:
{{ race.hasActiveDays }}
I don't think you'll be able to call a function in a repository the way you are trying to do. One thing I've done is put a function on the entity class and you can call that from your template.
In your Race class:
public function hasActiveDays(){
// here, perhaps pull all of the days for this race - maybe from a doctrine relation
// loop through, filter, etc.
// return whatever is appropriate
}
... then, in your template, you'll be able to call that function the way you are trying to above.
Related
In Symfony 5, let's say we have 3 entities linked like this :
Foo is an entity that have Bar as child. Foo as one property called fooProperty.
Bar have Foo as parent, and Baz as child
Baz have Bar as parent, of course. Baz have one property called bazProperty.
Let's say that the value of bazProperty is dependent of the value of the value of fooProperty. My first idea was to refer to foo entity inside the baz entity class :
function setBazProperty($value) {
if ($this->getBar()->getFoo()->getFooProperty > 0) {
$this->bazProperty = $value;
} else {
$this->bazProperty = 0;
}
}
But this occur many sql queries, as Doctrine will ask first to get Bar entity, then Foo entity.
So I imagine to access to Foo entity through an unique query managed in a repository class.
But, because of the separation of concern, I wouldn't inject the repository in the Baz entity, but I would use a service instead.
So I've created a BazService with two arguments in the constructor :
public function __construct(Baz $baz, BazRepository $bazRepository)
{
$this->baz = $baz;
$this->bazRepository= $bazRepository;
}
In this service, I've also added a method fetching Foo entity :
public function getFoo()
{
return $this->bazRepository->getFoo($this->baz);
}
And last, in a controller, now I would like to get Foo entity :
$bazService = new BazService($baz);
$foo = $bazService->getFoo();
Here are my questions :
I'm not able to initialize bazService in the controller. The constructor ask for 2 argmuments (the entity and the repository) and I would like only to provide the entity and to inject automatically the repository class.
I've tried to add it in serices.yaml without success (probably because I didn't instantiate bazService in the constructor of my controller) :
App\Service\BazService:
arguments:
$bazRepository: App\Repository\BazRepository
Is there any other solution ? How I can inject the entity class differently in the service class ?
Using a service when setting a property is too complex is the recommended solution ? Some article (here, here and here) recommend to use a service when the method inside entity class become more complex and require external entity or repositories. But maybe there's a lighter solution...
Separation of concerns is IMHO the right argument to look at. There are some approaches to go for, that depend largely on how you retrieve the entity. However, in my opinion, the concern of an entity is NOT to fetch some other entities data from the database, it is the repository's or maybe the controller's concern. So let's see how to do that ...
One way is to automatically retrieve the parent entity / entities. Depending on your use case, you could do that in general (via fetch="EAGER" - see: #ManyToOne / #OneToOne), otherwise you could implement a special repository function, that fetches the additional entities. If your entities always only have at most one parent each, this can absolutely reduce the number of queries from 3 to 1, since the parent and parent of parent entities can be retrieved simultaneously.
// in BazRepository
public function getWithParents($id) {
$qb = $this->createQueryBuilder('baz');
$qb->leftJoin('baz.bar', 'bar')
->addSelect('bar')
->leftJoin('bar.foo', 'foo')
->addSelect('foo')
->where('baz.id = :id')
->setParameter('id', $id);
return $qb->getQuery()->getOneOrNullResult();
}
if the child entity accesses the parent entity, it should just use the entity from cache and avoid a second query (source: https://symfonycasts.com/screencast/doctrine-relations/join-n-plus-one)
If having the entities is already "too much", you can slightly cheat by (again) creating a custom repository method that not only fetches the Baz entity, but also the Foo.fooProperty value and sets it for a virtual/temporary property on the Baz entity.
// in BazRepository
public function getWithFooProperty(int $id) {
$qb = $this->createQueryBuilder('baz');
$qb->leftJoin('baz.bar', 'bar')
->lefTJoin('bar.foo', 'foo')
->select('foo.fooProperty as fooProperty')
->where('baz.id = :id')
->setParameter('id', $id);
$result = $qb->getQuery()->getResult(); // should be an array with an array with two keys, but I might be wrong
if(count($result) == 0) {
return null;
}
$baz = $row[0][0];
$baz->fooProperty = $row[0][1];
return $baz;
}
(Disclaimer: please check the $result here, to see if the accesses are correct)
you now could access it in Baz:
function getFooProperty() {
if(isset($this->fooProperty)) {
return $this->fooProperty;
} else {
// fallback, in case entity was fetched by another repository method
return $this->getBar()->getFoo()->getFooProperty();
}
}
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.
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.
When using SonataAdminBundle, I am trying to visualize the leaf nodes of a database. My tables are A -> B -> C (-> = contains some). The leaf nodes I try to visualize is C.
C has a __toString() which will call B.__toString(), which in turns calls A.__toString().
The problem: I end up showing 30 lines and making 700 calls to the database.
Sometimes, I can avoid this problem by adding a filter, so it would make a request with the filter first and would "preload" some objects, but in this case, I can't add a filter as such.
Is there a way to preload my objects beforehand? The answer would probably contain 2 parts:
Where in Sonata should I do this?
What code should I execute to preload a hierarchy of objects which would minimize the amount of calls to the database?
You should override method createQuery where you can place your custom joins.
Example:
<?php
namespace Acme\DemoBundle\Admin;
use Sonata\AdminBundle\Admin\Admin;
class CarAdmin extends Admin
{
public function createQuery($context = 'list')
{
$query = parent::createQuery($context);
$query->leftJoin($query->getRootAlias . '.Model', 'mo');
$query->leftJoin('mo.Make', 'ma');
return $query;
}
}
Remember that, createQuery method returns Sonata ProxyQuery object, not Doctrine Query. So you should operate on query returned by Admin::createQuery.
I have an issue when erasing something from a BD.
The problem is that it not only erase the object i looked for (using findOneBy), but all the objects related to the principal id.
//---Controller
$new = $this->getDoctrine()->getManager();
$OBJcar = $new->getRepository('SomeOtherBundle:CarEntityClass')
->findOneBy(array('idOwner' => $idowner, 'idCar' => $idcar));
if($OBJcar){
$new->remove($OBJcar);
$new->flush();
$msj="The car for an specific owner has been erased.";
}
//---Profiler (Query)
"START TRANSACTION"
Parameters: { }
Time: 0.22 ms
DELETE FROM schema.CarTable WHERE id_owner = ?
Parameters: ['123456']
Time: 0.63 ms
"COMMIT"
Parameters: { }
Time: 0.63 ms
How to erase the one row i am getting from the db?
I voted down the answer above, because I'm tired of people using string DQLs.
It's not standartized, non-object oriented(even though in background dql operates with objects), it doesn't use caching mechanisms query builder provides, it's non-flexible and simply looks unclean.
Here is the "right way"(IMHO):
You add the repository class for entity
You add the method you need with a query builder in it
You call the method while passing parameters needed for specific REPOSITORY OBJECT ORIENTED ACTION
You get an easy-to-handle result
Here's the code:
namespace ProjectName\BundleName\Repository;
use Doctrine\ORM\EntityRepository;
class CarRepository extends EntityRepository
{
public function deleteCarWithOwner($ownerId,$carId)
{
$isDeleted = $this->createQueryBuilder("car")
->delete()
->where('car.id = :carId')->setParameter("carId", $carId)
->andWhere('car.idOwner = :ownerId')->setParameter("ownerId", $ownerId)
->getQuery()->execute();
return $isDeleted;
}
}
Also, refer to http://doctrine-orm.readthedocs.org/en/latest/reference/query-builder.html for query builder details. There are a lot of "pros" and I see no "cons" for using builder.
LATE UPDATE
Also, a lot of Doctrine's entity events are not dispatched when using DQL.
Use DQL
$query = $em->createQuery('DELETE SomeOtherBundle:CarEntityClass c WHERE c.idOwner = 4 AND c.id = 10');
$query->execute();
This will remove only single car with ID 10 and owner with ID 4.