Concat_ws in propel - php

how can I modify my propel query to get the same result as the following MySQL query ?
SELECT v.id,CONCAT_WS(" ",b.name,v.model) AS car_name
FROM vehicle v
JOIN account_vehicle av ON av.account_id=:uid
LEFT JOIN brands b ON b.id=v.manufacturer
WHERE av.vehicle_id=v.id
Right now i have something like that
$query = VehicleQuery::create('v')
->joinAccountVehicle('av')
->leftJoinBrands('b')
->select(array('v.Id', 'b.Name', 'v.Model'))
->where('av.VehicleId=v.Id')
->find();
How can i modify the propel use - to get the same result ? I'm having difficulties with the concat_ws function in propel.
I've tried using the Criteria model - but I cant add the joined table (criteria requires TABLEPEER:COLUMN_NAME's and rejects my aliases)

Try something like this:
$query = VehicleQuery::create('v')
->joinAccountVehicle('av')
->leftJoinBrands('b')
->select(array('v.Id', 'b.Name', 'v.Model','CONCAT_WS(" ",b.name,v.model) AS car_name'))
->where('av.VehicleId=v.Id')
->find();

Related

How to give alias name on same name columns

I'm using eloquent. I would like to write following code in case of SQL.
SELECT A.col as a_col, B.col as b_col
FROM a_tbl A
JOIN b_tbl B ON A.id = B.id
a_tbl and b_tbl have same name columns.
How can I write this with eloquent?
You can do it in this way:
$records = DB::table('a_tbl')
->join('b_tbl', 'a_tbl.id', '=', 'b_tbl.id')
->select('a_tbl.col as a_col', 'b_tbl.col as b_col')
->get();
Also you can read about laravel docs for this:
https://laravel.com/docs/8.x/queries#joins

propel - JoinWith and use Query

What is a diference beetween JoinWith, and useTablenameQuery ? How its work in propel, why JoinWith is faster than useTable_nameQuery ? How work formatter ? Thanks for help :
)
Main difference is when you have filter specifically for the on the right like TableB=?
joinWith: Default Join condition as defined in the schema
$rows = TableAQuery::create()
->joinWith('TableB')
->find();
useTableBQuery Use a filter for the table right
$rows = TableAQuery::create()
->useTableBQuery()
->filterByColumnName($value) // A column in TableB
->endUse()
->filterByColumnName($value2) // A column in TableA
->find();
Notice the second ->filterByColumnA() is outside the ->useTablBQuery, to illustrate that you can have filter on both tables

Find auctions for a user in eloquent

I'm working with L5 and elequent
My table structure is..
users
id
other field
auctions
id
other field
lots
id
auction_id
other field
lot_user
lot_id
user_id
I want to find auctions for a user.
How can i do this?
$user = User::find($id);
$auctions = $user->auctions();
I have got an idea to do this with eloquent..
$auctions = $user->lots()
->join('auctions','auctions.id','=','lots.id')
->select(['auctions.*'])
->get();
I'm not sure Eloquent is going to be very efficient here, but you could do something like :
In your User(s) class, you need to define a many-many relationship like :
public function lots()
{
return $this->belongsToMany('App\Lot');
}
In your Lot(s) class, you need to define the inverse of a one-to-many relationship like:
public function auctions()
{
return $this->belongsTo('App\Auction')
}
Then, to get Lots for a user, you'd do something like :
$user->lots();
To get auctions, you'd need to loop over lots and call $lot->auctions() for each one, and then filter by id to get the unique auctions.
This is a case where it would probably be easier to use the DB facade to built a query instead of just trying to use Eloquent.
About DB facade.
Raw query will looks like this:
SELECT * FROM auctions AS a
INNER JOIN lots AS l ON (l.auction_id = a.id)
INNER JOIN lot_user AS lu ON (lu.lot_id = l.id AND lu.user_id = $findUserId)
GROUP BY a.id
And using query-builder you can do it like this:
DB::table('auctions')
->join('lots', 'lots.auction_id', '=', 'auctions.id')
->join('lot_user', function ($join) {
$join->on('lot_user.lot_id', '=', 'lots.id')
->where('lot_user.user_id', '=', $findUserId);
})
->groupBy('auctions.id')
->get();

