Get the last value of a entity field with condition - php

I'm making a website for performance monitoring.
I have 2 tables:
Users table linked in one-to-Many with performances
Performances table linked in Many-to-one with users table
I just wanna get the last weight which is not null in my table performances and display it in twig
database screenshot
For exemple in this database, the result would be : 80
I tried with queries but I get an arror message so I don't know how to do
Thanks in advance for your help !

I hope this could helps you.
I suppose you have a repository class for your Performance entity. I would use this kind of code for a Symfony3 application
<?php
namespace AppBundle\Repository; //replace AppBundle by the name of your bundle
use Doctrine\ORM\Tools\Pagination\Paginator;
/**
* PerformanceRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class PerformanceRepository extends \Doctrine\ORM\EntityRepository
{
public function getLastWeigth()
{
$qb = $this->getEntityManager()->createQueryBuilder()
->select('p')
->from($this->_entityName, 'p')
->expr()->isNotNull('p.weight')
->orderBy('p.date', 'desc')
->setMaxResults(1);
$query = $qb->getQuery();
$result = $query->getSingleResult();
return $result;
}
}
Edit: here is an Exemple of use in the controller in a Symfony 3 application:
<?php
namespace AppBundle\Controller\Performance;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use AppBundle\Repository\PerformanceRepository;
class PerformanceController extends Controller
{
public function lastWeightAction()
{
$repo = $this->getDoctrine()->getManager()->getRepository('AppBundle:Performance');
$lastPerformance = $repo->getLastWeigth();
//some other code
}
}
Edit2:
If you need to get the last weight by a user Id:
class PerformanceRepository extends \Doctrine\ORM\EntityRepository
{
public function getLastWeigth($userId)
{
$qb = $this->getEntityManager()->createQueryBuilder()
->select('p')
->from($this->_entityName, 'p')
->join('p.user', 'u')
->where('u.id = :userId')
->expr()->isNotNull('p.weight')
->orderBy('p.date', 'desc')
->setMaxResults(1);
->setParameter(':userId', $userId);
$query = $qb->getQuery();
$result = $query->getSingleResult();
return $result;
}
}

Related

Symfony3 - create join query of two tables in controller

How to show two tables data from my controller.
Here is my controller's code.
class TestController extends Controller
{
public function showAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$teacher = $this->getDoctrine()->getRepository(Teacher::class);
$query = $em
->createQueryBuilder('t')
->from('AppBundle:Teacher','t')
->Join('AppBundle:Student','s')
->where('t.id=id and s.tid=tid')
->getQuery()
->getResult();
}
}
When print_r it's showing only one table data.
Please help
Please check below mentioned solution.
$query = $em
->createQueryBuilder('t.*,s.*')
->from('AppBundle:Teacher','t')
->Join('AppBundle:Student','s')
->where('t.id=id and s.tid=tid')
->getQuery()
->getResult();
}
Let me know if it not works.
I assume that you have defined a relationship between Teacher and Student in your entities. In this case you can get the Student objects by calling $teacher->getStudents() (assuming that you have defined such a method in your Teacher entity class). See Doctrine documentation about association mapping
Example for a One-To-Many relationship:
<?php
use Doctrine\Common\Collections\ArrayCollection;
/** #Entity */
class Teacher
{
// ...
/**
* One Teacher has Many Students.
* #OneToMany(targetEntity="Student", mappedBy="teacher")
*/
private $students;
// ...
public function __construct() {
$this->students = new ArrayCollection();
}
}
/** #Entity */
class Student
{
// ...
/**
* Many Students have One Teacher.
* #ManyToOne(targetEntity="Teacher", inversedBy="students")
* #JoinColumn(name="teacher_id", referencedColumnName="id")
*/
private $teacher;
// ...
}
In the QueryBuilder object you can avoid the need of additional queries on $teacher->getStudents() calls by adding something like that:
$query = $em
->createQueryBuilder('t')
->from('AppBundle:Teacher','t')
->join('AppBundle:Student','s')
->select(array('t', 's'))
->where('t.id=id and s.tid=tid')
->getQuery()
->getResult();
}
If there is a relationship defined between Teacher and Student in your entities as mentioned above you can even simplify the join:
$query = $em
->createQueryBuilder('t')
->from('AppBundle:Teacher','t')
->join('t.students', 's')
->select(array('t', 's'))
->getQuery()
->getResult();
}
Furthmore you do not need to call the from() method if you create the QueryBuilder object via the TeacherRepository object:
$query = $teacher
->createQueryBuilder('t')
->join('t.students', 's')
->select(array('t', 's'))
->getQuery()
->getResult();
}
$query = $em
->createQueryBuilder('t')
->add('select', 't,s')
->from('AppBundle:Teacher', 't')
->Join('AppBundle:Student', 's')
->where('t.id = s.tid')
->getQuery()
->getResult();
it working perfect.
First we select all from Teachers table, then join students. Assume that your relationship name in Teachers model is student. In repository file:
public function getWithStudents() {
return $this->createQueryBuilder('t')
->Join('t.student', 's')
->addSelect('s')
->getQuery()->getArrayResult();
}
Then in controller call it:
$teachersWithStudents = $teacher->getWithStudents();
Or in this case you can just
$teachersWithStudents = $teacher->getStudents();
Suppose you have two tables.comment table and article table and You want to fetch comments on each article
$commentContent = $em
// automatically knows to select Comment
// the "c" is an alias you'll use in the rest of the query
->createQueryBuilder('c')
->select('c.message, c.name')////Fields required to display
->from('AppBundle:Comment','c')
->join('AppBundle:Article','a')
->where('c.idArticle=a.id and c.publish_mainPage = 1')
->orderBy('c.idArticle', 'DESC')
->getQuery()
->getResult();
var_dump($commentContent);

