I am having a few related problems with the OrderBy method when generating a query for OrmLite. The following two statements work:
.OrderBy(ob => new { at = ob.SortBy, ob.Id });
.OrderBy(ob => new { at = Sql.Desc(ob.SortBy), ob.Id });
But the following statement gives me a compile error (Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access):
.OrderBy(ob => new { at = Sql.Desc(ob.SortBy), Sql.Desc(ob.Id) });
And the following statement gives me a sql error (Incorrect syntax near the keyword 'ASC'):
.OrderBy(ob => new { at = Sql.Desc(ob.SortBy) });
And when digging deeper I see that the OrderByExpression gives me:
ORDER BY "SortBy" DESC ASC
So... the semi-obvious solution/workaround now that I've been working with OrmLite for a few days is to just concatenate statements and also using OrderByDescending... for example
.OrderByDescending(ob => ob.SortBy).OrderByDescending(ob => ob.Id)
or
.OrderByDescending(ob => ob.SortBy)
Related
I have a model called "Consol", where I have a scope that looks like this:
public function scopeOfFinalsSent($query, $set)
{
if ($set) {
return $query->whereNotNull('finals_sent')->where(function ($query) {
$query->where('final_weight', '>=', 'current_weight')
->where('final_cbm', '>=', 'current_cbm');
});
}
return $query;
}
I am using it like this:
return Consol::ofFinalsSent(true)->count();
When using this scope together with MSSQL, I get the below error:
SQLSTATE[42000]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Error converting data type nvarchar to numeric. (SQL: select count(*) as aggregate from [consols] where [first_etd] between 2020-04-20 and 2020-04-26 and [finals_sent] is not null and ([final_weight] >= current_weight and [final_cbm] >= current_cbm))
Please note, if I switch over to MySQL, I don't get the above error.
I have tried to debug the actual query, by using the toSql() method, on my scope. The query, that I am performing looks like this:
select * from [consols] where [first_etd] between '2020-04-20' and '2020-04-26' and [finals_sent] is not null and ([final_weight] >= "current_weight" and [final_cbm] >= "current_cbm")
When running this query directly in the SQL Server Management Studio on the actual table, the query is being executed perfectly.
Below you can see the schema for the table:
You need to use whereColumn to compare values of columns, in your case:
public function scopeOfFinalsSent($query, $set)
{
if ($set) {
return $query->whereNotNull('finals_sent')->where(function ($query) {
$query->whereColumn('final_weight', '>=', 'current_weight')
->whereColumn('final_cbm', '>=', 'current_cbm');
});
}
return $query;
}
Otherwise you end up comparing things like final_weight to the string value "current_weight"
I fixed this issue by using the whereRaw method that Laravel provides, as I suspected something "going on behind the scenes" with the where method.
My scope now looks like this (notice the whereRaw instead of where being used):
public function scopeOfFinalsSent($query, $set)
{
if ($set) {
return $query->whereNotNull('finals_sent')->where(function ($query) {
$query->whereRaw('final_weight >= current_weight')
->whereRaw('final_cbm >= current_cbm');
});
}
return $query;
}
Trying to get query results into an array. The example from the documentation shows this working...
let statement = try db.prepare(query)
var results = Array(statement)
...however when I try to compile this I get...
Ambiguous use of 'init'
... for the Array(statement)
If I change to this...
let statement = try db.prepare(query)
var results = Array<SQLite.Row>(statement)
I get this error...
Type of expression is ambiguous without more context
What can I do to make this work?
You need to iterate through the rows to put them into an array:
for statement in try db.prepare(query) {
yourArray.append(statement)
}
Hello everyone como estan will see I have this query in SQL
SELECT MAX(id_alternativa) FROM pregunta_alternativa WHERE id_pregunta = 7
which I'm doing in cakephp using the MAX function framework
public function register() {
$query = $this->PreguntasAlternativas->find()
->select([
'id_alternativa' => $query->func()->max(['id_alternativa'])
])->where(['id_pregunta' => '7']);
$this->set('alternatives',$query); }
but it shows this is the message Error
Error: Call to a member function func() on null File C:\xampp\htdocs\serapp\src\Controller\PreguntasAlternativasController.php Line: 14
as I see documentation so their functions are used
Sql Functions Cakephp
thanks for helping me
the solution was simple place it here, referring to the documentation
public function register() {
$query = $this->PreguntasAlternativas->find();
$query->select(['id_alternativa' => $query->func()->max('id_alternativa')
])->where(['id_pregunta' => '7']);
You must be setear variable $query
I have a list of checkboxes. I am trying to pass the list of selected checkboxes to a perl script. I am obtaining the list of checkboxes using the folliwng code :
function exec(){
var checkedValue = "";
var inputElements = document.getElementsByTagName('input');
for(var i=0; inputElements[i]; i++){
if(inputElements[i].className==="chk" &&
inputElements[i].checked){
checkedValue += inputElements[i].value;
if (inputElements[i+1])
checkedValue += ", ";
else
checkedValue += "";
}
}
I am then passing "checkedValue" to a perl script as follows :
self.location='/cgi-bin/ATMRunJob.pl?tcs='+checkedValue;
In the perl script, I read the array as follows :
our #testCasesToRun = $var->param("tcs");
This is then assigned to a hash as follows :
my $runSpec = {
TestCasesToRun => #testCasesToRun
};
However, I get the following error when I load the page in the browser :
Failed TestLimits() with error: [hash: k=TestCasesToRun, v=1,]:[array]:Value is not an array ref
In check against following TLS:
[
'hr',
{
'OptDefaults' => {
'JobRunningGroupName' => 'astbluetooth',
'RunMode' => 'Queue',
'CountTowardsReporting' => 1,
'JobOwnerGroupName' => 'astbluetooth',
'SelectSetupTeardown' => 1
},
'Optional' => {
'TestCasesToRun' => [
'ar',
undef,
undef,
[
'r',
1,
undef
]
],
I am new to perl as well as CGI scripting. How could I get around this error?
NOTE : All the code snippets have been shortened for brevity, but still portray the essence of the problem.
EDIT : What I want to do is this. The user selects a list of test cases from a checkboxed list that he wants to execute. I take the test case ids of all the selected test cases and pass it to a perl script. In the perl script, I just need to assign these selected testcase ids to the TestCasesToRun element in the runspec hash.
What would be the correct way to do that?
You are assigning an array as a hashkey value. That doesn't work; you need to assign an array ref:
my $runSpec = {
TestCasesToRun => \#testCasesToRun
};
Given that the code compiles, I have a feeling you just messed up your examples in the Q - please fix them to accurately reflect your code, even if they will be slightly less brief.
Your 'tcs' parameter is a single string (assigned via JS). Why are you then assigning results of param('tcs') to an array in the first place? Do you have a split somewhere in your code that you didn't include into the example?
Your dump contains an array reference within an array reference. You need to elaborate on what the expected structure of TestCasesToRun arrayref is, and show the code which processes it in the test runner.
As per your last comment:
Change your JavaScript code to join using simple comma: checkedValue += ",";
Change your Perl assignment to: our #testCasesToRun = split(/,/, $var->param("tcs"));
I had created the following table method in order to extract some specific table columns to allow later comparison to values stored on arrays:
public function findAllComposedExcelColumns()
{
$q = Doctrine_Query::create()
->select('p.branch_code, p.state_id, p.state_description, p.account, p.client_name')
->from('Process p');
return ($q->fetchArray());
}
But when I print an element of the retrieved array, it has also the property id which a don't need.
Array ( [0] => Array ( [id] => 1 [branch_code] => ... [state_id] => ... [state_description] => ... [account] => ... [client_name] => ... ) )
Why the id is also appearing on the results? There is any way to remove it?
Try hydrating with HYDRATE_SCALAR - it might give you what you want.
Eg.
public function findAllComposedExcelColumns()
{
$q = Doctrine_Query::create()
->select('p.branch_code, p.state_id, p.state_description, p.account, p.client_name')
->from('Process p');
$output = $q->execute(array(), Doctrine_Core::HYDRATE_SCALAR);
return $output;
}
edit:
note that this will also change your array keys,
eg from ['branch_code'] to ['p_branch_code']
using this type of hydration method is also not ideal when there are related records.
All in all, the above achieves your goal for this scenario. However, I would agree with DrColossos that it is better to simply loop over and ignore the data you don't want.