Cakephp - how check row is null? - cakephp

I want to set flash message if there's not fullfilled row "surname" in database. How can I check if row surname is null ?
I tried to make something like this:
if(!isset(['User']['surname'])
or
if($user(['User']['surname']) === NULL
it obviously doesnt work

You can check it like.
<?php
if(!isset($user['User']['surname']) || empty($user['User']['surename'])){
// your code goes here
}

if(empty($user['User']['surname'])) {
// your code goes here
}

you can check like this
if(isset($user['surname']) || empty($user['surname'])){
$this->Session->setFlash('No Data.');
}

Related

how to filter data from array and apply conditions angular8

I am getting below sample data from api dynamically.
Can anyone suggest how to apply condition based on data.
You need to check values in array
if (this.tagSchemaDetails.schema_context.providers.indexOf('All') < 0) {
request['schema_context'] = this.createConditionsData();
}
As your providers is array of string and you are just comparing that with string so in all cases it will validate the condition.
So you have to check the strings in the array and compare them with 'All' to properly check the condition.
So your code should look like this:
if(!this.tabSchemaDetails.schema_context.providers.includes('All')){
request['schema_context']=this.createConditionsData();
}
as others have said here, you have to check values in the "providers" array. since you just need to check provider != 'All' you can simply check for the first element in the array as below.
if (this.tagSchemaDetails.schema_context.providers[0] !== 'All') {
request['schema_context'] = this.createConditionsData();
}

Check if a ListView has no data

After making a REST request, I want to check if the ListView dataSource has received data. My code looks to something like
if(this.state.dataSource.length == 0){
return (
<Text> No data </Text>
);
I always get an error telling me that this.state.dataSource.length is undefined. You can find a minimal example in this snack.
You can do it like :
if ( this.state.dataSource && this.state.dataSource.length == 0 ) {
// your code
}
You want to make sure this.state.dataSource has been initialized to an empty array when your component is created. That way, this.state.dataSource.length === 0 will be true until your data is fetched.

Cakephp 1.2 this->set not working

So I am setting a variable to be used in view.
This happens during a form POST. Maybe this can give some hints to someone.
public function confirm_card(){
if(!isset ($this->data['UserPayment']) && empty($this->data['UserPayment'])){
$this->Session->setFlash(__d('payments', 'Select payment method', true), 'flash_error');
$this->redirect($this->referer());
}
else{
foreach($this->data['UserPayment'] as $key=>$up){
if(!empty($up)){
$this->set(array('paytype'=>$key));
return;
}
}
}
}
And in view
echo $paytype;
Result in view
Notice (8): Undefined variable: paytype
The key is returned as it should be so no empty values there.
This should be very basic... I am missing something here?
Try with
$this->set('paytype', $key);
Change
$this->redirect($this->referer());
The problem was !empty($up) vs $up != '' ?
$up is normally 0 or 1 ?

CakePHP saveField NULL value in a INT column

I want to update one column which is an INT with a NULL value. I'm using CakePHP 2.3 with the saveField('field', 'value') method.
Here is my code :
$this->Customer->id = $customer_id;
if ($this->Customer->saveField('account_id', 'NULL')) {
//Do some stuff
}
So when I put 'NULL', the row is updated to 0. I tried NULL without quotes and the row is not updated.
EDIT :
The field in the db accepts NULL value.
Do you have any idea ?
Thanks
Do not pass 'NULL' as a string but as null value:
if($this->Customer->saveField('account_id', null))
{
//do some stuff
}
else
{
//do you get anything here ?
debug($this->Customer->validationErrors);
}
I've had a similiar problem caused by a Upload Behaviour that had validations preventing the saving. Check your models, it might help. I assume saveField returns true even when validation rules prevent saving.

How to alter field value drupal 7

I am working with drupal 7, and wanted to change the output of "number_float" value when it is "0.00". I have digged into field.api but has no clue what function to do.
To say it in plain English:
if the field type "number_float" and value is "0.00", print "empty value".
This is also to consider before views output.
Any hint or guidance would be very much appreciated.
Thanks
UPDATE:
I used hook_field_attach_view_alter. It does as expected, however I wonder if this is the right thing.
function mymodule_field_attach_view_alter(&$output, $context) {
foreach (element_children($output) as $field_name) {
$element = &$output[$field_name];
if ($element['#field_type'] == 'number_float' && $element['#formatter'] == 'number_decimal') {
foreach ($element['#items'] as $delta => $item) {
if ($element[$delta]['#markup'] == '0.00' || $element[$delta]['#markup'] == '0,00') {
$element[$delta]['#markup'] = t('Empty value message');
}
}
}
}
}
Any suggestion or betterment will be the answer.
thanks
A more standard Drupal way would be to manipulate the value in a preprocessor function. You can use hook_preprocess_HOOK for a theme function or template that's defined by another module. Inside of it, test for the 0.00 value and replace.
The update with hook_field_attach_view_alter is the way to go since nobody provides any other suggestion.

Resources