Clone eloquent models for different queries - php

Sorry for modifying this question (to all those who have answered here). The question was in fact wrong.
$query = User::find(1)->books(); //books() is "return $this->hasMany('Book');"
$query_for_counting_history_books = clone $query;
$query_for_counting_geography_books = clone $query;
$number_of_history_books = $query_for_counting_history_books->where('type', '=', 'history')->count();
$number_of_geography_books = $query_for_counting_geography_books->where('type', '=', 'geography')->count();
$books = $query->paginate(10);
I want $books to be queried as:
SELECT * FROM books WHERE user_id=1 limit 10;
But its output is:
SELECT * FROM books WHERE user_id=1 and type="history" and type="geography"
limit 10;
Am I missing something about Clone here?
What should be done for this solution?

Not sure why your clone() is not working - but try this:
$query = User::where('confirmed', '=', '1');
$query_for_counting_male = $query->replicate();
$query_for_counting_female = $query->replicate();
http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Model.html#method_replicate

Since $query is instance of HasMany, So the cloning should be done as:
$query_for_counting_history_books = clone $query->getQuery();
$query_for_counting_geography_books = clone $query->getQuery();
http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Relations/HasOneOrMany.html#method_getQuery

Related

How can I generate this query in doctrine?

How can I generate this query in doctrine or query builder?
SELECT EndDate from helios.fsa_audits order by StartDate desc limit 1;
Any idea or Advice about how to make it?
Seeing your entity here, in your controller you can try this:
$em = $this->getDoctrine()->getManager();
$qb = $em->createQueryBuilder()
->select(array('a'))
->from(FsaAudits::class, 'a')
->orderBy("a.StartDate","DESC")
->setMaxResults(1);
$resultset = $qb->getQuery()->getResult();
if (count($resultset) <= 0) {
$fsaobj = $resultset[0];
echo $fsaobj->getEndDate();
}
See Working with query builder

How can I avoid Eloquent query to be chained?

I'm new to Laravel.
I wonder how I can avoid query to be chained.
$visitRecords = VisitRecord::whereDate('visited_at', '=', Carbon::today()->toDateString());
$knockBounce = $visitRecords->where("bounce_zone", "1")->get()->count();
$approachBounce = $visitRecords->where("bounce_zone", "2")->get()->count();
This is the code I wrote but this gives me the result I don't expect...
Result
select * from `visit_records` where date(`visited_at`) = '2017-05-12'
select * from `visit_records` where date(`visited_at`) = '2017-05-12' and `bounce_zone` = '1'
select * from `visit_records` where date(`visited_at`) = '2017-05-12' and `bounce_zone` = '1' and `bounce_zone` = '2'
I checked the query conducted and this is what I got.
What I expect...
select * from `visit_records` where date(`visited_at`) = '2017-05-12'
select * from `visit_records` where date(`visited_at`) = '2017-05-12' and `bounce_zone` = '1'
select * from `visit_records` where date(`visited_at`) = '2017-05-12' and `bounce_zone` = '2'
I want to conduct this query instead via Eloquent methods.
You've only instantiated a single QueryBuilder object.
You should create a second QueryBuilder object for the second query.
//Only create one carbon object
$date = Carbon::today()->toDateString()
$knockBounce = VisitRecord::whereDate('visited_at', '=', $date)->where("bounce_zone", "1")->count();
$approachBounce = VisitRecord::whereDate('visited_at', '=', $date)->where("bounce_zone", "2")->count();
Updated as per Matthew's comment, Laravel will execute a ->get() anyway for aggregate functions (count, min, max, avg) under the hood so it's not needed.
You need to have two different objects
you might try below code
$visitRecords = VisitRecord::whereDate('visited_at', '=', Carbon::today()->toDateString());
$visitRecords1 = clone $visitRecords;
$knockBounce = $visitRecords->where("bounce_zone", "1")->get()->count();
$approachBounce = $visitRecords1->where("bounce_zone", "2")->get()->count();
Here I have used clone to copy the $visitRecords object
Further reading about php clone
Edit to take only the needed bounce_zone
Alternatively, you can use collections :
$visitRecords = VisitRecord::whereDate('visited_at', '=', Carbon::today()->toDateString())->where("bounce_zone", "1")->orWhere("bounce_zone", "2")->get();
$knockBounce = $visitRecords->where("bounce_zone", "1")->count();
$approachBounce = $visitRecords->where("bounce_zone", "2")->count();
You run only one query and use all the power of Laravel :)

