Symfony2, Doctrine2, findBy() order not working - php

I created a repository for my Articles entity and I'm trying to get all values ordered by ID DESC. But, I'll get everytime values ordered by id ASC. Here is my ArticleRepository.php:
<?php
namespace Acme\BlogBundle\Entity;
use Doctrine\ORM\EntityRepository;
class ArticleRepository extends EntityRepository
{
public function findAll()
{
return $this->findBy(array(), array('id' => 'DESC'));
}
public function findOneBySlug($slug)
{
$query = $this->getEntityManager()
->createQuery('
SELECT p FROM AcmePagesBundle:Article a
WHERE a.slug = :slug
')
->setParameter('slug', $slug);
try {
return $query->getSingleResult();
} catch (\Doctrine\ORM\NoResultException $e) {
return false;
}
}
}
Any ideas?

The Syntax looks good. This should provide a set of well ordered articles.
First, make sure the #Entity annotation is set with the right Repository class within the Article Entity,
/**
* #ORM\Entity(repositoryClass="...");
*/
class Article
{
// ...
}
Also, if you want to use native helpers, you've just to call findBy from the ArticleRepository within your controller,
$articles = $this->get('doctrine')
->getRepository('YourBundle:Article')
->findBy(array(), array('id' => 'DESC'));

You don't need to create a query in ArticleRepostory.php for that
In your controller you can just do:
$entities = $em->getRepository('YourBundle:Article')->findBy(array(), array( 'id' => 'DESC' ));
->findBy(array(), array( 'id' => 'DESC' )); // Order Works
->findAll(array(), array( 'id' => 'DESC' )); // Order doesn't work

This should be work:
public function findAll()
{
$em = $this->getEntityManager();
$query = $em->createQuery('
SELECT *
FROM AcmePagesBundle:Article a
ORDER BY a.id DESC
');
return $query->getResult();
}

I would tend to do this in my repository (to allow for using the same select DQL in different methods in the repository - especially when you have lots of fetch-joins to include):
class FooRepository extends EntityRepository
{
/**
* Get the DQL to select Foos with all joins
*
* #return string
*/
public function getSelectDql()
{
$dql = '
SELECT f
FROM Entity:Foo f
';
return $dql;
}
/**
* Fetch all foos, ordered
*
* #return array
*/
public function fetchAllOrdered()
{
$dql = sprintf(
'%s %s',
$this->getSelectDql(),
'ORDER BY f.id DESC'
);
return $this->getEntityManager()
->createQuery($dql)
->getResult();
}
}
This should make your repository quite flexible, allow different ordering in different methods (if you need to order them differently) and keep all DB-related code out of your controller.

Symfony 3.3 find by order working example.
From your controller:
public function indexAction(){
$entityManager = $this->getDoctrine()->getManager();
$categoryRepository = $entityManager->getRepository(ProductCategory::class);
$items = $categoryRepository->findBy(array(), array('id' => 'DESC'));
....
}

Related

Symfony data fixtures on ManyToMany relationship

I have two entities Categories and Criteria that are linked by ManyToMany relationship. It generated a new table named as criteria_categories in the database.
What i want to do is to use fixture to populate the entity Criteria and the table criteria_categories.
The categories table has already data in the database.
So my problem is, how to get the data from categories and insert them using fixtures into the criteria_categories table?
My code is as follow:
Criteria
class Criteria
{
/**
* #ORM\ManyToMany(targetEntity="Categories", inversedBy="criteria", cascade={"persist"})
* #ORM\JoinTable(name="criteria_categories")
**/
private $categories;
}
Categories
class Categories
{
/**
* #ORM\ManyToMany(targetEntity="Criteria", mappedBy="categories")
**/
private $criteria;
}
DataFixtures
class LoadCriteria extends Fixture
{
public function load(ObjectManager $manager)
{
$criteria = array(
array(
'label' => 'Test1'
),
array(
'label' => 'Test2'
)
);
$cat = new Categories();
foreach ($criteria as $critere) {
$ctr = new Criteria();
$ctr->setCriteriaLabel($critere['label']);
$manager->persist($ctr);
}
$manager->flush();
}
}
So the real question is how to get the data from categories and use them here in this fixture to populate the table criteria_categories?
Thanks in advance for your answer.
In doctrine you can forget on criteria_categories table. The class criteria has a collection of categories. Don't need to worry about additional tables used behind the scenes.
But to your question. To get all categories from the db, you have to define this fixture as a service and then inject an entity manager or your "wrapper" service.
class LoadCriteria extends Fixture
{
/**
* #var CategoriesRepository
*/
private $repository;
public function __construct(EntityManagerInterface $em)
{
$this->repository = $em->getRepository(Categories::class);
}
public function load(ObjectManager $manager)
{
$this->categories = $repository->findAll();
$criteria = array(
array(
'label' => 'Test1'
),
array(
'label' => 'Test2'
)
);
$cat = new Categories();
foreach ($criteria as $critere) {
$ctr = new Criteria();
$ctr->setCriteriaLabel($critere['label']);
$manager->persist($ctr);
}
$manager->flush();
}
}
If you use default service configuration from symfony 3.3+, your fixture is already a service.
If you don't use default service configuration, you have to define the fixture as service manually by.
#services.yml
App\DataFixtures\ORM\CriteriaFixture:
tags: [doctrine.fixture.orm]

FindBySql in yii returns null value

I am new to yii. I am facing a proble with findBySql Method. While i am trying to get a record through passing Mysql query and parameter, it returns me a null value.
Here my code looks like this..
In Model i have defined a function getCountry() to get the country name.
class StateMaster extends CActiveRecord
{
public function tableName()
{
return 'T_State_Master';
}
public function getCountry($c_id)
{
//return array(StateMaster::model()->findBySql("select C_Name from T_Country_Master where C_Id=:CountryId;",array(':CountryId'=>$c_id)));
$result = array(StateMaster::model()->findBysql("select C_Name from T_Country_Master where C_Id={$c_id}"));
return $result;
}
/**
* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
* #param string $className active record class name.
* #return StateMaster the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}
Then in my view file trying to get the country name by providing the Country Id to it.
<?php $this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
array(
'label'=>'State Name',
'value'=>$model->S_Name,
),
array(
'label'=>'Country Name',
'value'=>$model->getCountry($model->C_Id),
),
array(
'label'=>'Created Date',
'value'=>Yii::app()->dateFormatter->format("dd-MMM-yyyy", $model->CreatedDt),
),
array(
'label'=>'Created By',
'value'=>$model->CreatedBy,
),
),
)); ?>
whether i wonder why is it not giving me the result.
I have checked the parameter passed into it and its successfully passing.
Please give me solution.
Thanks in advance
change your function to this:
public function getCountry($c_id)
{
$query = "select C_Name from T_Country_Master where C_Id={$c_id}";
//return Yii::app()->db->createCommand($query)->queryAll(); // returns an array, so in your detail view, you must handle it first
return Yii::app()->db->createCommand($query)->queryScalar();
}
Try this way, but if i were you, i will use first one.
public function getCountry($c_id)
{
$query = "select C_Name from T_Country_Master where C_Id={$c_id}";
return Yii::app()->db->createCommand($query)->queryScalar();
}
OR
public function getCountry($c_id)
{
$criteria = new CDbCriteria;
$criteria->select="C_Name";
$criteria->addCondition('C_Id = $c_id');
$result = StateMaster::model()->find($criteria);
return $result;
}

How to use KNPPaginatorBundle to paginate results using Doctrine Repository?

I'm working on a Symfony2 project and I decided to use KNPPaginatorBundle to build an easy pagination system. So I created a Product entity and I want to add the paginator to indexAction action (generated by CRUD command).
// Retrieving products.
$em = $this->getDoctrine()->getManager();
//$entities = $em->getRepository('LiveDataShopBundle:Product')->findAll();
$dql = "SELECT a FROM LiveDataShopBundle:Product a";
$entities = $em->createQuery($dql);
// Creating pagnination
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$entities,
$this->get('request')->query->get('page', 1),
20
);
It works fine but I want to use the Product's repository instead of creating the query directly in the controller. How can I do that ?
In fact, directly add the collection of results to the paginate object is just too slow because its load all products then paginate the ArrayCollection.
Thanks in advance.
K4
I suggest using QueryBuilder in your ProductRepository and then passing that to the paginator:
ProductRepository extends EntityRepository
{
// This will return a QueryBuilder instance
public function findAll()
{
return $this->createQueryBuilder("p");
}
}
In the controller:
$products = $productRepository->findAll();
// Creating pagnination
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$products,
$this->get('request')->query->get('page', 1),
20
);
I think in some cases we could use Closure and pass to it a QueryBuilder object.
In your ProductRepository you could do something like this:
ProductRepository extends EntityRepository
{
public function findAllPublished(callable $func = null)
{
$qb = $this->createQueryBuilder('p');
$qb->where('p.published = 1');
if (is_callable($func)) {
return $func($qb);
}
return $qb->getQuery()->getResult();
}
}
and then in ProductController:
public function indexAction(Request $request)
{
$em = $this->get('doctrine.orm.entity_manager');
$paginator = $this->get('knp_paginator');
$func = function (QueryBuilder $qb) use ($paginator, $request) {
return $paginator->paginate($qb, $request->query->getInt('page', 1), 10);
};
$pagination = $em->getRepository('AppBundle:Report')->findAllPublished($func);
// ...
}
I think it more flexible and you could use findAllPublished method to get both paginated or NOT paginated results if you need.
Also keep in mind that callable type hint work in PHP >=5.4! Please, check docs for more info.
In our project we want to avoid using Doctrine queries in controllers. We have also separate layers. Controllers must not access the database. So I included pagination in the Repository.
Here my code in controller:
public function indexAction(Request $request)
{
$userRepository = $this->get('user_repository');
$page = intval($request->query->get('page', 1));
$pages = 0;
$users = $userRepository->findAllPaginated($pages, $page - 1, 10);
return $this->render('User:index.html.twig', array(
'users' => $users,
'page' => $page,
'pages' => $pages,
));
}
And here is the important code in my repository:
use Doctrine\ORM\Tools\Pagination\Paginator;
class UserRepository extends EntityRepository
{
/**
* #return User[]
*/
public function findAllPaginated(&$pages, $startPage = 0, $resultsPerPage = 5)
{
$dql = 'SELECT u FROM CoreBundle:User u';
$query = $this->getEntityManager()->createQuery($dql)
->setFirstResult($startPage * $resultsPerPage)
->setMaxResults($resultsPerPage);
$paginator = new Paginator($query);
$count = $paginator->count();
$pages = floor($count/$resultsPerPage);
return $paginator; // on $paginator you can use "foreach", so we can say return value is an array of User
}
}

How can I implemented a Doctrine2 filter based on a relation?

I have a Doctrine2 listener & filter that acts as a means of filtering out any unapproved/draft entities, which works fine on the entity that it's applied to, however, I don't see how to get it to work for its relations.
Lets say that entity is called Category, I then have Products related to that Category, when I do a findBy() for Products, I need the query to check that the Category they relate to is approved.
select * from products p
left join category c on p.category_id = c.id
where p.id = 5 and c.approved = true
The bits in bold are what need to be injected by my filter or equivalent.
How can I go about implementing this?
So far I have a subquery injected as part of the where in the filter, but this seems hellish, and I'm thinking there must be a better way:
class ApprovableFilter extends SQLFilter
{
protected $listener;
protected $entityManager;
public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias)
{
$config = $this->getListener()->getConfiguration($this->getEntityManager(), $targetEntity->name);
/* this bit works fine for the category */
if (isset($config['approvable']) && $config['approvable']) {
$column = $targetEntity->columnNames[$config['fieldName']];
return $targetTableAlias.'.'.$column.' = true';
}
/* this bit works for products.. but seems like a pretty poor solution */
if (isset($targetEntity->associationMappings['category'])) {
$config = $this->getListener()->getConfiguration(
$this->getEntityManager(),
$targetEntity->associationMappings['category']['targetEntity']
);
return '(
select d.id from dealership d
where d.id = '.$targetTableAlias.'.dealership_id
and d.'.$config['fieldName'].' = true
) is not null';
}
}
Your solution can be made a little more general by using annotations. I've written a gist about it at: https://gist.github.com/technetium/0c62164400a411e9ffc3713260448b25
The best i can think of, is to retrieve your Products via the Category object.
That way you only need to filter for the category.approved field.
For example:
public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias)
{
$config = $this->getListener()->getConfiguration($this->getEntityManager(), $targetEntity->name);
/* this bit works fine for the category */
if (isset($config['approvable']) && $config['approvable']) {
$column = $targetEntity->columnNames[$config['fieldName']];
return $targetTableAlias.'.'.$column.' = true';
}
}
Then your Category entity should have a products collection (assuming you have a Bidirectional relation).
use Doctrine\Common\Collections\ArrayCollection;
class Category {
/**
* #var ArrayCollection $products
* #ORM\OneToMany(targetEntity="Product", mappedBy="category")
*/
private $products;
public function __construct()
{
$this->products = new ArrayCollection;
}
public function getProducts()
{
return $this->products;
}
}
That way you can first retrieve your Category
$category = $this->get('doctrine')->getRepository('SomeBundle:Category')->find(5);
if( $category ) {
//Here you now the category is approved
$products = $category->getProducts();
}
hope this helps.
Edit:
To answer #lracicot question and to give an example for unidirectional relation:
I would create a ProjectRepository method for example:
...
findByCategoryApproved( $product_id, $approved = true )
{
$query =
'SELECT p
FROM AcmeBundle:Product
LEFT JOIN p.categories c
WHERE p.id = :id AND c.approved = :approved';
return $this
->getEntityManager()
->createQuery( $query )
->setParameter( 'id', $product_id )
->setParameter( 'approved', $approved )
->getOneOrNullResult();
}
...
$product = $doctrine
->getRepository('AcmeBundle:Product')
->findByCategoryApproved(5);