Symfony doctrine DQL random result in Query with MaxResult

How can I get a random result with an dql Query?
This is my query:
$firstCategoryId = 50;
$repository = $this->entityManager->getRepository(BaseProduct::class);
$products = $repository->createQueryBuilder('p')
->join('p.categories', 'c')
->where('c.id = :categoryId')
->setParameter('categoryId', $firstCategoryId)
->getQuery()
->setMaxResults(4)
->getResult();
This returns me always the first 4 products.
Lets say the category with ID 50 has over 100 products. And what I want is querying randomly 4 articles from category with ID 50. But how? Is this possible? Of course I can set no Max Result and than do it with PHP... but this is not a good solution because of performance.
You need to create dql function for that. https://gist.github.com/Ocramius/919465 you can check that.
namespace Acme\Bundle\DQL;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
class RandFunction extends FunctionNode
{
public function parse(Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
public function getSql(SqlWalker $sqlWalker)
{
return 'RAND()';
}
}
After that open your config.yml file and add autoload that RandFunction.
orm:
dql:
numeric_functions:
Rand: Acme\Bundle\DQL\RandFunction
And your query must be like:
$firstCategoryId = 50;
$repository = $this->entityManager->getRepository(BaseProduct::class);
$products = $repository->createQueryBuilder('p')
->join('p.categories', 'c')
->addSelect('RAND() as HIDDEN rand')
->where('c.id = :categoryId')
->orderBy('rand')
->setParameter('categoryId', $firstCategoryId)
->getQuery()
->setMaxResults(4)
->getResult();

Attempted to call method "createQueryBuilder" on class

I have an entity "Vehicules" that have ManyToOne relation with the entity "User". So each User hav one or more vehicules. I'm trying to count the number of vehicules foreach user and trying to show it in a table .
this is a part of my entity Vehicule
/**
* #ORM\ManyToOne(targetEntity="OC\UserBundle\Entity\User")
* #ORM\JoinColumn(name="User_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $direction;
this is the function in which i want to count the number of vehicules foreach user (direction) and show it in a table
public function afficheAction() {
$em = $this->getDoctrine();
$demandes = $em->getRepository('DemandevehBundle:Demande')->findAll();
// trying to count the number of vehicules foreach direction
$vehicules = $this->getDoctrine()->getRepository('CarPfeBundle:Vehicule')->findAll();
foreach($vehicules as $vehicule) {
$qb = $vehicule->createQueryBuilder('v');
$qb->select('count(v.id)');
$qb->where('v.direction = ?1');
$qb->setParameter('1', $vehicule->getId());
$query = $qb->getQuery();
$result = $query->getSingleScalarResult();
return $result;
}
// fin
return $this->render('DemandevehBundle:Demande:affiche.html.twig', array('demandes' => $demandes
,'result' => $result));
}
I get this error
Attempted to call method "createQueryBuilder" on class "Car\PfeBundle\Entity\Vehicule".
I feel that my function make no sense that's why i get this error. Any help please ?
Is possible to create a query builder on an entity manager instead of an entity. So you use the relative manager of the class so try this:
$qb = $this->getDoctrine()
->getRepository('CarPfeBundle:Vehicule')
->createQueryBuilder('v');
instead of this:
$qb = $vehicule->createQueryBuilder('v');
Hope this help

Laravel where on relationship object

I'm developing a web API with Laravel 5.0 but I'm not sure about a specific query I'm trying to build.
My classes are as follows:
class Event extends Model {
protected $table = 'events';
public $timestamps = false;
public function participants()
{
return $this->hasMany('App\Participant', 'IDEvent', 'ID');
}
public function owner()
{
return $this->hasOne('App\User', 'ID', 'IDOwner');
}
}
and
class Participant extends Model {
protected $table = 'participants';
public $timestamps = false;
public function user()
{
return $this->belongTo('App\User', 'IDUser', 'ID');
}
public function event()
{
return $this->belongTo('App\Event', 'IDEvent', 'ID');
}
}
Now, I want to get all the events with a specific participant.
I tried with:
Event::with('participants')->where('IDUser', 1)->get();
but the where condition is applied on the Event and not on its Participants. The following gives me an exception:
Participant::where('IDUser', 1)->event()->get();
I know that I can write this:
$list = Participant::where('IDUser', 1)->get();
for($item in $list) {
$event = $item->event;
// ... other code ...
}
but it doesn't seem very efficient to send so many queries to the server.
What is the best way to perform a where through a model relationship using Laravel 5 and Eloquent?
The correct syntax to do this on your relations is:
Event::whereHas('participants', function ($query) {
return $query->where('IDUser', '=', 1);
})->get();
This will return Events where Participants have a user ID of 1. If the Participant doesn't have a user ID of 1, the Event will NOT be returned.
Read more at https://laravel.com/docs/5.8/eloquent-relationships#eager-loading
#Cermbo's answer is not related to this question. In that answer, Laravel will give you all Events if each Event has 'participants' with IdUser of 1.
But if you want to get all Events with all 'participants' provided that all 'participants' have a IdUser of 1, then you should do something like this :
Event::with(["participants" => function($q){
$q->where('participants.IdUser', '=', 1);
}])
N.B:
In where use your table name, not Model name.
for laravel 8.57+
Event::whereRelation('participants', 'IDUser', '=', 1)->get();
With multiple joins, use something like this code:
$userId = 44;
Event::with(["owner", "participants" => function($q) use($userId ){
$q->where('participants.IdUser', '=', 1);
//$q->where('some other field', $userId );
}])
Use this code:
return Deal::with(["redeem" => function($q){
$q->where('user_id', '=', 1);
}])->get();
for laravel 8 use this instead
Event::whereHas('participants', function ($query) {
$query->where('user_id', '=', 1);
})->get();
this will return events that only with partcipats with user id 1 with that event relastionship,
I created a custom query scope in BaseModel (my all models extends this class):
/**
* Add a relationship exists condition (BelongsTo).
*
* #param Builder $query
* #param string|Model $relation Relation string name or you can try pass directly model and method will try guess relationship
* #param mixed $modelOrKey
* #return Builder|static
*/
public function scopeWhereHasRelated(Builder $query, $relation, $modelOrKey = null)
{
if ($relation instanceof Model && $modelOrKey === null) {
$modelOrKey = $relation;
$relation = Str::camel(class_basename($relation));
}
return $query->whereHas($relation, static function (Builder $query) use ($modelOrKey) {
return $query->whereKey($modelOrKey instanceof Model ? $modelOrKey->getKey() : $modelOrKey);
});
}
You can use it in many contexts for example:
Event::whereHasRelated('participants', 1)->isNotEmpty(); // where has participant with id = 1
Furthermore, you can try to omit relationship name and pass just model:
$participant = Participant::find(1);
Event::whereHasRelated($participant)->first(); // guess relationship based on class name and get id from model instance
[OOT]
A bit OOT, but this question is the most closest topic with my question.
Here is an example if you want to show Event where ALL participant meet certain requirement. Let's say, event where ALL the participant has fully paid. So, it WILL NOT return events which having one or more participants that haven't fully paid .
Simply use the whereDoesntHave of the others 2 statuses.
Let's say the statuses are haven't paid at all [eq:1], paid some of it [eq:2], and fully paid [eq:3]
Event::whereDoesntHave('participants', function ($query) {
return $query->whereRaw('payment = 1 or payment = 2');
})->get();
Tested on Laravel 5.8 - 7.x

Doctrine : how to manipulate a collection?

With symfony && doctrine 1.2 in an action, i try to display the top ranked website for a user.
I did :
public function executeShow(sfWebRequest $request)
{
$this->user = $this->getRoute()->getObject();
$this->websites = $this->user->Websites;
}
The only problem is that it returns a Doctrine collection with all the websites in it and not only the Top ranked ones.
I already setup a method (getTopRanked()) but if I do :
$this->user->Websites->getTopRanked()
It fails.
If anyone has an idea to alter the Doctrine collection to filter only the top ranked.
Thanks
PS: my method looks like (in websiteTable.class.php) :
public function getTopRanked()
{
$q = Doctrine_Query::create()
->from('Website')
->orderBy('nb_votes DESC')
->limit(5);
return $q->execute();
}
I'd rather pass Doctrine_Query between methods:
//action
public function executeShow(sfWebRequest $request)
{
$this->user = $this->getRoute()->getObject();
$this->websites = $this->getUser()->getWebsites(true);
}
//user
public function getWebsites($top_ranked = false)
{
$q = Doctrine_Query::create()
->from('Website w')
->where('w.user_id = ?', $this->getId());
if ($top_ranked)
{
$q = Doctrine::getTable('Website')->addTopRankedQuery($q);
}
return $q->execute();
}
//WebsiteTable
public function addTopRankedQuery(Doctrine_Query $q)
{
$alias = $q->getRootAlias();
$q->orderBy($alias'.nb_votes DESC')
->limit(5)
return $q
}
If getTopRanked() is a method in your user model, then you would access it with $this->user->getTopRanked()
In your case $this->user->Websites contains ALL user websites. As far as I know there's no way to filter existing doctrine collection (unless you will iterate through it and choose interesting elements).
I'd simply implement getTopRankedWebsites() method in the User class:
class User extends BaseUser
{
public function getTopRankedWebsites()
{
WebsiteTable::getTopRankedByUserId($this->getId());
}
}
And add appropriate query in the WebsiteTable:
class WebsiteTable extends Doctrine_Table
{
public function getTopRankedByUserId($userId)
{
return Doctrine_Query::create()
->from('Website w')
->where('w.user_id = ?', array($userId))
->orderBy('w.nb_votes DESC')
->limit(5)
->execute();
}
}
You can also use the getFirst() function
$this->user->Websites->getTopRanked()->getFirst()
http://www.doctrine-project.org/api/orm/1.2/doctrine/doctrine_collection.html#getFirst()

Categories