Doctrine2 findOneBy argument equivalent to BETWEEN in SQL - symfony

I need to be able to use the following WHERE clause in Doctrine:
WHERE AL.UserID = 41 AND (TheDate BETWEEN DATE_SUB(CURDATE(), INTERVAL 2 WEEK) AND CURDATE())
and what I'm currently doing is:
$results = $em->getRepository('MyBundle:MyTable')->findOneBy(array('userId' => $userId));
However, I haven't been able to filter results by the last 2 weeks without writing SQL or DQL.
Is there any way I can achieve this through Doctrine's methods?

You can use QueryBuilder. Assuming you are using MySQL BETWEEN, something like:
<?php
// $em instanceof EntityManager
$qb = $em->createQueryBuilder();
$now = new DateTime();
$qb->select('c')
->from('MyClass', 'c')
->where($qb->expr()->andX(
$qb->expr()->eq('c.UserID', '?1'),
$qb->expr()->gte('c.TheDate', '?2'),
$qb->expr()->lte('c.TheDate', '?3')
))
->setParameter(1, 41)
->setParameter(2, $now.sub(new DateInterval('P2W')))
->setParameter(3, $now);
$query = $qb->getQuery();
$c = $query->getSingleResult();
This is cleaner code and should work across the different database platforms supported by Doctrine2 ORM.

Related

Symfony QueryBuilder andWhere for User-Relation not Working

I want to load items for a given User. Between Item and User lives a One-To-Many relationship.
If I use the following, everything is working well:
$result = $em→getRepository(Item::class)→findBy(['user' => $user]);
The $result has 3 entries.
Now I have to give additional conditions to the query. So I want to use the queryBuilder insight the repository:
$result = $this->createQueryBuilder('i')
->andWhere('i.user = :user')
->setParameter('user', $user)
->getQuery()
->getResult();
The result is empty but the query runs without any errors.
Can someone tell me what I'm doing wrong?
In both cases the $user is the related App\Entity\User entity.
I found the solution. It didn't work because of the Uuid. This works:
$result = $this->createQueryBuilder('i')
->andWhere('i.user = :user')
->setParameter('user', $user->getId()->toBinary())
->getQuery()
->getResult();

doctrine how to fetch database switch a datetime

I can't find the answer in the documentation.
I use symfony for an application, i have a database with a datetime for each of my table and would like to get only the row with the most recent datetime is there a function like $repository->findOneById($id) to obtain this object ?
Here is a solution using QueryBuilder:
$repository = $this->getDoctrine()
->getRepository('AcmeStoreBundle:Product');
$query = $repository->createQueryBuilder('p')
->orderBy('p.datetime', 'DESC')
->setMaxResults(1)
->getQuery();
$product = $query->getResult();
Regards.

How to write DQL query innner join to avoid lazy loading?

My code:
$entityManager = $this->getDoctrine()->getEntityManager();
$result = $entityManager->createQueryBuilder()
->select('c')
->from('BlogHomepageBundle:Comment', 'c')
->innerJoin('BlogHomepageBundle:Post', 'po', 'WITH', 'c.postFk = po.postId')
->getQuery()->getResult();
foreach ($result as $c) {
//additional sql query
echo $c->getPostFk()->getName();
}
It works just fine, but when Im trying to get postFk ( post relate to comment one-to-many relation) I'm getting additional query to db ( lazy loading). Can I avoid this situtation to get all data in one DQL query ? Simple to performe sql query
select c.*, p.* from comment as c inner join post as p on c.post_fk = p.post_id
I want to use DQL ( not raw sql ).
EDIT
I would like to have Objects and access to them like in foreach loop / not scalar data.
You need to specifically select a property or a join to avoid lazy loading :
->select('c', 'po')
http://www.doctrine-project.org/blog/doctrine-lazy-loading.html
I think I found solution #Rpg600 thanks for help.
This is code:
$entityManager = $this->getDoctrine()->getEntityManager();
$result = $entityManager->createQueryBuilder()
->select('c', 'po')
->from('BlogHomepageBundle:Comment', 'c')
->innerJoin('c.postFk', 'po', 'WITH', 'c.postFk = po.postId')
->getQuery()
->getResult();
foreach ($result as $c) {
echo $c->getContent();
echo $c->getPostFk()->getName();
}

Is there possible to use createQueryBuilder for insert/update? If not, what function should I use?

