Sort by entity's method with KNP pagination - symfony

I have entity with method getItemsAmount, that fetches all Item entities related to my parent entity, counts them and returns result as regular integer.
Then I create table for parent entity with "Items Amount" column.
Now when i try to configure KNP pagination to sort by "itemsAmount" property of parent entity, I do get exception:
There is no such field [itemsAmount] in the given Query component, aliased by [a]
Thats obvious - I do not have such a property, I calculate it with my Entity class after its fetched.
Is there possibility to make KNP pagination sort such things ?

Related

Create Doctrine DQL query by instance of and select child column

I'm trying to make a query to fetch a discriminator column.
Here is what I have to fetch all links related to an Entity class:
SELECT transaction.id FROM Entity transaction
INNER JOIN transaction.links link WHERE (link INSTANCE OF Child)
So, the Entity class has an associates links method which is returning all Child entities related to it.
Now, I want to fetch a number column from link which is an instance of child class.
Something like: link.number
Is it possible to make Doctrine DQL for this? What I received from my DQL is this:
Class Child has no field or association named number

Single id is not allowed on composite primary key in entity using knp paginator

I have a question about knp paginator.
I use Symfony 2.8.
I made a table that has composite primary key, and corresponding list page using knp paginator.
I'm receiving the exception when I try to show.
"Single id is not allowed on composite primary key in entity"
I tried to inspect source files of knp paginator and doctrine.
So I found a workaround.
1)Set knp option "distinct" to false;
2)Set following hints to query.
set "knp_paginator.count" to rows count of query result.
set "knp_paginator.fetch_join_collection" to false -- this is neccessary.
Is this right way?
Are there potential problems?
The problem you might encounter is posted here in this related issue on GitHUB:
The only "problem" is when your Paginator query calls for a fetch join to a collection. The work around for this is to use a regular join as mentioned above. The drawback is that your paginated entities will not be hydrated with the collection. The collection will have to be lazy-loaded when called upon.

Symfony multiple instances of an entity in a form submission

In Symfony 2.7, I have an entity called Task against which I have persisted 5 entries in the database with the following fields:
ID, TaskName, Date
Now I want to set weights to these entries using a separate entity called Weights (obviously using relationships) the new weights class would have these fields:-
ID, TaskID (Foreign Key), WeightValue
And the rendered form should show all 5 entries of Task type in a twig template and then a text box against each entry to enter weight values like this:-
Task1 [txtbox]
Task2 [txtbox]
....
[SUBMIT]
And when I submit the form all these values should be validated and persisted to the database in Weights table with a related value in TaskID column as a relationship.
What could be the best way to do it?
Thanks
Why exactly does Weight have to be a relationship and not just a new property of task? Please see Doctrine Best Practices.
Just create a FormType for Tag containing both the name and weight and persist everything in the Tag-entity as you would normally. See the cookbook for a good example.

symfony create a table with attribute from multiple entities

I have different entities like:
Element, Status, Action, Contributor ...
And I need to create an entity, let say Synonym, which can be related to one of these previous entities
My entity Synonym, can be a synonym of the entity Element or Status or Action ...
I try to use a MappedSuperclass but I'm really lost.
I think this kind of relation is common and must be easy to create with doctrine but I can't find the good way.

Can't get any entity associated to a tree entity

I'm working on an e-learning application and I have to categorize courses. So my Section entity is one of Tree extension. In order to retrieve all courses of a section, I made the following assoication:
/**
* #ORM\OneToMany(targetEntity="Svi\FormationBundle\Entity\Course", mappedBy="section")
*/
private $courses;
The problem is that when I try get courses in twig doing this {% if section.courses|length > 0 %}, I receive this error message
Key "courses" for array with keys "id, titre, sommaire, slug, deactivated, lft, lvl, rgt, root, __children" does not exist in SviFormationBundle:Formation:see_courses.html.twig at line 22.
I made a dump on the section object and it displayed all attributes except ones of OneToMany and ManyToOne associations. Any help please? Isn't it possible to associate other entities to a Tree entity? If so how to make a nested classification in Symfony? Thanks.
In your query, you have to explicitely add the 'join' or 'leftJoin' to the 'Course' entity.

Resources