I want to select users from a database with Doctrine and Symfony. Depending on whether I have a supplied list of user IDs I want to only select users with these IDs. If the list is empty, then all users should be selected.
Here is the code I have created so far:
class UserRepository extends EntityRepository {
public function selectUsers (array $userIds) {
$dql = "
SELECT
u
FROM
MyBundle:User
WHERE
u.id IN (:users)"; // OR (:users) does not contain any values
$query = $this
->getEntityManager()
->createQuery($dql)
->setParameter("users", $userIds);
return $query->getResult();
}
}
How can I check whether the array is empty? So far, I have tried IS EMPTY, = (), = [], SIZE(:users) = 0, COUNT(:users) = 0 but all of them give me errors. What is the correct syntax here?
You can dynamically build DQL query
public function selectUsers(array $userIds)
{
$dql = "SELECT u FROM MyBundle:User";
$params = array();
if ($users) {
$dql .= " WHERE u.id IN (:users)";
$params["users"] = $userIds;
}
return $this->getEntityManager()->createQuery($dql)->execute($params);
}
You are building two different queries - select all, select specific users. I think you cannot build such SQL query. Maybe DQL has some shortcut how you can do it, but I would prefer SQL-ish syntax.
The only solution I have come up with so far is to calculate the count in PHP and pass it in as an additional parameter.
class UserRepository extends EntityRepository {
public function selectUsers (array $userIds) {
$dql = "
SELECT
u
FROM
MyBundle:User
WHERE
u.id IN (:users) OR :userCount = 0";
$query = $this
->getEntityManager()
->createQuery($dql)
->setParameter("users", $userIds)
->setParameter("userCount", count($userIds));
return $query->getResult();
}
}
However, I have a hard time believing that this is impossible to do directly in DQL.
Related
I'm using Symfony 4 with EasyAdmin and I need to make a select query that retrieve all the users, but I want the connected user to be selected as the first result.
I don't know how to proceed at all. I was thinking maybe do multiple select in the same query ? but I don't even know how to do this.
So far I have this :
$repo = $this->getDoctrine()->getRepository(User::class);
$authUser = $this->getUser();
$queryBuilder = $repo->createQueryBuilder('u');
return $queryBuilder;
// Doctrine createQueryBuilder looks like this
public function createQueryBuilder($alias, $indexBy = null)
{
return $this->_em->createQueryBuilder()->select($alias)->from($this->_entityName, $alias, $indexBy);
}
EDIT : I imperatively need to return the queryBuilder object, not the result set, that's why it's tricky.
An approach that will query all users, but gives you an array in the order you describe:
$users = $userRepository->findall();
foreach ($users as $user) {
$this->getUser() == $user ? $currentUser[] = $user : $otherUsers[] = $user;
}
$myUserList = array_merge($currentUser, $otherUsers);
Two sidenotes: 1: This queries all users and then splits them. I'm not sure if this could be what you want. 2: if no user is currently logged in this code will not work because $currentUser won't be defined. Of course, you can change it so it will also work when no user is logged in.
Edit based on the fact that returning the querybuilder is imperative:
You can use CASE to test and create a property, which you can hide with AS HIDDEN in your query and then order by this hidden property like so:
public function currentUserFirst($userId){
$qb = $this->createQueryBuilder('u')
->select('u, CASE WHEN u.id = :userId THEN 0 ELSE 1 END AS HIDDEN order')
->setParameter('userId', $userId)
->orderBy('order', 'ASC');
return $qb;
}
As the first comment pointed out, you will need to use a UNION, which is not supported in DQL.
Therefore your only option is to use a native query using the Connection which you need to get from the EntityManager.
It would look something like this:
$id = $this->getUser()->getId();
$sql = 'SELECT * FROM `users` WHERE `id` = :id UNION SELECT * FROM `users` WHERE`id` != :id'
$users = $this->getDoctrine()->getConnection()->fetchAll($sql, compact('id'));
You can use setMaxResults:
$qb->andWhere('q.start <= :start')
->setParameter(':start', $value)
->setMaxResults(1)
;
I'm about to output a list that includes several documents(called waiver). However not every user should be allowed to see all documents and therefore I've implemented an filter to check if the user has the same "airline" and "market" assigned. So every user should only see the documents that are assigned to his "airline" and "market".
This is f.e. the getter for the airline of the user entity:
/**
* Get airlines
*
* #return array
*/
public function getAirlines()
{
if($this->airlines != null)
{
$airlines = explode(",", $this->airlines);
return $airlines;
}
return Array();
}
This is the controller logic:
//Get User:
$user = $this->container->get('security.context')->getToken()->getUser();
// Gets an Array of User markets
$user_markets = $user->getMarkets();
// Gets an Array of User carriers
$user_airlines = $user->getAirlines();
if(!$this->ROLE_IS(Array( 'ROLE_XY'))){
$query = $em->createQuery(
'SELECT w
FROM WaiverBundle:Waiver w
WHERE w.carrier = :carrier
AND w.market = :market
ORDER BY w.id DESC'
)
->setFirstResult($page*10-10)
->setMaxResults(10)
// I wan't to get the whole array and not just one position here:
->setParameters(array(':carrier'=>$user_airlines[0],
':market'=>$user_markets[0],
));
}else{
$query = $em->createQuery(
'SELECT u
FROM WaiverBundle:Waiver u
ORDER BY u.id DESC'
)
->setFirstResult($page*10-10)
->setMaxResults(10);
}
Question: How do I manage to compare the DQL attributes with an array and not just a string as a parameter?
I think you want to use "IN" syntax, not "=" syntax:
'SELECT w
FROM WaiverBundle:Waiver w
WHERE w.carrier IN (:carrier)
AND w.market = :market
ORDER BY w.id DESC'
Your query is not complicated. I think you should consider QueryBuilder instead of DQL in this case. Something like this would do the trick:
$qb = $em->createQueryBuilder();
$qb->select('w')
->from('WaiverBundle:Waiver', 'w')
->where($qb->expr()->in('w.carrier', ':carrier'))
->andWhere($qb->expr()->eq('w.market', ':market'))
->orderBy('w.id', 'DESC')
->setParameters(
array(
'carrier'=>$user_airlines[0],
'market'=>$user_markets[0)
);
I want to get the field of an entity that is associated with another .
My entity Offers has a last_offer field.
Offers is related to the Products entity.
Then , with a consultation in my entity Products, I want to get the latest offer associated with the entity Offer.
Controller:
public function lastAction($key)
{
$em = $this->getDoctrine()->getManager();
$last_offer = $em->getRepository('MyAppBundle:Products')->findOfferByKey($key);
$response = new JsonResponse();
return $response->setData($last_offer);
}
My repository:
public function findOfferByKey($key){
$em = $this->getEntityManager();
$dql = 'SELECT pr, of FROM MyAppBundle\AppBundle\Entity\Products pr
INNER JOIN pr.offers of
WHERE pr.key = :key';
$query = $this->getEntityManager()
->createQuery($dql)
->setParameter('key', $key)
->setHydrationMode(\Doctrine\ORM\Query::HYDRATE_ARRAY);
return $query->execute();
}
My routing:
last_offer:
path: /{key}/last_offer
defaults: { _controller: "MyAppBundle:Products:last" }
But, this return an array.
I want to return only last_offer element.
Or to return the entity offers without being in an array.
You're specifically telling doctrine to generate/return an array with this line
->setHydrationMode(\Doctrine\ORM\Query::HYDRATE_ARRAY);
I think all you need to do is remove that line and doctrine will generate/return entities instead. However, you will get a Collection back, so make sure to take that into account. Perhaps something like
public function findOfferByKey($key) {
$dql = 'SELECT pr, of FROM MyAppBundle\AppBundle\Entity\Products pr
INNER JOIN pr.offers of
WHERE pr.key = :key';
$query = $this->getEntityManager()
->createQuery($dql)
->setParameter('key', $key)
;
$results = $query->execute();
if (count($results) !== 1)
{
// It's up to you how to handle zero or multiple rows
}
return $results->current();
}
EDIT
I see what's happening - I wasn't paying attention to your SELECT caluse. You're not selecting just the last_offer columns, you're selecting the entirety of the Products and Offers entities = $results is going to be an array of all of these together. In this scenario, $query->execute() will return an array() instead of a collection.
If you want to select just the Offer entities, you need to modify the SELECT
$dql = 'SELECT of
FROM MyAppBundle\AppBundle\Entity\Products pr
INNER JOIN pr.offers of
WHERE pr.key = :key';
But be wary that this still may return more than one row.
I have a couple of pretty complex queries, and for each of them I have to write a second query counting results. So for example, in the model:
$dql = "SELECT u FROM AcmeBundle:Users u LEFT JOIN AcmeBundle:Products p WITH u.id = p.id";
I would have to create a duplicate query like this:
$countingQuery = "SELECT COUNT(u.id) FROM AcmeBundle:Users u LEFT JOIN AcmeBundle:Products p WITH u.id = p.id";
The main problem with that is that with every change in the first query, I would have to change the second either.
So I came up with another idea:
$countingSelect = "SELECT COUNT(u.id)";
$noncountingSelect = "SELECT u";
$dql = " FROM AcmeBundle:Users u LEFT JOIN AcmeBundle:Products p WITH u.id = p.id";
return $this->getEntityManager()->createQuery($noncountingSelect . $dql)
->setHint('knp_paginator.count', $this->getEntityManager()->createQuery($countingSelect . $dql)->getSingleScalarResult());
It works of course, but the solution seems quite ugly with larger selects.
How can I solve this problem?
I believe the Doctrine\ORM\Tools\Pagination\Paginator will do what you're looking for, without the additional complexity.
$paginator = new Paginator($dql);
$paginator
->getQuery()
->setFirstResult($pageSize * ($currentPage - 1)) // set the offset
->setMaxResults($pageSize); // set the limit
$totalItems = count($paginator);
$pagesCount = ceil($totalItems / $paginator->getMaxResults());
Code yanked from: http://showmethecode.es/php/doctrine2/doctrine2-paginator/
You can create a customer repository as explained in the docs and add your query to that with a minor edit like..
use Doctrine\ORM\EntityRepository;
class ProductRepository extends EntityRepository
{
public function findProducts()
{
return $this->findProductsOrCountProducts();
}
public function findCountProducts()
{
return $this->findProductsOrCountProducts(true);
}
private function findProductsOrCountProducts($count = false)
{
$queryBuilder = $this->createQueryBuilder('u');
if ($count) {
$queryBuilder->select('COUNT(u.id)');
}
$query = $queryBuilder
->leftJoin('AcmeBundle:Products', 'p', 'WITH', 'u.id = p.id')
->getQuery()
;
if ($count) {
return $query->getSingleScalarResult();
} else {
return $query->getResult();
}
}
}
Then you can call your method using...
$repository = $this->getDoctrine()
->getRepository('AcmeBundle:Users');
// for products
$products = $repository->findProducts();
// for count
$countProducts = $repository->findCountProducts();
Note:
I know it's not best practice to just say look at the docs for the customer repository bit s here' the YAML mapping...
# src/Acme/StoreBundle/Resources/config/doctrine/Product.orm.yml
Acme\StoreBundle\Entity\Product:
type: entity
repositoryClass: Acme\StoreBundle\Entity\ProductRepository
# ...
The following code is issuing the following error message:
A Database Error Occurred
Error Number: 1066
Not unique table/alias: 'users'
SELECT * FROM (`users`, `users`) JOIN `user_profiles` ON `users`.`id` = `user_profiles`.`user_id`
Filename: /home/xtremer/public_html/kowmanager/models/cpanel/dashboard.php
Line Number: 38
Here is my code:
class Dashboard extends CI_Model {
private $table_name = 'users'; // user accounts
private $profile_table_name = 'user_profiles'; // user profiles
function __construct() {
parent::__construct();
$ci =& get_instance();
$this->table_name = $ci->config->item('db_table_prefix', 'tank_auth').$this->table_name;
$this->profile_table_name = $ci->config->item('db_table_prefix', 'tank_auth').$this->profile_table_name;
}
/**
* Get user info by Id
*
* #param int
* #param bool
* #return object
*/
function get_user_info($id) {
$this->db->select('*');
$this->db->from('users');
$this->db->join('user_profiles', 'users.id = user_profiles.user_id');
$query = $this->db->get($this->table_name);
if ($query->num_rows() == 1) {
return $data = $query->row();
} else {
return NULL;
}
}
}
EDIT: I know have this for my function and changed it because after looking at it I didn't need the extra table. I have an array ($data) of values being sent to my header but can I get away with sending it to the build that way it sends the array to all the partials.
function get_user_info($id)
{
$this->db->select('*');
$this->db->from('users');
$this->db->where('users.id', $id);
$query = $this->db->get();
if ($query->num_rows() == 1)
{
return $data = $query->row();
}
else
{
return NULL;
}
}
And this is from my controller I updated:
function index()
{
$id = $this->tank_auth->get_user_id();
$data = $this->Dashboard->get_user_info($id);
print_r($data);
$this->template->set_layout('cpanel')->enable_parser(false);
$this->template->set_partial('header', 'partials/header', $data);
$this->template->set_partial('sidebar', 'partials/sidebar');
$this->template->set_partial('content', 'partials/content');
$this->template->set_partial('footer', 'partials/footer');
$this->template->build('/cpanel/index');
}
The problem is this:
SELECT * FROM (users, users)
I assume you're trying to join the table to itself, but you can't do that without giving aliases to the tables, otherwise the database doesn't know which side of the join you're referring to when you reference fields from it.
Try this:
SELECT *
FROM users u1, users u2
JOIN user_profiles ON u1.id = user_profiles.user_id
Update:
My above answer was written based on the error message, but I didn't notice the code link (I've since added the code to be inline with the question).
I'm not overly familiar with CI, but I have a feeling your problem lies in these three lines:
private $table_name = 'users';
$this->db->from('users');
$query = $this->db->get($this->table_name);
In line 3, you're explicitly getting $this->table_name which points to the users table as shown in line 1). However, by explicitly setting a from table, also pointed to users, in line 2, I think you're accidentally setting up a join between two tables. Since these two tables are both the same, and neither is aliased, it is resulting in an error. Try removing the $this->db->from('users'); line and seeing if that resolves the issue.
Update 2:
I've been reading the CodeIgniter user guide, and it seems you don't need to specify anything as a parameter to $this->db->get() when you're using building a query using methods like from(). I'd suggest just changing this line:
$query = $this->db->get($this->table_name);
to this:
$query = $this->db->get();
See this page for details.
You are taking the Cartesian product of the users table with itself when you write users,users but not giving either of them an alias. I don't think you mean to be taking the Cartesian product at all. Try:
SELECT * FROM users JOIN user_profiles ON users.id = user_profiles.user_id