Phalcon\Mvc\Model\Row: PHP isset() returns TRUE when value is NULL

The values of Phalcon\Mvc\Model\Row object can be access like an array; if a value in a PHP array is NULL, isset it shall return FALSE, however if a value in a Phalcon\Mvc\Model\Row object is NULL it will return TRUE. Is there anyone experienced the same issue?


update: I am using Phalcon 1.3


update: if I isset the NULL in the object way (->index), isset will return FALSE (correct); only when I access it like an array will give me TRUE



4.2k

What version of Phalcon (1.3.x or 2.0.0) are you using and do you have a code sample to show exactly how you found the issue so that we can try to replicate it?



11.9k
edited May '15

thanks for your reply @Steven. Below is the code (please set the pre tag to 1400px to get a better alignment):

<?php

    public function listAction(){
        $systemUser_loggedIn_authority = $this->systemUserLibrary->systemUser_loggedIn_authority_get();
        $model_find_columns            = $this->_list_model_find_columns_get($systemUser_loggedIn_authority);
        $data                          = $this->_list_data_get(array('model_find_columns'=>$model_find_columns));
        $this->view->setVar('customersOrStaffs', $data);
        $this->view->pick('customersOrStaffs/list');
    }
    private function _list_data_get($parameters){
        $staffs = Staffs::find(array(
            'conditions' => 'TRUE', // not ready
            'columns'    => $parameters['model_find_columns']['staffs']
        ));
        if(!$staffs->count()){ return NULL; }
        $data = array();
        foreach($staffs as $index => $staff){
            $data[] = array(
                'role'         => 'staff',
                'staff'        => $staff,
                'systemUser'   => $parameters['model_find_columns']['system_users']
                                      ? SystemUsers::findFirst(array(
                                            'conditions' => 'id = '.$staff->id__system_users, // not ready
                                            'columns'    => $parameters['model_find_columns']['system_users']
                                        ))
                                      : NULL,
                'personalInfo' => PersonalInfos::findFirst(array(
                                      'conditions' => 'id = '.$staff->id__personal_infos, // not ready
                                      'columns'    => $parameters['model_find_columns']['personal_infos']
                                  ))
            );
        }
        return $data;
    }
    private function _list_model_find_columns_get($systemUser_loggedIn_authority){
        switch($systemUser_loggedIn_authority){
            case 2:
                return array(
                    'staffs'         => 'id__system_users, id__personal_infos, id__staffs___supervisor, is_assistant,              note_for_customer',
                    'system_users'   => 'authority',
                    'personal_infos' => 'first_name,       last_name,          image,                                                                                          note_for_staff'
                );
            case 3:
            case 4:
                return array(
                    'staffs'         => 'id__system_users, id__personal_infos, id__staffs___supervisor, is_assistant, is_active,   note_for_customer',
                    'system_users'   => 'authority,        username,           language,                create_date,  last_update, is_active',
                    'personal_infos' => 'first_name,       last_name,          image,                   phone,        email,       address,          last_update, note_for_staff'
                );
            default:
                return array(
                    'staffs'         => 'id__system_users, id__personal_infos, id__staffs___supervisor, is_assistant,              note_for_customer',
                    'system_users'   => NULL,
                    'personal_infos' => 'first_name,       last_name,          image'
                );
        }
    }

/*
the staff row looks like this:
object(Phalcon\Mvc\Model\Row)#121 (6) {
      ["id__system_users"]=>
      string(1) "1"
      ["id__personal_infos"]=>
      string(1) "1"
      ["id__staffs___supervisor"]=>
      NULL
      ["is_assistant"]=>
      string(1) "0"
      ["is_active"]=>
      string(1) "1"
      ["note_for_customer"]=>
      NULL
    }
and then if I isset($data[0]['staff']['note_for_customer']); I will get TRUE;
*/

What version of Phalcon (1.3.x or 2.0.0) are you using and do you have a code sample to show exactly how you found the issue so that we can try to replicate it?



552
edited May '15

Isset() returns true on your array because the value is set (even if it is null). Try is_null(), it will work for your app since "empty" fields are returned with null



11.9k
edited May '15

actually isset shall return FALSE if NULL: http://php.net/manual/en/function.isset.php. Plus is_null may generate a PHP notice if the variable is not set, that's why sometimes isset is more appropriate.

Isset() returns true on your array because the value is set (even if it is null). Try is_null(), it will work for your app since "empty" fields are returned with null