Parameterized DeleteAllByAttributes Doesn't Work in YII - yii

I am using the following code:
MyClass::model()->deleteAllByAttributes(array('phone_number'=>':phone_number'), '', array(':phone_number'=>$phoneNumber));
And I am getting the following error:
CDbException
SQLSTATE[HY093]: Invalid parameter number: number of bound variables
does not match number of tokens. The SQL statement executed was:
DELETE FROM `my_class` WHERE `my_class`.`phone_number`=:yp0
(E:\xampp\htdocs\yii\db\CDbCommand.php:354)

I believe you don't need to bind the attributes in the attributes array (as in findAllByAttributes() too). The values in the params array are bound to values in the condition string, not the attributes array, so I believe the following should work for you (and be sanitized):
MyClass::model()->deleteAllByAttributes(array(
'phone_number'=>$phoneNumber,
));
Alternatively, you could use:
MyClass::model()->deleteAllByAttributes(array(),'`phone_number` = :phone_number',array(
':phone_number'=>$phoneNumber,
));
Which would have the same effect... But then you might as well use deleteAll():
MyClass::model()->deleteAll('`phone_number` = :phone_number',array(
':phone_number'=>$phoneNumber,
));

Related

Excluding undefined Field Values in MariaDB TypeORM Queries

I have a basic Nest project using TypeORM. A model, Plate, has been contained in its own module and is imported into the main module.
The service for the Plate model contains the following method:
public query(params?: QueryPlateParams, limit: number = 100): Promise<Plate[]> {
const findOp = PlatesService.FindOperatorMap[params.type];
const whereObj = {
// If a find operation is defined, apply it to the value. Otherwise, just use the
// value itself.
id: findOp?.(params.value) ?? params.value,
available: params.isAvailable,
state: params.state
};
console.log(whereObj);
return this._platesRepository.find({
where: whereObj,
take: limit
});
}
The idea of this method is to take the query object, which could have any combination of the value, isAvailable, and state properties defined, then to apply the defined values to the query's WHERE clause with some modification, of course not defining filters for fields which had no value in the source query object.
When a query is made, the following logs are observed.
{ id: 'ABSOLVE', available: undefined, state: undefined }
query: SELECT `Plate`.`id` AS `Plate_id`, `Plate`.`state` AS `Plate_state`, `Plate`.`available` AS `Plate_available`, `Plate`.`lastChecked` AS `Plate_lastChecked` FROM `plates` `Plate` WHERE (`Plate`.`id` = ? AND `Plate`.`available` = ? AND `Plate`.`state` = ?) LIMIT 100 -- PARAMETERS: ["ABSOLVE",null,null]
What's important here is the parameter interpolation at the end of the second line: PARAMETERS: ["ABSOLVE",null,null]. The values for isAvailable and state are undefined, but the SQL statement translated them to null, but undefined and null are distinct values in Javascript/Typescript. The intent, of course, is to ignore undefined values in the query, not interpret them as null.
I found an option for the MongoDB connector that will ignore these undefined values. I do not see the same for the MariaDB/MySQL connector, which is what is being used for this service.
Is there any option to ignore these undefined values instead of interpreting them as null? I'd like to use the IsNull() or null values to explicitly check for SQL NULL.
Sadly you need just to not provide the undefined fields (add filter before passing whereObj into the find function.
Or you can read this thread with some other options dealing with the issue https://github.com/typeorm/typeorm/issues/2934
Basically - the issue is because creator thought that setting property of object to undefined removes the property from the object own properties therefore he treated undefined and null the same.

Drupal 7 - db_select: SQL function in where condition

I need to use this condition in my select statement:
WHERE YEAR(date) = YEAR(CURDATE())
but if I do, like this:
$query = db_select('table', 't');
$query->fields('t');
$query->condition('YEAR\(date\)', 'YEAR(CURDATE())', '=');
Drupal won't have it (even if I do not escape those parenthesis - it simply ignores them) because I get an error:
Column not found: 1054 Unknown column 'YEARdate' in 'where clause':
How to overcome this error?
Hmm.. just like this, it seems:
$query->where('YEAR(date) = YEAR(CURDATE())');
The where allows for the arbitrary SQL:
The where() method allows for the addition of arbitrary SQL as a conditional fragment. $snippet may contain any legal SQL fragment, and if it has variable content it must be added using a named placeholder. The $args array is an array of placeholders and values that will be substituted into the snippet. It is up to the developer to ensure that the snippet is valid SQL. No database-specific modifications are made to the snippet.
Hm, you could also use db_query, it allow you to write SQL queries "without Drupal".
I mean, you'll be able to add custom WHERE statements or any SQL-proper functions, like custom functions ;)
Eg.
$result = db_query('SELECT title FROM {node} WHERE type = "%s" AND title LIKE "%%%s%%"', 'type', 'title');
Use addExpression method :
https://api.drupal.org/api/drupal/includes!database!select.inc/function/SelectQuery%3A%3AaddExpression/7.x
$query = db_select('table', 't');
$query->fields('t');
$query->addExpression('YEAR(t.date) = YEAR(CURDATE())');
$result = $query->execute()->fetchAll();
var_dump($result);

Ruby array as parameter to a plain SQL Query

I'm building a command line application that needs to connect in various postgresql databases and execute different queries in the ones as Prepared Statements. In a specific query, I need to use the IN clause in conjunction with the ActiveRecord's connection_raw method. My code is so:
ActiveRecord::Base.connection_raw.prepare('read_publications', "UPDATE publications SET readed = TRUE WHERE id IN ($1);")
After, I try execute this query:
ActiveRecord::Base.connection_raw.exec_prepared('read_publications', [1,2,3,4])
The problem is this is not working. The following error is raised when the query runs:
no implicit conversion of Array into Integer
What I'm doing wrong? Exists a way in that I can convert this array to a value that the IN clause can understand?
If you are using a raw connection, you can't pass in arrays like you can with ActiveRecords. ActiveRecord does some preprocessing for you. If you need raw SQL, then you need a parameter for each array element.
arr = [1,2,3,4]
i = 1
param = []
arr.each { param.push(i); i+=1; }
sql = "UPDATE publications SET readed = TRUE WHERE id IN ($"+param.join(',$')+");"
ActiveRecord::Base.connection_raw.prepare('read_publications', sql)
ActiveRecord::Base.connection_raw.exec_prepared('read_publications', arr)
However, the documentation says the array parameters has to be in a certain format:
https://deveiate.org/code/pg/PG/Connection.html#method-i-exec_prepared
params is an array of the optional bind parameters for the SQL query. Each element of the params array may be either:
a hash of the form:
{:value => String (value of bind parameter)
:format => Fixnum (0 for text, 1 for binary)
}
See similar question: Prepare and execute statements with ActiveRecord using PostgreSQL

Passing pig macro parameters

I have a pig macro
define chop_massive (my, maxev) returns grouped, massive {
gr = foreach (group $my by id) generate group as id, $my;
split gr into
massive if COUNT($my) > $maxev,
grouped otherwise;
$grouped = grouped;
$massive = foreach massive generate id, COUNT($my) as $my;
};
my problem is with passing maxev parameter.
when I use a constant literal (e.g., 100L) all is fine.
however, I want to use $MAX_EVENTS specified using -param on the command line.
I tried
A, massive = chop_massive(A, $MAX_EVENTS);
and got
mismatched input '100L' expecting set null
I tried
A, massive = chop_massive(A, ($MAX_EVENTS));
and got
mismatched input '(' expecting set null
I tried
A, massive = chop_massive(A, '$MAX_EVENTS');
and got
Macro doesn't support user defined schema that contains name that conflicts with alias name: A
I tried
massive if COUNT($my) > $MAX_EVENTS,
inside the macro and got
Macro inline failed for macro 'chop_massive'. Reason: Undefined parameter : MAX_EVENTS
(this error is at least clear and reasonable).
So what should I do?
While I don't see it documented anywhere, it wouldn't surprise me if expressions are not supported as macro arguments. Why not just pass $EVENTS_PER_DAY and $DAYS as parameters to the macro (three parameters instead of two)?

Yii framework - picking up field value from other model

I have been struggling with this, i have two models and showing data in Cgridview with one model, this model contains some id's whose values are in different table
So, i have added
'value'=> 'TblAreaoflaw::model()->FindByPk($data->typeoflaw)->areaoflaw'
which is giving this error
"Trying to get property of non-object"
Might be due to this reason that the some records doesn't exist in the TblAreaoflaw. Can't we check in this line through isset?
When i put static value, it work well, like
'value'=> 'TblAreaoflaw::model()->FindByPk(5)->areaoflaw',
Could anyone please help
thanks a lot
The error you get is because this expression TblAreaoflaw::model()->FindByPk($data->typeoflaw) is returning null. This means that you are effectively trying to get null->areaoflaw which won't work (this is what the error message "Trying to get property of non-object" clarifies).
My best guess is that $data->typeoflaw returns a non-existing primary key for the TblAreaoflaw model.
Make sure :
TblAreaoflaw is actually a model, I doubt its Areaoflaw
You have database specified primary key which is the id (5) you are passing
Try:
'value'=> '(TblAreaoflaw::model()->FindByPk($data->typeoflaw)->areaoflaw) ?
: "default or null value"'
Obviously substitute the null string to whatever you want. You may need to adjust the condition to use !empty() or similar, but see how it goes. (And if you do that or aren't using PHP 5.3, use the full ternary expression.)