Laravel From Raw DB to Eloquent

i m trying to change my laravel raw query to eloquent, since now i have got some basics of eloquent and i have tried making blogs in laravel.
But some complexity has remain the same.
First: this is my Raw SQL:
select group_concat(DISTINCT q.sku SEPARATOR ", ") as sku, `sales_flat_order`.`status`, `sales_flat_order`.`increment_id`, `sales_flat_order`.`shipping_description`, `sales_flat_order`.`subtotal`, `sales_flat_order`.`customer_email`, `d`.`country_id`, `d`.`region`, `d`.`city`, `d`.`postcode`, group_concat(DISTINCT q.name SEPARATOR ", ") as name, concat(sales_flat_order.created_at) AS created_at from `sales_flat_order` left join `sales_flat_order_item` as `q` on `sales_flat_order`.`entity_id` = `q`.`order_id` left join `sales_flat_order_address` as `d` on `d`.`parent_id` = `sales_flat_order`.`entity_id` group by `sales_flat_order`.`increment_id` order by `sales_flat_order`.`increment_id` asc
My Raw query in Laravel:
SalesFlatOrder::leftJoin('sales_flat_order_item as q','sales_flat_order.entity_id', '=','q.order_id')
->leftJoin('sales_flat_order_address as d', 'd.parent_id', '=', 'sales_flat_order.entity_id')
->select((array(DB::Raw('group_concat(DISTINCT q.sku SEPARATOR ", ") as sku'),'sales_flat_order.status','sales_flat_order.increment_id', 'sales_flat_order.shipping_description','sales_flat_order.subtotal','sales_flat_order.customer_email','d.country_id', 'd.region', 'd.city','d.postcode',DB::raw('group_concat(DISTINCT q.name SEPARATOR ", ") as name'),DB::raw('concat(sales_flat_order.created_at) AS created_at'))))
->groupBy('sales_flat_order.increment_id')
->orderBy('sales_flat_order.increment_id')
->paginate(10);
Now i am trying to change this whole raw query into eloquent. I have already made models. So following is my eloquent query which is in my controller.
public function detailed(){
$sales = SalesFlatOrder::with('address')->groupBy('increment_id')->orderBy('increment_id')->paginate(10);
return View::make('detailed')->with('sales', $sales);
}
My Problem: 1:
I do have group concat (Distinct q.sku) . So how to do that with eloquent. Because sometimes you need to do group by Date. or group by Orders. So how to show Distinct data so sku column. So how to convert this full fledge raw query into Eloqeunt where we have group_concat, Distinct for so many columns of DB
First create a Custom Collection:
$customCollection = new \Illuminate\Database\Eloquent\Collection;
Then, you can do this like that:
$user =new User;
$user->setRawAttributes((array) $userRawData);
$customCollection->add($user);
Regards!
You can move from RAW to Eloquent elegant queries with models relationship, eloquent DB tables naming convention (without specifying table name in model), route data binding crom DB and scopes.
But still, sometimes you will have to help youself with some RAW for more complicated operations.

How to set a where clause filter with doctrine2 query buider in a many-to-many relation

I've been trying with no success to user doctrine2 query builder to fetch records in a related many-to-many table using a where clause.
I would like to reproduce the following statement:
SELECT [...] FROM Company
JOIN CompanyAddress ON CompanyAddress.CompanyId = Company.Id
JOIN Address ON Address.Id = CompanyAddress.AddressId
WHERE Address.State = ?
following some ideias found on google, stackoverfow and doctrine docs:
$qb = $this->_em->createQueryBuilder();
$qb->select('c')
->from('Company', 'c')
->where(':State MEMBER OF c.Address')
->setParameter('State', $arguments);
but the results are not the desired one. Any help? Thanks..

Categories