For now I succeded to create a function that retrieves data from the database using Doctrine's function createQueryBuilder.
Does anybody know if there is a similar function to insert or update the database? Or how can i use createQueryBuilder?
Doctrine 2 ORM does not support INSERT via DQL or the DQL query builder. For a complete syntax, check the EBNF of DQL.
To handle inserts in ORM, you always manually instantiate an entity and persist it with the entity manager:
$user = new \My\Entity\User();
$entityManager->persist($user);
$entityManager->flush();
You can only handle SELECT, UPDATE and DELETE via DQL in Doctrine ORM:
Select:
SELECT u FROM My\Entity\User u WHERE u.id = :userId
Update:
UPDATE My\Entity\User u SET u.status = 'banned' WHERE u.id = :userId
Delete
DELETE My\Entity\User u WHERE u.id = :userId
You can handle these operations with the QueryBuilder as well:
Select:
$queryBuilder = $entityManager->createQueryBuilder();
$queryBuilder
->select('u')
->from('My\Entity\User', 'u')
->where($queryBuilder->expr()->eq('u.id', ':userId'));
Delete:
$queryBuilder = $entityManager->createQueryBuilder();
$queryBuilder
->delete('My\Entity\User', 'u')
->where($queryBuilder->expr()->eq('u.id', ':userId'));
Update:
$queryBuilder = $entityManager->createQueryBuilder();
$queryBuilder
->update('My\Entity\User', 'u')
->set('u.status', 'banned')
->where($queryBuilder->expr()->eq('u.id', ':userId'));
Another option you have instead using a QueryBuilder, is using Doctrine DBAL prepare and execute functions. Probably is not as flexible as use QueryBuilder, but for do INSERTs in some situations could be useful.
The way to use is getting the Database Connection from the Entity Manager.
$sql = "INSERT INTO table (field1, field2) VALUES ('foo', 'var')";
$stmt = $em->getConnection()->prepare($sql);
$stmt->bindValue(':invoice', $invoiceId);
$result = $stmt->execute();
If you use DBAL queryBuilder, it is possible to insert.
$qb = $connection->createQueryBuilder();
To insert with the queryBuilder it is like this :
$qb->insert('MuBundle:MyClass', 'momc')
->values (array(
'property1 (id for example)' => '?'
'property2 (name for exmaple)' => '?'
))
->setParameter(0, $id)
->setparameter(1, $name)
using QueryBuilder to insert data is not possible unless your are willing to write the DQL or SQL. If you are looking for a way to simply insert data to your database table, you have to first of all make sure the data is loaded into an Entity class for the table in which you want to insert your data. For example $em->persist($entity);

Use Limit and Offset in Doctrine2 query

I'm trying to do the pagination, but there is an error:
[Syntax Error] line 0, col 57: Error: Expected end of string, got 'limit'
I'm not quite sure if this is the right syntax (and logic) to make my query:
public function getFriendsFromTo ($user, $limit, $offset)
{
return $this->getEntityManager()
->createQuery('SELECT f FROM EMMyFriendsBundle:Friend f WHERE f.user='.$user.' limit '.$limit. 'offset' .$offset)
->getResult();
}
Friends and users are related manyToOne and oneToMany, so in the friends table there is a field - user_id.
This is in my controller:
$user = $this->get('security.context')->getToken()->getUser();
$id = $user->getId();
$friends = $user->getFriends();
$result = count($friends)
$FR_PER_PAGE = 7;
$pages = $result/$FR_PER_PAGE;
$em = $this->getDoctrine()->getEntityManager();
$friends = $em->getRepository('EMMyFriendsBundle:Friend')
->getFriendsFromTo($id, $FR_PER_PAGE, $page*$FR_PER_PAGE);
I know that it's stupid and even wrong (especially the third parameter to be $page*$FR_PER_PAGE), but I just wanted to try if the query works, and it didn't.
Nope. Use:
return $this->getEntityManager()
->createQuery('...')
->setMaxResults(5)
->setFirstResult(10)
->getResult();
$towary = $this->getDoctrine()
->getRepository('AcmeStoreBundle:Towar')
->findBy(array(),array(),10,($current-1)*$numItemsPerPage);
You can use findBy 3rd and 4th parameters of method of doctrine repository, which are limit and offset.
Here is the method definition:
findBy(
array $criteria,
array $orderBy = null,
integer|null $limit = null,
integer|null $offset = null
)
Source: http://www.doctrine-project.org/api/orm/2.2/class-Doctrine.ORM.EntityRepository.html
you can also use
$query->getSingleResult();
Doctrine2.6, stumbled upon this old post and tried the DQL way but it did not fit for purpose. So if you want to avoid using DQL because you already have Entities mapped and joined together, you can do paging using matching & Criteria
$criteria = Criteria::create()
->setMaxResults($limit ? $limit : null)
->setFirstResult($offset ? $offset : null)
$result = $em->getRepository('EMMyFriendsBundle:Friend')
->matching($criteria)->toArray();

Resources