doctrine 2 querybuilder with set parameters not working - doctrine

this is my query:
public function getDetails($userid, $orderby, $sort){
$query = $this->_em->createQueryBuilder()
->select('u')
->from('\Entities\Users', 'u')
->where('u.userid= ?1')
->orderBy('u.?3', '?3')
->setParameter(1, $userid)
->setParameter(2, $orderby)
->setParameter(3, $sort)
->getQuery()
->getResult();
}
it keeps erroring: Message: [Semantical Error] line 0, col 83 near '?3 DESC': Error: '?3' is not defined.
how do i get the orderby from the properties in that function to the query?

You can't use placeholders for dinamical build of DQL query. You'll have to code it by your own:
$sortBy = in_array($sortBy, array(...)) ? $sortBy : 'id';
$sortDir = $sortDir == 'ASC' ? 'ASC' : 'DESC';
$this->em->createQueryBuilder()
...
->orderBy('u.' . $sortBy, $sortDir)

You cant bind parameters to QueryBuilder, only to Query, so just swap lines, first get query out of builder, then fill it with parameters and get result.
$query = $this->_em->createQueryBuilder()
->select('u')
->from('\Entities\Users', 'u')
->where('u.userid= ?1')
->orderBy('u.?3', '?3')
->orderBy('u.'.$orderBy, $sort)
->getQuery()
->setParameter(1, $userid)
->getResult();
}
In doctrine 2.4 its fixed, and you can bind parameters to QueryBuilder.
Update: i've missed moment with placeholder in field name, SQL do not support such constructions.

Related

how to convert sql to query builder laravel

