condition in order by clause in cakephp...? - php

SELECT * FROM `properties` order by (`queue_type` = 'update') desc;
how can we run this query using cakephp using pagination, I need to sort my properties according to the queue type. option in queue type will be update, folio , verification , new and all.
My query is running perfectly in mysql but I need to do it in cakephp but not need to use query function of cakephp.
Please help me quickly.

Related

how to convert simple mysql query to doctrine Query language?

Convert mysql Query to doctrine Query language ?
I have done the mysql and run the query in phpmyadmin and I'm getting returned rows :
select ppp.payperiod_sdesc,ppesa.gross_pay,pptpp.esi_employer_contribution,pptpp.pf_employer_contribution,pplw.employerContribution
from py_process_emp_status_approved AS ppesa
left join py_process_tds_pf_pt AS pptpp on ppesa.ou_code = pptpp.ou_code
left join py_pay_group AS ppg on pptpp.pg_code = ppg.pg_code
left join py_process_labour_welfare AS pplw on ppg.pg_code = pplw.pg_code
left join py_pay_period AS ppp on pplw.payperiod_code = ppp.payperiod_code
left join py_payroll_calendar AS ppc on ppp.paycal_code = ppc.paycal_code
WHERE ppesa.ou_code = 15000
ORDER BY ppesa.ou_code DESC
LIMIT 0,5
please convert the mysql query to doctrine Query language.
You may be able to find more guidance in the docs here:
https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/native-sql.html
Have a look at result set mapping. You can also just create a repository function to execute native sql and then call your repo function.
You can then call something like this below:
results = getDoctrine()->getManager()->getRepository(Your repo)->repofunction();
If your unsure how to create / call a repository function its covered multiple times in the symfony docs.
Doctrine loads all results then filters down, this is an expensive process you may be best letting sql do the work for you.
Hopefully you have everything you need to get to your end result.

What is the dafault ordering in Laravel Eloquent Model::all() function?

In the Laravel Eloquent Model::all() function, what is the default ordering for the returned query? I ask this because I'm pretty sure it's in ascending order by primary key which defaults do 'id' when you make the model through
php artisan make:model Model -m
However, when I call it like this:
return $users = User::all();
I get the following results in the browser:
The results seem to be in no particular order by any of the attributes.
I am fully aware I can order them by id by doing
return $users = User::orderBy('id', 'asc')->get();
But just a few days ago they were being ordered automatically. What gives?
The default sort order for laravel is simply nothing. It does not apply a default "ORDER BY" clause, which means that it follows PostgreSQL rules for unordered results. From some similar answers here and here:
Do not depend on order when ORDER BY is missing.
Always specify ORDER BY if you want a particular order -- in some situations the engine can eliminate the ORDER BY because of how it
does some other step.
GROUP BY forces ORDER BY. (This is a violation of the standard. It can be avoided by using ORDER BY NULL.)
SELECT * FROM tbl -- this will do a "table scan". If the table has
never had any DELETEs/REPLACEs/UPDATEs, the records will happen to be
in the insertion order, hence what you observed.
If you had done the same statement with an InnoDB table, they would
have been delivered in PRIMARY KEY order, not INSERT order. Again,
this is an artifact of the underlying implementation, not something to
depend on.
With a great agreement with FrankerZ's answer, I would like to add, whenever you wonder what query laravel is structuring under the ORM, DB listener will be quickest option to use :
<?php
\DB::listen(function($sql) {
dump($sql);
});
W.r.t. this question it would give select * from users when you do User::all().
Note: Check listen function as per the laravel version you are using, the parameter count are different in older and newer versions.
Reference : Documentation Link

Doctrine query builder order by field

Can anyone tell me the similar query in Doctrine to get in the order we pass inside IN() function.
SELECT * FROM user
WHERE id IN (5,2,3,1,4)
ORDER BY FIELD(id,5,2,3,1,4)
It doesn't seems like you can use the FIELD function with Doctrine. But you can still use the Doctrine 2 extension project which add support for additional query functions.

Phalcon - How do i do a SELECT IN Subquery with Phalcon models?

I need to know how to do i do a subquery type selection with phalcon models?
for example i want to select all the users who viewed me, they are stored in the UserView table with columns 'id','user_from','user_to' (mapped by User table user_id to either user_from or user_to)
so i want to select all the users who has a user_to as with the current user, and group by user_to make sure i only get one recorded, I wrote below function to do this but there is fundamental two problems
1. Is how to do sub-query using phalcon models
2. Is my logic correctly applied on the back-end of the DB (as i cant see real executed query)
public function getUserWithViewedMe($limit=1000000){
return User::query()
->rightJoin("XYZ\Models\UsersView")
->andWhere(" XYZ\Models\UsersView.user_from IN :user_id: ",
array('user_id' => $this->user->user_id) )
->group('user_id')
->order("XYZ\Models\UsersView.id DESC ")
->limit($limit)
->execute();
}
This returns empty set...
So far it is not possible to model subqueries in Phalcon. There is also topic according to standard implementation issues.
To query params according to other table, here is an answer.
To query IN you can use queryBuilder
$this->modelsManager->createBuilder()
// ...
->inWhere('column', $array);

Laravel 4.2 Insert from Select

I am completely new to Laravel Framework. And I am using 4.2 version.
I am trying to run below sql statements through function calling from routes.php.
The page shows no error.
But there is no 'truncate' or 'insert' happend in Database.
Here is the code in Controller funtion:
DB::statement('TRUNCATE TABLE calc2');
DB::statement(
"Insert into calc2
Select groups, members, date(timestamp) as Date, count(id) as Total, sum(order) as Order
from Order
where order > 0 and date(timestamp) >= '$lastday'
group by groups, members, date(timestamp)
");
I checked DB connections. I also run these query directly in MySQL. It is working fine.
I tried DB::select instead of DB::statement and I tried with Eloquent/raw too.
But still Database having 0 records only.
Thanks in advance if anyone can suggest me where I am doing mistake.
Are you commiting your queries?
If not, just use DB::commit();
Doc: http://laravel.com/docs/4.2/database#database-transactions

Categories