Is there anyway to pass dql to doctrine criteria - symfony

I am using Symfony3 framework, and I have user entity, and file entity. I wanted to present in sonata administration user list with sum of all size files which are uploaded by user. When I want to make that field sortable I am getting error:
`Catchable Fatal Error: Argument 1 passed to Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery::entityJoin() must be of the type array, null given, called in /home/milos/sites/coinaphoto/vendor/sonata-project/doctrine-orm-admin-bundle/Datagrid/ProxyQuery.php on line 143 and defined`
I have custom function in User entity which is calculating sum of files. It returns string.
My question will be can I somehow pass dql to criteria in order to get sum. Or can you suggest some other way to implement this?
` public function getStoragge(){
$criteria = Criteria::create()
->where(Criteria::expr()->someexpression...);
$matches = $this->file->matching($criteria);
}`
Something similar like when you need to aggregate fields
` $dql = "SELECT SUM(e.amount) AS balance FROM Bank\Entities\Entry e " .
"WHERE e.account = ?1";
$balance = $em->createQuery($dql)
->setParameter(1, $myAccountId)
->getSingleScalarResult();`

I don't know about your dql thing, but some fact about sorting in Sonata Admin table views. The problem is, that sorting actions still be made in the background with database operations, no matter what you are doing "virtual" in your model. If you are adding just a method to your model, the datagrid is not able to involve this method/property in sorting.
My experience is, that you can't sort by fields which are not physically available in the corresponding table. Even if you write the whole query for yourself, if you click on the sort buttons, a complete other query is fired which is ignoring your request.
I'll be pleased if somebody tell me the contrary ...
See also this issue https://github.com/sonata-project/SonataAdminBundle/issues/1077

Related

Can entity tracking be turned off?

I've got multiple entities set up with their respective repositories, all working properly. However, Doctrine seems to populate proxies where I don't want them to be populated.
I've got an entity called Item, which references a Category, both by having a $category_id and a $category field. The latter has the relationship set up with #ORM\ManyToOne() and #ORM\JoinColumn(), working correctly.
In my controller, when I'm querying Items, I receive a list of items with proxies to the related categories, which I can strip out from my response, identifying them as being proxies. However, if for whatever reason, I also query Categories in an unrelated query, the item-related query return with not proxies but actual hydrated Category instances, which I don't want.
$this->categoryRepository->findBy(...);
...
$items = $this->itemRepository->findBy(...);
return $this->respond($items);
Here, $items[0]->category will have been populated by the framework by the time the execution reaches the return statement.
Is it possible to turn this behaviour off?
You can specify the fetch policy on a relationship to be EXTRA_LAZY, this will fetch the least amount of date on execution as possible. Per the docs:
With Doctrine 2.1 a feature called Extra Lazy is introduced for associations. Associations are marked as Lazy by default, which means the whole collection object for an association is populated the first time its accessed.
So in your #ORM\ManyToOne() annotation, add a parameter: fetch="EXTRA_LAZY" to the others.

How to use Catalyst to search, select and display entries from my database

I have to use Catalyst in order to create a database and access it through a browser.
I have created a very simple database using DBIx-class and sqlite, just one table and filled it with some records.
I have managed to display the whole table and its rows using Template Toolkit view module and the code below into my controller.
$c->stash(ptm => [$c->model('DB::ptm')->all]);
Now I have created a simple search box in order to search the database and display any entries that match with the keyword, but I don't know how to pass the keyword to my controller nor how to implement the subroutine in order to achieve this.
I have searched for more than three days without finding any solution.
There are two completely different problems here.
Accepting arguments in Catalyst
Performing a search in DBIC
So, starting with the first one. Reading a query string.
$c->request->query_parameters->{field}
Then performing a search. Just call search instead of all and pass a hashref of your columns and values.
$c->model('DB::ptm')->search( { 'name' => $tag } );

Using a table display method in a query or view

I'm trying to make a query against the table HcmWorker and related.
But i want to figure out how to get the result of the display method HcmWorker.primaryDepartmentName() into it's own field in my query.
I also tried creating a view to execute the function via a ViewMethod but that doesn't seem to work as ViewMethods only inject code into the final query against the view.
I'm NOT making a form. The end result has to come through the QueryService.
Sorry, but what you are trying to do is not possible.
You could calculate a non stored field in the postLoad method, but that would impact every access to your table, and it would most likely not work in the context of a query service.

Simplify Doctrine code in Controller

(sorry for my bad english)
I'm a newbie to Symfony2/Doctrine, and currently doing my first project. Is there a way to simplify the below code?
I have two Entities, one contains Orders and other entity that contain Order Status (NEW, IN PROGRESS, SENT, ....). Order have a field 'Status' which is one to many with the 'Status' id field in Status entity.
When I create a new Order, I must assign a status to the order, with this code:
$order = new Order();
$order->setStatus($this->getDoctrine()->getEntityManager()->getRepository('OrderBundle:Status')->findOneByStatus(0));
'0' means for status 'NEW'. I think that this code can be simplified, but can't find how to do this.
Any ideas ?
Thanks!
What you need is to introduce relations between entities (many-to-one, for example). Check official Doctrine documentation.
Another option, if you don't want to limit your entities with relations, you may put the logic into custom entity repositories. This will reduce the amount of code a bit.

Field Collection, adding new elements to it in form

I have a $form with a field collection "field_definition" which has 2 fields in it with unlimited cardinality. I can kind of create the fields by doing this in hook_form_alter:
$field_definition_template = $form['field_definition']['und'][0];
for($k=0;$k<count($column_names);$k++)
{
$form['field_definition']['und'][$k] = $field_definition_template;
$form['field_definition']['und'][$k]['#delta'] = $k;
}
The problem is the names are wrong, the ids are wrong and pretty much everything else. Is there a way to do this correctly?
You will want to work with hook_field_presave() in order to add the values to the field itself. Using Form API on fields does work, sometimes, but it will be cleaner if you switch over to using the Field API directly.

Resources