how i select some value equal array in Doctrine "createQueryBuilder" - symfony

can you help me with this code
There are several columns in the database, one of which is a role, user role value looks like this:
["ROLE_ADMIN"]
how can I get all users who have this role?
public function findUserWithRolle(){
$qb=$this->createQueryBuilder('R');
$qb->select('R.username')
->where('R.roles=["ROLE_ADMIN"]');
return $qb->getQuery()->getResult();
}

If use "direct LIKE-approach" you could get them like:
public function findUserWithRolle(string $role = 'ROLE_ADMIN'){
$qb=$this->createQueryBuilder('R');
$qb->select('R.username')
->andWhere('R.roles LIKE :role')
->setParameter('role', '%'.$role.'%');
return $qb->getQuery()->getResult();
}
Note: There are possible some pitfalls. With "LIKE-approach". But for your case enough.

Related

Doctrine OrderBy a PHP Function

I have an entity city that has an address attribute and a PHP function calculate($addess) that takes the city attribute address and returns a number.
I m trying to use the return of the function calculate() to order my cities. is it possible with Doctrine DQL? How can I do it?
Thanks in advance for any guidance
public function calculate($address)
{
//...
return $number;
}
I don't know if that exists but this is what I'm trying to do:
$qb
->select('u')
->from('City', 'u')
->orderBy(calculate($address), 'ASC')
;
Address needs to be a City attribute.
I don't know what calculate() logic does but couldn't you give it to your class as parameter? Than add adress_number to yor DB and mapping and simply orderBy this parameter:
$qb->select('u')
->from('City', 'u')
->orderBy('u.adressNumber', 'ASC');
Hope this helps. :)

How can i get current user's id to use it in a dql statement?

I know that this question was asked by many but no one was answered even :
This reponse.
is not a good reponse for how do i get current user's id into a dql statement in Repository.
i see so much talking about it's not logic or it's a bad choice so what 's the good one to use the current user's id like a param into a dql statement or QueryBuilder and this what i do but doesn't work :
My Controller :
public function GetGroupsByStudentAction()
{
$em=$this->getDoctrine()->getManager();
$modeles=$em->getRepository("AcmeMyBundle:GroupMatiere")
->findGroupByStudent();
return($this->render("AcmeMyBundle:GroupMatiere:list.html.twig",array("modeles"=>$modeles)));
}
public function ConnectedUserIdAction()
{ $user = $this->container->get('security.token_bag')->getToken()->getUser();
return $user->getId();
}
MyServices :
<service id="serviceGroupe" class="Acme\MyBundle\Controller\GroupMatiereController"/>
MyRepository :
public function findGroupByStudent()
{
// $currentid = i dont know how i call the methode from the service;
$query=$this->getEntityManager()
->createQuery("SELECT m from AcmeMyBundle:GroupMatiere m WHERE m.idGroupe=?1")
->setParameter(1, $currentid);
return $query->getResult();
}
That's work if i choose for the $currentid=1; But i need connected user id .
Thanks For Help and any other suggestion to change the logic i will be happy !
Define your repository method like this:
public function findGroupByStudent($student)
{
$query=$this->getEntityManager()
->createQuery("SELECT m from AcmeMyBundle:GroupMatiere m WHERE m.idGroupe=?1")
->setParameter(1, $student->getId());
return $query->getResult();
}
And then in the controller pass the Student entity that belongs to the currently logged in user, e.g.:
$modeles=$em->getRepository("AcmeMyBundle:GroupMatiere")
->findGroupByStudent($this->getUser()->getStudent());

Case-insensitive user-names in Symfony2

