Doctrine QueryBuilder order by the amount of 2 fields - doctrine-orm

I would like to calculate the amount of 2 fields, and ordering the results by.
The querybuilder ends like this:
$qb->orderBy('(e.likesCnt + e.additionalLikes)', 'DESC')
and I got this error:
An exception has been thrown during the rendering of a template
("[Syntax Error] line 0, col 264: Error: Expected end of string, got
'e'")
Last try was when:
$qb->select('e, (e.likesCnt + e.additionalLikes) AS totalLikes')
$qb->orderBy('totalLikes', 'DESC')
but it brings an error too
An exception has been thrown during the rendering of a template
("[Semantical Error] line 0, col 290 near 'totalLikes DESC,': Error:
'totalLikes' is not defined.")
Any solutions gratefully accepted :-)
Update:
Well, alias is not allowed in the "orderby" statement. Also, if one of your field is null then the amount will be null too, that's cause the semantical error. My solution is:
$qb->orderBy('((e.likesCnt + e.additionalLikes)+0)', 'DESC')
In this case, the ordering will be applicated without semantical error and gets the correct results.

The select method is getting parameter as array, for example:
$qb->select('e', '(e.likesCnt + e.additionalLikes) AS totalLikes')
$qb->orderBy('totalLikes', 'DESC')

Related

Value at 'body' failed to satisfy constraint: Member must not be null

I'm trying to run a prediction using a sagemaker endpoint. The input format is comma separated features and | separated observations.
However when I try to iterate over the input data and invoke the end point on every iteration like this :
ENDPOINT_NAME = "my_endpoint"
runtime= boto3.client('runtime.sagemaker')
results = []
for r in request_body.split('|'):
response = runtime.invoke_endpoint(EndpointName=ENDPOINT_NAME,
ContentType='text/csv',
Body=r)
result = json.loads(response['Body'].read().decode())
results.append(result)
I get the following error:
ValidationError: an error occurred (ValidationError) when calling the InvokeEndpoint operation: 1 validation error detected: Value at 'body' failed to satisfy constraint: Member must not be null
As a sanity check I ran :
for r in request_body.split('|'):
print(r)
And I get the result I expect to get:
3.0,0.0,4795.0,0.0,1.0,24.0,30.0,25.0,3.0
3.0,2.0,3818.0,0.0,3.0,10.0,22.0,11.0,11.0
5.0,0.0,3565.0,0.0,1.0,79.0,89.0,80.0,-66.0
5.0,-1.0,3227.7,0.0,0.0,16.0,17.0,17.0,1.0
5.0,0.0,3375.0,0.0,2.0,21.0,45.0,22.0,6.0...etc
Which leads me to believe that the logic in extracting the separate observations is sound, but somehow when I execute the call I get this null value error.
The idea is to get ordered predictions so that I can later map them to an id that is not part of the training features and hence not in the dataset.
Thank you in advance.
I had the same issue. Check if you are also passing to the endpoint an empty "r".
request_body.split('|') will generate a list with each of the rows of the dataframe, but it will also include one empty: ''

Power Query: try ... otherwise on list item expression

Why does catching the following index-error fail:
let
Source = "abc",
letter_list = Text.ToList(Source),
try_on_index_error = try letter_list{5}
in
try_on_index_error
The try statement does not return an Error record. It keeps throwing the error as if no try was present.
In this case it works as expected, returning an Error record as a valid result of the query:
let
Source = "abc",
try_another_error = try Source + 1,
Error = try_another_error[Error]
in
Error
https://learn.microsoft.com/en-us/power-query/handlingerrors#catching-an-error-with-try-and-otherwise
https://learn.microsoft.com/en-us/powerquery-m/m-spec-consolidated-grammar#error-handling-expression
It is naturally hard to answer questions on the behavior of closed-source software. However, the comment by Ron Rosenfeld solved my trouble:
letter_list = List.Buffer(Text.ToList(Source))
The docs (last paragraph in chapter Evaluation) explain the unexpected behavior: Lazy evaluation is done for lists, records and let-expressions. As seen above, in case of lazy evaluation, try statement falls back to syntax checking, supposedly.

oracle.jdbc.OracleDatabaseException: ORA-01722: invalid number

Getting the above error but the offending column is not a number. Somehow it is related to the cycle_code column which is a string. My query is hacky since I dont have full control over building it so I have to append another condition at the end ( "!= 'ABC'"). If I remove that condition the query works. I dont understand why im getting an invalid number error though?
select * from my_view WHERE ... CYCLE_CODE IN ( 'XYZ' ) AND CYCLE_CODE != 'ABC';

Get elements from LazyLoadCollection

I have found Doctrine\Common\Collections\Criteria to be a very useful concept, if they worked for me.
In a symfony controller, I am calling this code:
$criteria = Criteria::create()
->where(Criteria::expr()->gt('position', 0))
->orderBy(['riskPosition', Criteria::ASC]);
$positions= $this->getDoctrine()->getRepository(DataCategory::class)->matching($criteria);
dump($positions->count()); // dumps 1, correct!
dump($positions);
foreach($positions as $r)
dump($r); // -> Unrecognized field: 0
dump($positions) gives
LazyCriteriaCollection {#881 ▼
#entityPersister: JoinedSubclassPersister {#849 ▶}
#criteria: Criteria {#848 ▼
-expression: Comparison {#836 ▶}
-orderings: array:2 [▶]
-firstResult: null
-maxResults: null
}
-count: 1
#collection: null
#initialized: false
}
As soon as I access an element of the returned array, I get an error
ORMException::unrecognizedField(0)
in vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php (line 1193)
But as soon as I want to access the elements (e.g. loop and dump) I get some error like An exception has been thrown during the rendering of a template ("Unrecognized field: 0").
As far as I have studied the code, the problem is that the query results have not been fetched from the database. Only count() works. How can I trigger this fetch?
Does it matter that my entity has #ORM\InheritanceType("JOINED")?
This code (circumventing the use of Criteria) does give correct results, but I'd like to use Criteria:
$riskPositions = $this->getDoctrine()->getRepository(DataCategory::class)
->createQueryBuilder('p')
->where('p.position > 0')
->orderBy('p.position', 'ASC')
->getQuery()
->execute();
The issue is caused by line:
->orderBy(['riskPosition', Criteria::ASC]);
Doctrine\Common\Collections\Criteria `s orderBy accepts an array argument where
Keys are field and values are the order, being either ASC or DESC.
github link
Apparently, there is a mistake at doctrine s documentation.
So doctrine thinks that "0", which is the 1st key of the array argument, is the field to sort by, but cannot find it.
To solve, change the above line to:
->orderBy(['riskPosition' => Criteria::ASC]);

What is the "select when" syntax for?

Experimenting with the language I've found that select is defined in the global scope and its precedence is higher than local variables.
def example(select)
puts select
end
example 3
# Syntax error in eval:3: unexpected token: end (expecting when, else or end)
So experimenting with select step by step I get this:
select 1 end
# Syntax error in eval:3: unexpected token: end (expecting when, else or end)
and then
select when 1 end
# Syntax error in eval:1: invalid select when expression: must be an assignment or call
then
select when x = 1 end
# Syntax error in eval:1: invalid select when expression: must be an assignment or call
then
select when x
# Syntax error in eval:1: unexpected token: EOF (expecting ',', ';' or '
I'll skip ahead a few steps as you should have an idea of how I've come to my question…
select when x;
else y
end
# Error in line 1: undefined local variable or method 'x_select_action'
and lastly
x_select_action = 4
select when x;
else y
end
# Error in line 3: undefined method 'x_select_action' (If you declared 'x_select_action' in a suffix if, declare it in a regular if for this to work. If the variable was declared in a macro it's not visible outside it)
So there is this keyword in the language which precedes local variables precedence and I don't know what it's for. But apparently it looks for x_select_action when x is given as a when clause. What is this select for and how is it meant to be used?
Searching online I see select defined on Enumerable, Hash, Channel, and Array… but at first glance these don't seem to be it.
Thanks for the help!
It's similar to Go's select: https://tour.golang.org/concurrency/5
But it still needs some tweaks to be finished, that's why there are no docs about it yet.