Can anyone help me out to convert this SQL to query builder!
SELECT topwords.*,
mw.word AS my_word
FROM topwords
LEFT JOIN (SELECT DISTINCT words.word
FROM definition_word
JOIN words
ON words.id = definition_word.word_id
WHERE definition_word.user_id = $user) AS mw
ON topwords.word = mw.word
I have a problem with how to use a subquery in leftjoin!
I tried something like this but it has error!
See error as image
DB::table('topwords')
->leftJoin(DB::raw("SELECT DISTINCT
words.word
FROM definition_word
JOIN words ON words.id = definition_word.word_id
WHERE definition_word.user_id = $user as mw"),"topwords.word", "=", "mw.word" )
->select(
"topwords.*",
"mw.word AS my_word"
)->orderBy('id','desc')->paginate(15);
you can use Join Sub query official document subquery-joins
$mw = DB::table('words')
->select('DISTINCT words.word')
->join('definition_word', function($join) use ($user)
{
$join->on('wordss.id', '=', 'definition_word.word_id')
->where('definition_word.user_id', $user);
});
$topwords = DB::table('topwords')
->joinSub($mw, 'mw',function ($join) {
$join->on('topwords.word', '=', 'mw.word');
})
->select('topwords.*','mw.word AS my_word')
->orderBy('id','desc')
->paginate(15);
You have this error because of paginate and aggregation
Try to make custom pagination, using LengthAwarePaginator
Here is example: Laracast
So you need to make something like this:
$query = DB::table('topwords')
->leftJoin(DB::raw("SELECT DISTINCT
words.word
FROM definition_word
JOIN words ON words.id = definition_word.word_id
WHERE definition_word.user_id = $user as mw"),"topwords.word", "=", "mw.word" )
->select(
"topwords.*",
"mw.word AS my_word"
);
$paginator = new LengthAwarePaginator($query->get(), $query->count(), $request->input('per_page', 15), $request->input('page', 1));
And then you can use it in the collection

Use Where in QueryBuilder - Laravel

I have a table Products(id,category_id,name);
I want to get a result like this query:
SELECT * FROM Products WHERE category_id=$category_id OR $category_id=0;
When I assign $category_id with 0 value => above query will return all records.
How do I write that query in Laravel?
I've try this:
Products::where(function($query) use ($category_id){
$query->where("category_id",$category_id)
->orWhere($category_id,0)
})->get();
But, Error:
It look like:
SELECT
*
FROM
`products`
WHERE
( `product_category_id` = 0 OR `0` = 0 )
And Print Error: Column not found: 1054 Unknown column '0' in 'where clause'
How to fix: '0' = 0 to 0 = 0?
Thanks!
Use when():
$products = Product::when($categoryId, function ($query, $categoryId) {
$query->whereCategoryId($categoryId);
})->get();
This method will call the function only if the condition is truthy. You could achieve the same thing with simple if statement:
$products = Product::query();
if ($categoryId) {
$products->whereCategoryId($categoryId);
}
$products = $products->get();
What you gain by using when() is that you don't break the chain, so you can use conditional query changes in one expression. It's useful if you want to just return it, for example.
use it
Products::where(function($query) use ($category_id){
$query->where("category_id",$category_id)
->orWhere("category_id",0);
})->get();
what's wrong with your code is this:
->orWhere($category_id,0)
you made the column part as variable.
try this code:
Products::where(function($query) use ($category_id){
return $query->where("category_id", $category_id)
->orWhere("category_id", 0);
})->get();
$product = new Products;
if ($category_id != 0) {
$product = $product->where("category_id", $category_id);
}
$products = $product->get();

How to Use Union And Paginate in laravel

I Want To Union two Model And Paginate Result But Get Error, In Bellow See My Call Code And Result:
$this->archive = $this->archive
->select (['id','property_id','plan_id','pay_id','price','period','start_at','expire_at'])
->whereHas('property',function ($query) use ($user){
$query->select(['user_id','type_id','title'])->where('user_id',$user);
})->with(['property'=>function($query){
$query->select('title','id');
},'property.owner'=>function($query){
$query->select('name','family','id');
},'plan'=>function($query){
$query->select('title','price','id','status');
},'transaction'=>function($query){
$query->select('port','price','id','status','payment_date');
}]);
$this->model = $this->model
->select (['id', 'property_id','plan_id','pay_id','price','period','start_at','expire_at'])
->whereHas('property',function ($query) use ($user){
$query->select(['user_id','type_id','title'])->where('user_id',$user);
})->with(['property'=>function($query){
$query->select('title','id');
},'property.owner'=>function($query){
$query->select('name','family','id');
},'plan'=>function($query){
$query->select('title','price','id','status');
},'transaction'=>function($query){
$query->select('port','price','id','status','payment_date');
}])
->union($this->archive)->orderBy('expire_at','DESC')->paginate ($paginate);
And Result Of Them Is:
SQLSTATE[21000]: Cardinality violation: 1222 The used SELECT statements have a different number of columns
Please Help Me
you can union sql results like this,
$result = $query1->merge($query2);
$resultSorted = $result->sortByDesc('expire_at');
$count = $query1->count() + $query2->count();
$page = $request['page'];
$perPage = 20;
$resultSorted = new LengthAwarePaginator(
$resultSorted->forPage($page, $perPage), $count, $perPage, $page
);
Pagination with union is not supported in Laravel since there's no elegant way to do it without major drawbacks.

Laravel how do I get the row number of an object using Eloquent?

I'd like to know the position of a user based on its creation date. How do I do that using Eloquent?
I'd like to be able to do something like this:
User::getRowNumber($user_obj);
I suppose you want MySQL solution, so you can do this:
DB::statement(DB::raw('set #row:=0'));
User::selectRaw('*, #row:=#row+1 as row')->get();
// returns all users with ordinal 'row'
So you could implement something like this:
public function scopeWithRowNumber($query, $column = 'created_at', $order = 'asc')
{
DB::statement(DB::raw('set #row=0'));
$sub = static::selectRaw('*, #row:=#row+1 as row')
->orderBy($column, $order)->toSql();
$query->remember(1)->from(DB::raw("({$sub}) as sub"));
}
public function getRowNumber($column = 'created_at', $order = 'asc')
{
$order = ($order == 'asc') ? 'asc' : 'desc';
$key = "userRow.{$this->id}.{$column}.{$order}";
if (Cache::get($key)) return Cache::get($key);
$row = $this->withRowNumber($column, $order)
->where($column, '<=',$this->$column)
->whereId($this->id)->pluck('row');
Cache::put($key, $row);
return $row;
}
This needs to select all the rows from the table till the one you are looking for is found, then selects only that particular row number.
It will let you do this:
$user = User::find(15);
$user->getRowNumber(); // as default ordered by created_at ascending
$user->getRowNumber('username'); // check order for another column
$user->getRowNumber('updated_at', 'desc'); // different combination of column and order
// and utilizing the scope:
User::withRowNumber()->take(20)->get(); // returns collection with additional property 'row' for each user
As this scope requires raw statement setting #row to 0 everytime, we use caching for 1 minute to avoid unnecessary queries.
$query = \DB::table(\DB::raw('Products, (SELECT #row := 0) r'));
$query = $query->select(
\DB::raw('#row := #row + 1 AS SrNo'),
'ProductID',
'ProductName',
'Description',
\DB::raw('IFNULL(ProductImage,"") AS ProductImage')
);
// where clauses
if(...){
$query = $query->where('ProductID', ...));
}
// orderby clauses
// ...
// $query = $query->orderBy('..','DESC');
// count clause
$TotalRecordCount = $query->count();
$results = $query
->take(...)
->skip(...)
->get();
I believe you could use Raw Expresssions to achieve this:
$users = DB::table('users')
->select(DB::raw('ROW_NUMBER() OVER(ORDER BY ID DESC) AS Row, status'))
->where('status', '<>', 1)
->groupBy('status')
->get();
However, looking trough the source code looks like you could achieve the same when using SQLServer and offset. The sources indicates that if you something like the following:
$users = DB::table('users')->skip(10)->take(5)->get();
The generated SQL query will include the row_number over statement.
[For Postgres]
In your model
public function scopeWithRowNumber($query, $column = 'id', $order = 'asc'){
$sub = static::selectRaw('*, row_number() OVER () as row_number')
->orderBy($column, $order)
->toSql();
$query->from(DB::raw("({$sub}) as sub"));
}
In your controller
$user = User::withRowNumber()->get();

Laravel pagination not working with group by clause

It seems Laravel pagination does not working properly with group by clause. For example:
$users = Subject::select(DB::raw('subjects.*, count(user_subjects.id) as total_users'))
->join('user_subjects', 'user_subjects.subject_id', '=', 'subjects.id')
->whereNull('user_subjects.deleted_at')
->groupBy('subjects.id')
->orderBy('subjects.updated_at', 'desc')
->paginate(25);
Produced
select subjects.*, count(user_subjects.id) as total_users
from `subjects` inner join `user_subjects` on `user_subjects`.`subject_id` = `subjects`.`id`
where `subjects`.`deleted_at` is null and `user_subjects`.`deleted_at` is null
group by `subjects`.`id`
order by `subjects`.`updated_at` desc
note that, there is no limit clause on the query.
Working fine if no group by clause in the query:
$users = Subject::select(DB::raw('subjects.*, count(user_subjects.id) as total_users'))
->join('user_subjects', 'user_subjects.subject_id', '=', 'subjects.id')
->whereNull('user_subjects.deleted_at')
->orderBy('subjects.updated_at', 'desc')
->paginate(25);
produced the following query:
select subjects.*, count(user_subjects.id) as total_users from `subjects`
inner join `user_subjects` on `user_subjects`.`subject_id` = `subjects`.`id`
where `subjects`.`deleted_at` is null and `user_subjects`.`deleted_at` is null
order by `subjects`.`updated_at` desc
limit 25 offset 0
does anyone has any idea how can i fix this?
Check the documentation
https://laravel.com/docs/5.2/pagination
Currently, pagination operations that use a groupBy statement cannot
be executed efficiently by Laravel. If you need to use a groupBy with
a paginated result set, it is recommended that you query the database
and create a paginator manually.
I know it is an old question, by I am sharing my solution for future reference.
I managed to write a function based on this link which does the heavy job of determining the pagination of a complex query. Just pass the 'QueryBuilder' and it will return the paginated object/collection.
Additionally, this procedure can track and maintain the other parameters except for page=.
public function mergeQueryPaginate(\Illuminate\Database\Eloquent\Builder $query): \Illuminate\Pagination\LengthAwarePaginator
{
$raw_query = $query;
$totalCount = $raw_query->get()->count();
$perPage = request('per-page', 10);
$page = request('page', 1);
$skip = $perPage * ($page - 1);
$raw_query = $raw_query->take($perPage)->skip($skip);
$parameters = request()->getQueryString();
$parameters = preg_replace('/&page(=[^&]*)?|^page(=[^&]*)?&?/', '', $parameters);
$path = url(request()->getPathInfo() . '?' . $parameters);
$rows = $raw_query->get();
$paginator = new LengthAwarePaginator($rows, $totalCount, $perPage, $page);
$paginator = $paginator->withPath($path);
return $paginator;
}
This works for me in laravel 5.2
Select(\DB::RAW("assignment_descendant_child.assignment_descendant_child_id, assignment_descendant_child.assignment_descendant_child_name, COUNT(assignment_descendant.assignment_descendant_id) as xNum"))
->leftJoin(
'assignment_descendant',
'assignment_descendant.assignment_descendant_child_id',
'=',
'assignment_descendant_child.assignment_descendant_child_id'
)
->orderBy('assignment_descendant_child_name')
->groupBy('assignment_descendant_child.assignment_descendant_child_id')
->paginate(\Config::get('constants.paginate_org_index'))
create a database view namedvw_anything. MySql query will be like
create view vw_anything as select subjects.*, count(user_subjects.id) as total_users from subjects inner join user_subjects on user_subjects.subject_id = subjects.id
where subjects.deleted_at is null and user_subjects.deleted_at is null group by subjects.id;
Now create a new model named UserSubModel for this view, protected $table = 'vw_anything';
Now your paginate query will be like UserSubModel::orderBy('subjects.updated_at', 'desc')->paginate(25);
.
To answer this questioin Laravel Pagination group by year and month only
View query will be :
create view vw_anything as select gallery.*, DATE_FORMAT(created_at, "%Y-%m") as tanggal,count(created_at) as jumlah from gallery group by tanggal;
Let you model is VwModel then your paginate query will be
VwModel::where('type','Foto')->orderBy('tanggal','desc')->paginate(2);
This works if you want to group by and paginate.
$code = DB::table('sources')
->select(DB::raw('sources.id_code,sources.title,avg(point) point'))
->join('rating','sources.id_code','rating.id_code')
->groupBy('sources.id_code')
->groupBy('sources.title')
->groupBy('sources.language')
->groupBy('sources.visited')
->paginate(5);

Resources