By default, Symfony2 matches usernames case-sensitively. I want users to be able to enter johnsmith, JohnSmith, johnSMITH, or any variant of that, and have them all register as johnsmith. How do I do this?
I though the easiest way would be to always convert each username to lower-case before comparing them. This is easy to do on the one side (just throw a lower() into the SQL statement), but how do I do that for what the user types in in the login form? Since Symfony automatically takes care of the login_check route handling, I can't figure it out.
Better yet, is there some option I can set to enable case-insensitivity instead?
Just write correct loadUserByUsername function in UserRepository.
It must not be case sensitive:
public function loadUserByUsername($username)
{
$user = $this->createQueryBuilder('u')
->where('LOWER(u.email) = :username')
->setParameter('username', strtolower($username))
->getQuery()
->getOneOrNullResult();
if ($user){
return $user;
}
throw new UsernameNotFoundException('Unable to find user "' . $username . '"');
}
You already fixed it, but I will explain another solution for users with similar problems:
You have to implement your own Provider this way: http://symfony.com/doc/current/cookbook/security/entity_provider.html#authenticating-someone-with-a-custom-entity-provider
Use the following query instead in method loadUserByUsername:
$user = $this->findOneBy(array('username' => strtolower($username)));
This worked for me. (Also in Doctrine Mongo ODM)
Did you try to convert the input into lowercase using CSS ? There are actually ways to control data input before it is handed to the login_check controller, but if you want a quick fix :
p {
text-transform: lowercase;
}
In your setUsername you could just have the text changed to lowercase like..
public function setUsername($username)
{
$this->username = mb_strtolower($username);
return $this;
}
For reference, FOSUserBundle handles this by "canonicalizing" the username (to usernameCanonical) and email address (to canonicalEmail) in the updateUser call (see Doctrine\UserManager that calls the Canonicalizer) which it then uses for searches.
I feel like an idiot. All I had to do was add another lower() around the where clause in my SQL statement. Like this:
select lower(USERNAME) as USERNAME, PASSWORD, ROLES, ALL_CUSTOMER_ACCESS
from COMPANYNAME_USERS
where lower(USERNAME) = lower(:username)

How to show symfony roles

I'm working with symfony 2.3. How I can show the user roles without (ROLE_). I want to change the view, but in the database are intact.
When I display the roles in the view I have this
ROLE_ADMIN
ROLE_CONSULTOR
and I want
Admin
Consultor
Role Entities just need to implement the RoleInterface. You can add your own custom fields such as the names you want.
http://api.symfony.com/2.3/Symfony/Component/Security/Core/Role/RoleInterface.html
Not clear what you want to achieve, but if there are few roles, may be 2-3, (e.g. ROLE_ADMIN, ROLE_CONSULTOR, ROLE_USER), just define a method which receives the system role, returns the human readable one. May be like this:
public function convertToHumanreadable($role)
{
$return = null;
switch ($role) {
case 'ROLE_ADMIN':
$return = 'Admin';
break;
...
}
return $return;
}
Or even like this:
public function convertToHumanreadable($role)
{
$roleParts = explode('_', $role)
return ucfirst(strtolower($roleParts[1]));
}
Or you can create your own role entity by implementing the RoleInterface as #DerickF mentioned.

Symfony2: How to create custom query methods on related entities

I have a User entity which has an ArrayCollection of Positions. Each Position has for sure a user_id property.
Now i want to get all positions from a user (to get all i would do $user->getPositions()) that are matching a specific query, for example have a date property that matches the current date. Therefor i want to do something like $user->getCurrentPositions() and it should return a subset of the positions related to that user.
How is that possible?
EDIT:
What i really wanna do is something like this in my controller:
$em = $this->getDoctrine()->getManager();
$users = $em->getRepository('fabianbartschWhereMyNomadsAtBundle:User')->findAll();
foreach ($users as $user) {
$positions = $user->getCurrentPositions();
foreach ($positions as $position) {
echo $position->getLatitude().'<br>';
}
}
I wanna iterate over all users and from each user i want to have the relevant positions. But that isnt possible from the repository i guess, as i get the following message: Attempted to call method "getCurrentPositions" on class ...
If you are using Doctrine you can use the built-in Criteria API which is meant for this purpose exactly.
Collections have a filtering API that allows you to slice parts of data from a collection. If the collection has not been loaded from the database yet, the filtering API can work on the SQL level to make optimized access to large collections.
Ok i found out, its for sure possible with Repositories:
Entity\User.php
/**
* #ORM\Entity(repositoryClass="fabianbartsch\WhereMyNomadsAtBundle\Entity\UserRepository")
* #ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
Entity\UserRepository.php
/**
* UserRepository
*/
class UserRepository extends EntityRepository
{
public function getCurrentPositions()
{
$query = $this->getEntityManager()
->createQuery(
"SELECT p
FROM xxx:Position p
WHERE p.start <= '2014-08-17' AND p.end >= '2014-08-17'"
);
try {
return $query->getResult();
} catch (\Doctrine\ORM\NoResultException $e) {
return null;
}
}
}
In the user object only related position entries are affected by the query, so is no need to join user entity with the position entity. Pretty simple, should just try out instead posting on stackoverflow, sry guys :P

Resources