For an optimized query with Doctrine in a Symfony 3 project, I want to select multiple entities like this :
Entity "e" have a oneToMany relation and "a" have also a OneToMany relation with "au" ("au" should have been the linked table in a ManyToMany relation but i needed some extra fields in it)
$qb = $this->createQueryBuilder('e');
return $qb->select('e, d, du')
->leftJoin('e.demos', 'd')
->leftJoin('d.demo_user', 'du')
->getQuery()
->getResult();
If one of my relation is empty (there is no demo_user for exemple), my entity e and related are not send in results. I want to have ALL the results even if there is no demo nor demo_user related.
Thank you for your help ;-)
Example to connect 3 entities in query:
you must link table in query
return $this->createQueryBuilder( 'e' )
// if skip "->select ..." then receive all column form entities: E, AU, DU
->select(
'e.id as id',
'e.taskName as taskName',
'e.dueDate as dueDate',
'au.isOwn as isOwn',
'du.fild2 as fild2'
)
->leftJoin('YourBundle:EntityAU', 'au', 'WITH', 'au.idTask = e.id')
->leftJoin('YourBundle:EntityDU', 'du', 'WITH', 'ad.fild1 = au.id')
->where( 'au.idParent = :par1' )
->setParameter( 'par1', $varId )
->orderBy( 't.parentString', 'ASC' )
->getQuery()
->getResult();
Related
i am trying to fetch record with order by query but situation is this that i have to use order by both on current table column and relationship table column in laravel . I tried this
$consignments = Consignment::where('delivery_run_id', $id)->whereIn('status', [
'In Warehouse',
'With On Forwarder',
'In Transit (Warehouse->Delivery)',
'Awaiting Pickup'
])->with(['consignment_run_sheet' => function ($query) {
$query->orderBy('run_sheet_id');
}])->orderBy('delivery_date', 'DESC')->get();
$deliveryRuns = DeliveryRun::all();
How I can achieve it?
it will order just the relation items in this way. solution is use subquery or joins. useing Subquery is like this if assume the modal for consignment_run_sheet relation is ConsignmentRunSheet and the relation is belongsTo:
$consignments = Consignment::where('delivery_run_id', $id)->whereIn('status', [
'In Warehouse',
'With On Forwarder',
'In Transit (Warehouse->Delivery)',
'Awaiting Pickup'
])->with('consignment_run_sheet')
->orderBy(ConsignmentRunSheet::select('delivery_date')
->whereColumn('consignments.id', 'consignment_run_sheet.consignment_id'), 'DESC')
->get()
source:
https://reinink.ca/articles/ordering-database-queries-by-relationship-columns-in-laravel
I have belongsToMany relationship between items and vehicle.
items can be assigned to multiple vehicles. same vehicle can b assigned to multiple items. so my pivot table item_vehicle have extra column date which will show that when vehicle is assigned to item.
here is my query.
select `items`.`id`, `items`.`name`, `items`.`area` as `total_area`,
`item_vehicle`.`date`, `vehicles`.`name` as `vehicle_name`,
SUM(parcel_vehicle.area) as processed_area
from `parcels`
inner join `item_vehicle` on `item_vehicle`.`p_id` = `items`.`id`
inner join `vehicles` on `item_vehicle`.`t_id` = `vehicles`.`id`
where `item_vehicle`.`date` < '?' and `items`.`processed` = ? and `vehicles`.`name`=?
group by items.id
what will be the eloquent way of doing this
Item::with(['vehicle'=>function($q){$q->wherePivot('date','<','2019/2/12');}])->whereHas('vehicle',function($q){$q->where('vehicles.id','2');})->where('processed',1)->where('id',4)
->get();
my concerns is it should run only one query
$parcels = Parcel::join('item_vehicle', 'item_vehicle.pid', '=' ,'items.id')
->join('vehicles', 'vehicles.id', '=' ,'item_vehicle.t_id')
->where('item_vehicle.date', '<', $date)
->where('items.processed', $processed)
->where('vehicles.name', $vehicleName)
->select(
'items.id',
'items.name',
\DB::raw('items.area as total_area'),
'item_vehicle.date',
\DB::raw('vehicles.name as vehicle_name'),
\DB::raw('SUM(parcel_vehicle.area) as processed_area')
)
->groupBy('items.id')
->get();
However, you have non-aggregated columns in select and you are doing group by. To make this work you might need to disable mysql's only_full_group_by mode
Hi I want to form a query in laravel 5.4 eloquent for the following SQL:
select A.*, concat(B.firstname, ' ', B.lastname) as clientname, C.category as category from payments A, clients B, categories C where A.client_id = B.id and A.category_id = C.id
All the foreign keys are properly given and the models are also properly created.
My aim is to get all the master record name like category, client name, etc to come in the child table record set directly.
You can try this:
DB::table('payments')
->select('payments.*',DB::raw('CONCAT(clients.firstname," ",clients.lastname) as clientname'),categories.category)
->join('clients','clients.id','=','payments.client_id')
->join('categories','categories.id','=','payments.category_id')
->get();
Hope this help for you !!!
Paste your row query in DB::row() and print data like following
$data= DB::row("select A.*, concat(B.firstname, ' ', B.lastname) as clientname, C.category as category from payments A, clients B, categories C where A.client_id = B.id and A.category_id = C.id")->get();
dd($data);
if your query is right and properly managed relation than its retrieve data
If you already made Models than Why you don't use ORM?
Use Models,
payment::leftJoin('clients ','payments.client_id','=','clients.id')
->leftJoin('categories ','payments.category_id ','=','categories .id')->select(['payment.*','categories.category',DB::raw("CONCAT(clients.firstname,' ',clients.lastname) as clientname")])->get();
I want to execute following raw query with laravel eloquent ORM:
SELECT *,(SELECT country FROM tbl_sim_numbers WHERE highland_id= b.highland_id) as country1,(SELECT operator FROM tbl_sim_numbers WHERE highland_id= b.highland_id) AS operator1 FROM tbl_sim_distributions b ORDER BY b.created_at
on eloquent ORM of laravel. I tried:
$sub = DB::select(DB::raw("SELECT *,(SELECT country FROM tbl_sim_numbers WHERE highland_id= b.highland_id) as country1,(SELECT operator FROM tbl_sim_numbers WHERE highland_id= b.highland_id) AS operator1 FROM tbl_sim_distributions b
ORDER BY b.created_at"));
$pdfGenObjForList = DB::table( DB::raw("({$sub->toSql()}) as sub") )
->mergeBindings($sub->getQuery());
However above code gives error.Someone,please help.
I'm joining two tables that both have tag_id (i.e. album.tag_id and photo.tag_id)
function GetJoined()
{
return Album::join('photos', 'photos.tag_id', '=', 'albums.tag_id')
}
but I don't want that resulting query to keep reminding me that this was a join from two tables
GetJoined()->where('tag_id', 1);
will be all like
'tag_id' in where clause is ambiguous
Any suggestions?