symfony2 query builder with multiple databases (doctrine)

I want join the abteilung table which is in another database with other access data.
Here is the right database for abteilung:
$manager = $this->getDoctrine()->getManager('olddb')->getRepository('ChrisOldUserBundle:BpDepartment');
And this is the old query i want to change:
$result = $this->getDoctrine()->getRepository('KfzBuchungBundle:Rent')
->createQueryBuilder('r')
->addSelect('abteilung')
->addSelect('auto')
->join('r.auto','auto')
->join('r.abteilung','abteilung')
->where('r.mieteStart >= :date_from')
->andWhere('r.mieteEnde <= :date_to')
->setParameter('date_from', $date_from)
->setParameter('date_to', $date_to)
->orderBy('r.mieteStart', 'ASC')
->distinct()
->getQuery()->getArrayResult();
I tried with:
$rsm = new ResultSetMapping();
$rsm->addEntityResult('Chris\KfzBuchungBundle\Entity\Rent', 'bp');
$rsm->addEntityResult('Chris\Bundle\OldUserBundle\Entity\BpDepartment', 'bp_dpt');
$rsm->addFieldResult('bp','id','id');
$query = $this->getDoctrine()->getManager()->createNativeQuery('SELECT * FROM bp_department bp_dpt', $rsm);
$result = $query->getResult();
But same shit, i have no idea.

Laravel Query Builder use variables in MySQL for complex select?

How to do this with one mysql request:
$revision = $this->Revision->where('batch', $batch)->first();
$revisions = $this->Revision->where('batch','>', $batch)
->where('revisionable_type', $revision->revisionable_type)
->where('revisionable_id', $revision->revisionable_id)
->get();
$this->Revision = eloquent model;
others are just columns.
I misunderstood your question initially. You can do this by using:
$sql = "SELECT * FROM revisions AS tmpa
INNER JOIN revisions AS tmpb
ON tmpb.revisionable_type = tmpa.revisionable_type
AND tmpb.revisionable_id = tmpa.revisionable_id
WHERE tmpa.batch = '$batch'";
DB::select(DB::raw($sql));

Using subqueries with doctrine / Errors with aliases

I'm trying to make a simple query with a subquery in a orWhere clause (with Doctrine).
As always, Doctrine tries to rename every aliases and completely destroys the queries...
Here's an example:
$q = Doctrine_Query::create()
->from('Actualite a')
->where('a.categorie_id =?', $id)
->orWhere('a.categorie_id IN (select id from cat.categorie as cat where cat.categorie_id =?)', $id)
->execute();
Which in MySQL would make something like:
SELECT *
FROM actualite a
WHERE a.categorie_id = 1 OR a.categorie_id IN (SELECT cat.id FROM categorie cat WHERE cat.categorie_id = 1);
Everything is right about it, but then again Doctrine destroys it:
Couldn't find class cat
Every time I try to do something a little complex with Doctrine, I have errors with aliases. Any advice or ideas about how to fix this?
Thanks!
The SQL example you've provided is fine but the corresponding Doctrine syntax has a couple of errors. Here's a clean version:
$q = Doctrine_Query::create()
->select('a.*')
->from('Actualite a')
->where('a.categorie_id = ?', $id)
->orWhere('a.categorie_id IN (SELECT cat.id FROM Categorie cat WHERE cat.categorie_id = ?)', $id)
->execute();
You should use createSubquery() to explicitely tell doctrine about your nested subquery. So your query should look something like this:
$q = Doctrine_Query::create()
->select('a.*')
->from('Actualite a')
->where('a.categorie_id = ?', $id)
;
$subquery = $q->createSubquery()
->select("cat.id")
->from("Categorie cat")
->where("cat.categorie_id = ?", $id)
;
$q->orWhere('a.categorie_id IN ('.$subquery->getDql().')')->execute();
Another example can be found here:
http://www.philipphoffmann.de/2012/08/taming-doctrine-subqueries/
I think you query should be like this add the select and remove as
$q = Doctrine_Query::create()
->select('a.id')
->from('Actualite a')
->where('a.categorie_id =?', $id)
->orWhere('a.categorie_id IN (select id from cat.categorie cat where cat.categorie_id =?)', $id)
->execute();
Try this may help you.
Thanks

Categories