Symfony2: how to get user's list by ROLE in FOSUserBundle? [duplicate]

I am using the FOS bundle and I want to retrieve all users with a given ROLE from the database.
What is the best way to do this?
Just add this in your UserRepository or replace $this->_entityName by YourUserBundle:User:
/**
* #param string $role
*
* #return array
*/
public function findByRole($role)
{
$qb = $this->_em->createQueryBuilder();
$qb->select('u')
->from($this->_entityName, 'u')
->where('u.roles LIKE :roles')
->setParameter('roles', '%"'.$role.'"%');
return $qb->getQuery()->getResult();
}
If you are using FOSUser Groups you should use:
/**
* #param string $role
*
* #return array
*/
public function findByRole($role)
{
$qb = $this->_em->createQueryBuilder();
$qb->select('u')
->from($this->_entityName, 'u')
->leftJoin('u.groups', 'g')
->where($qb->expr()->orX(
$qb->expr()->like('u.roles', ':roles'),
$qb->expr()->like('g.roles', ':roles')
))
->setParameter('roles', '%"'.$role.'"%');
return $qb->getQuery()->getResult();
}
Well, if there is no better solution, I think I will go to a DQL query:
$query = $this->getDoctrine()->getEntityManager()
->createQuery(
'SELECT u FROM MyBundle:User u WHERE u.roles LIKE :role'
)->setParameter('role', '%"ROLE_MY_ADMIN"%');
$users = $query->getResult();
If you have this requirement and your user list will be extensive, you will have problems with performance. I think you should not store the roles in a field as a serialized array. You should create an entity roles and many to many relationship with the users table.
As #Tirithen states, the problem is that you will not get the users that have an implicit role due to role hierarchy. But there is a way to work around that!
The Symfony security component provides a service that gives us all child roles for a specific parent roles. We can create a service that does almost the same thing, only it gives us all parent roles for a given child role.
Create a new service:
namespace Foo\BarBundle\Role;
use Symfony\Component\Security\Core\Role\RoleHierarchy;
use Symfony\Component\Security\Core\Role\Role;
/**
* ReversedRoleHierarchy defines a reversed role hierarchy.
*/
class ReversedRoleHierarchy extends RoleHierarchy
{
/**
* Constructor.
*
* #param array $hierarchy An array defining the hierarchy
*/
public function __construct(array $hierarchy)
{
// Reverse the role hierarchy.
$reversed = [];
foreach ($hierarchy as $main => $roles) {
foreach ($roles as $role) {
$reversed[$role][] = $main;
}
}
// Use the original algorithm to build the role map.
parent::__construct($reversed);
}
/**
* Helper function to get an array of strings
*
* #param array $roleNames An array of string role names
*
* #return array An array of string role names
*/
public function getParentRoles(array $roleNames)
{
$roles = [];
foreach ($roleNames as $roleName) {
$roles[] = new Role($roleName);
}
$results = [];
foreach ($this->getReachableRoles($roles) as $parent) {
$results[] = $parent->getRole();
}
return $results;
}
}
Define your service for instance in yaml and inject the role hierarchy into it:
# Provide a service that gives you all parent roles for a given role.
foo.bar.reversed_role_hierarchy:
class: Foo\BarBundle\Role\ReversedRoleHierarchy
arguments: ["%security.role_hierarchy.roles%"]
Now you are ready to use the class in your own service. By calling $injectedService->getParentRoles(['ROLE_YOUR_ROLE']); you will get an array containing all parent roles that will lead to the 'ROLE_YOUR_ROLE' permission. Query for users that have one or more of those roles... profit!
For instance, when you use MongoDB you can add a method to your user document repository:
/**
* Find all users with a specific role.
*/
public function fetchByRoles($roles = [])
{
return $this->createQueryBuilder('u')
->field('roles')->in($roles)
->sort('email', 'asc');
}
I'm not into Doctrine ORM but I'm sure it won't be so different.
You can use just this on your DQL:
SELECT u FROM YourFavouriteBundle:User u WHERE u.roles [NOT] LIKE '%ROLE_YOUR_ROLE%'
Of course with QueryBuilder it's more elegant:
// $role = 'ROLE_YOUR_ROLE';
$qb->where('u.roles [NOT] LIKE :role')
->setParameter('role', "%$role%");
Finally i solved it, following is an exact solution:
public function searchUsers($formData)
{
$em = $this->getEntityManager();
$usersRepository = $em->getRepository('ModelBundle:User');
$qb = $usersRepository->createQueryBuilder('r');
foreach ($formData as $field => $value) {
if($field == "roles"){
$qb->andWhere(":value_$field MEMBER OF r.roles")->setParameter("value_$field", $value);
}else{
$qb->andWhere("r.$field = :value_$field")->setParameter("value_$field", $value);
}
}
return $qb->getQuery()->getResult();
}
Cheers!
In case you need to filter users by role using a DQL filter in a YAML file (In EasyAdminBundle for instance)
entities:
Admin:
class: App\Entity\User
list:
dql_filter: "entity.roles LIKE '%%ROLE_ADMIN%%'"
Here I give an alternative solution :
I find users of roles for a given array
In controller I call the function like that
$users = $userRepository->findUsersOfRoles(['ROLE_ADMIN', 'ROLE_SUPER_USER']);
Then in my repository I make a loop to generate condition and set the parameters :
public function findUsersOfRoles($roles)
{
$condition = 'u.roles LIKE :roles0';
foreach ($roles as $key => $role){
if ($key !== 0){
$condition .= " OR u.roles LIKE :roles".$key;
}
}
$query = $this->createQueryBuilder('u')
->where($condition);
foreach ($roles as $key => $role){
$query ->setParameter('roles'.$key, '%"'.$role.'"%');
}
return $query->getQuery() ->getResult();
}

Categories