Filtering by sum of rows - Propel - php

I have a virtual column in my query like this:
->withColumn('SUM(retoure_menge)', 'return_quantity')
and I want to filter my query to only have this sum greater than zero.
How can I do something similar to
->filterByReturnQuantity(array('min' => 0))
?

Something like this should do the trick.
$query->withColumn('SUM(retoure_menge)', 'return_quantity')
->where('return_quantity > 0')
->find();

Related

Operation count different id's

i need to do this operation:
Count the total of each number of records with a different 'rrpp': 1,2,3,4 ∞
I always use this to sum the records of a RRPP:
$variable = Modelo::where('dia_id' , $request->id)->where('rrpp' , 1)->count('id');
but this way I only get the result of 1 in specific.
And what I need is to get the result like this
help, thanks
you only get result 1 because you use this where('dia_id' , $request->id) in your query,
if you want to count rrpp=1 use this query:
$variable = Modelo::where('rrpp' , 1)->count('id');
if you want to count all rrpp use this query:
$variable = Modelo::select('rrpp',
DB::raw('count(*) as total'))
->groupBy('rrpp')
->get()

Laravel Eloquent where field is X or null

I have a table like this:
table
- field1: tinyint
- field2: varchar (nullable)
- datefield: timestamp (nullable)
Now I want to get all entries where field1 is 1, field2 is null and where datefield is smaller than X or null. I already tried something like this:
$query = Model::where('field1', 1)
->whereNull('field2')
->where('datefield', '<', $date)
->orWhereNull('datefield');
but thats not working. I always get every entry where datefield is null. It doesn't matter what the other fields are. I also tried to split it in 2 queries: First get every row where datefield is smaller than X or null and then (based on it) get every field where field1 is 1 and field2 is null.
The result was the same.
Any idea how to do this?
It sounds like you need to make use of advanced where clauses.
Given that search in field1 and field2 is constant we will leave them as is, but we are going to adjust your search in datefield a little.
Try this:
$query = Model::where('field1', 1)
->whereNull('field2')
->where(function ($query) {
$query->where('datefield', '<', $date)
->orWhereNull('datefield');
}
);
If you ever need to debug a query and see why it isn't working, it can help to see what SQL it is actually executing. You can chain ->toSql() to the end of your eloquent query to generate the SQL.
You could merge two queries together:
$merged = $query_one->merge($query_two);
Using coalesce() converts null to 0:
$query = Model::where('field1', 1)
->whereNull('field2')
->where(DB::raw('COALESCE(datefield_at,0)'), '<', $date)
;
If you are confused about where to put the get()/first() for getting the collection or a single row here is the way:
$query = Model::where('field1', 1)
->whereNull('field2')
->where(function ($query) {
$query->where('datefield', '<', $date)
->orWhereNull('datefield');
}
)->get();

Symfony 2 Multiple Selects with counts on the same table?

Ok I have a flag field on one table, open or closed which is boolean. I am trying to build one query that would take that field and count them based on that flag. Then I will need to group them by account ID
Here is what I am working with now,
$GetTest1 = $GetRepo->createQueryBuilder('s') <- I had 'w' in here but all that did was add an index and not a second alias?
->select(' (count(s.open_close)) AS ClosedCount, (count(w.open_close)) AS OpenCount ')
->where('s.open_close = ?1')
//->andWhere('w.open_close = ?2')
->groupBy('s.AccountID')
->setParameter('1', true)
//->setParameter('2', false)
->getQuery();
Is what I want do-able? I know (or at lest think) that I can build a query with multiple table alias? - Please correct me if I am wrong.
All help most welcome.
Thanks
This DQL query will group the rows in table by accountId and for each of them it will give you count for yes (and you can get count for no by substracting that from total).
BTW I found writing straight DQL queries much more straightforward than writing QueryBuilder queries (which i use only when i need to dynamically construct the query)
$results = $this->get("doctrine")->getManager()
->createQuery("
SELECT t.accountId, SUM(t.openClose) as count_yes, COUNT(t.accountId) as total
FROM AppBundle:Table t
GROUP BY t.accountId
")
->getResult();
foreach ($results as $result) {
//echo print_r($result);
//you can get count_no as $result["total"] - $result["count_yes"];
}

what the meaning mergeBindings in laravel

hy I'm new in laravel 4 and I have found code like this
$sub = Abc::where(..)->groupBy(..); // Eloquent Builder instance
$count = DB::table( DB::raw("({$sub->toSql()}) as sub") )
->mergeBindings($sub->getQuery())
->count();
my quetion is
1. what the meaning mergeBindings($sub->getQuery()) and give me example for using mergeBindings
assume your first query is like this:
$sub = Abc::where('type', 1)->groupBy(..);
then when we convert it to sql:
$sub->toSql();
this will return query string some thing like this:
SELECT * FROM abc where abc.type = ? GROUp BY ...
you can see the "?" as the type value which will be binded (replaced by 1) when the PDO executes that query string
So when we use that sub query in another query as you do in
$count = DB::table( DB::raw("({$sub->toSql()}) as sub") )
->mergeBindings($sub->getQuery())
->count();
you have converted the first sub query to sql string, BUT the second query does not know any thing about your first sub query binding which in this example the value 1
so we need to merge the binding from the first sub query into the last query
so that the last query when executes it will know the value 1 and binds it to the
where clause in replace of "?", and your final executed query will be something like this
(SELECT count(*) from abc where type = 1 GROUP BY ...) as sub
and thats it the use of mergeBindings() method
i hope this makes things clear for your question.
thanks,

Is it possible to re-use a Kohana ORM query for the row count?

So I have my query as so...
$records = ORM::factory('category');
Add a WHERE clause as so...
$records = $records->where('categoryid', 'LIKE', 'aa');
Grab a count for pagination as so...
$count = $records->count_all();
And my where clause gets cleared away as so...
SELECT `categories`.* FROM `categories` LIMIT 20 OFFSET 0
With this line commented out
//$count = $records->count_all();
My SQL looks just fine...
SELECT `categories`.* FROM `categories` WHERE `categoryid` LIKE 'aa' LIMIT 20 OFFSET 0
Is it possible to use a single query the way I'm trying to or do I have to make two duplicate identical queries? One for the count, and one for the actual results...
Thanks!
Use special reset(FALSE) call:
$records = $records->where('categoryid', 'LIKE', 'aa');
$records->reset(FALSE); // !!!!
$count = $records->count_all();
$categories = $records->find_all();

Categories