Converting '' to NULL in doctrine and symfony - sql

Im using the symfony framework with mysql.
I have a field in mysql that is generated by doctrine as:
weight: { type: double, notnull: false, default: NULL }
`weight` double(18,2) NULL DEFAULT NULL
The value is entered using a textbox and the generated sql trys to insert '' into this field if no value is given.
This produces the following error:
SQLSTATE[01000]: Warning: 1265 Data truncated for column 'weight' at row 1
How would change this value such that a Doctrine_Null gets used instead?
Also how would i be able to retrieve '(unknown)' for display purposes if the field is null?
Thanks

maybe you could try to use a validator on your form, like sfValidatorNumber ?
http://www.symfony-project.org/api/1_4/sfValidatorNumber

This should happen in your Form/Validation class. If you are expecting to have empty string values submitted then you need to convert those to null as part of this process.
As far as retrieving "unknown" for display i would probably do this as a custom getter on the model class.

Related

How to define get method in Anypoint studio if queryParam is defined?

Im trying to make a get method in Anypoint studio. I have already defined raml file with get method that looks something like this:
/kupci:
get:
queryParameters:
active:
required: false
enum:
- "true"
- "false"
I want to make a get flow that returns data from mysql database with 2 options:
if i have defined queryParam return everything from the database based of that condition
if not defined, just return everything from database
You just need a simple if else condition wherever you are creating your select query, and add a where clause if active is non empty.
SELECT field1, field2, ... FROM table
++ if(!isEmpty(attributes.queryParams.active)) 'WHERE active = $(attributes.queryParams.active)'
else ''
Note: You might need to change the above query if the datatype of the column active is Boolean. For example:
WHERE active IS $(attributes.queryParams.active)
You need to wrap this around the #[] script tag when writing in db:select as mentioned in the mule database connector docs

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.

Add field/string length to logstash event

I'm trying to add a string length field to an index. Ideally, I'd like to use the kibana script feature as I can 'add' this field later but I keep getting a null_pointer_exception with the following code... I'm trying to sort in a visualization based on the fields length.
doc['field'].value ? doc['field'].length() : 0
Is this correct?
I thought it was because my field isn't always set (sparse data), but I added the ?:0 to combat that (which didn't work)
Any ideas?
You can define an scripted field in Kibana, of type int, language painless, and try this:
return (doc['field'].value != null? doc['field'].value.length(): 0);

How to get objects from worklight JSONStore where value of specific attribute attribute is null or empty

How can I get all the objects from JSONSTore where the value of specified attribute is null.
I have tried to specify the value of attribute as empty or null but not able to get the result.
I have to implement the below SQL query in IBM Worklight to get data from JSONStore.
select * from employee where age IS NULL
Make sure you have something like this in your search fields: {value: 'string'}.
Then add data like this: WL.JSONStore.get('collection').add({value: 'NULL'}).
Then search for it like this: WL.JSONStore.get('collection').find({value: 'NULL'}).
It is not currently possible to search for things that are not string, integer, number or boolean. Notice that 'NULL' is a string in my examples above.
Feature requests here.

Prestashop and null fields in DB

I have a very simple piece of code :
$dropOrder = new DropOrder($dropOrderId);
$dropOrder->is_supplier_paid = $payValue;
$dropOrder->save();
It works and saves a 'is_supplier_paid' field value into the database. But it also does unexpected actions and fills all null valued fields with 0 values.
I try to save it like this :
$dropOrder->save(true);
But I still have the same strange behavior. I want to change one field only and don't touch the other ones.
Values are formated by ObjectModel::formatValue() before they are inserted / updated, based on the type of the field declared in your $definition.
You have to use TYPE_NOTHING to allow NULL values, it's the only way.
Take a look in Configuration class, with id_shop and id_group_shop fields.
PrestaShop 1.7:
I ran into the same problem in PS 1.7 and set TYPE_NOTHING is not sufficient to resolve this issue. In my case i also needed to add allow_null to true in the field's definition:
'my_field' => ['type' => self::TYPE_NOTHING, 'allow_null' => true, 'value' => null]
('value' => null is probably not necessary but suggested)