propel - JoinWith and use Query - php

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

Related

Filtering out rows where two columns equal one thing

I have the following query. I'd like for it to filter out rows after the join where both type and relationship equal 'communicator'. I tried whereRaw('(type <> communicator and relationship <> communicator') but I just get a nasty error. How can I achieve the result I'm looking for?
Relationship is in titles_to_communicators and type is in communicators.
$query = \DB::table('titles_to_communicators')
->leftJoin('communicators', 'communicators.id', '=', 'titles_to_communicators.communicator_id')
->where('relationship', '<>', 'character')
->whereIn('title_id', $childIds)
->groupBy('communicators.slug')
->limit(40);
try
where (type = "communicator" and relation = "communicator")

querybuilder join query in Doctrine2 for Symfony2

Could somebody convert this query for me in querybuilder?
SELECT m.id,n.unitid
FROM mappaths m JOIN unitids n on (m.id=n.id) where n.databaseid=1
I am using this query but it gives me all the values of mm.unitid, while my requirement is to get only one value that is defined by test=1 variable
$query=$qb->select('mm.unitid')
->from('ApiMapBundle:Mappaths','m')
->from('ApiMapBundle:Unitids','mm')
// ->leftJoin('m','u')
->leftJoin('m.refUnitids1','u','WITH','m.id = u')
// ->leftJoin('m.refUnitids2','v')
->where('m.id=:test')
->setParameter('test',1)
->getQuery()->getResult();
Try following:
$query = $qb->select('mm.unitid')
->from('ApiMapBundle:Mappaths','m')
->innerJoin('m.refUnitids1','mm','WITH','m.id = mm.FIELD') //you need to specify on which field of mm join should be done
->where('m.id=:test')
->setParameter('test',1)
->getQuery()
->getResult();
You need to specify field of Unitids which should be used to join to Mappaths. The best way would be to define this relation in Entity definition, then you can use just ->innerJoin('m.refUnitids1','mm') without additional join parameters.
Also, in this case, it is better to use innerJoin instead of leftJoin

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();

Concat_ws in propel

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();

Joining with OR in Kohana

I'm trying to combine two tables based on if they match a given pair in a many-to-many relation. I already know the SQL statement I'm trying to produce, which is functionally equivalent to the following:
SELECT columnA, columnB, ...
...
JOIN matching_table
ON ( (matching_table.id1 = table_a.id AND matching_table.id2 = table_b.id) OR
(matching_table.id1 = table_b.id AND matching_table.id2 = table_a.id) )
...
But I want to produce it using Kohana's query builder for consistency. The problem is I can't seem to find a way of creating a complex ON query. So far all I've got is
DB::select('columnA', 'columnB', ...)
...
->join('matching_table')
->on('matching_table.id1', '=', 'table_a.id')
->on('matching_table.id2', '=', 'table_b.id')
...
and this generates the first AND sequence but I can't seem to put it together with an OR.
Any suggestions?
You should be able to use where instead of on (not tested though):
->join('matching_table')
->where_open()
->where('matching_table.id1', '=', 'table_a.id')
->and_where('matching_table.id2', '=', 'table_b.id')
->where_close()
->or_where_open()
->where('matching_table.id1', '=', 'table_b.id')
->and_where('matching_table.id2', '=', 'table_a.id')
->or_where_close()

Categories