CodeIgniter Forums
No matching query: return empty key values instead null. Is this possible? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: No matching query: return empty key values instead null. Is this possible? (/showthread.php?tid=76550)



No matching query: return empty key values instead null. Is this possible? - YanKleber - 05-24-2020

Let's take this simple statement:

PHP Code:
$myarray $this->asArray()->where('id',$id)->first(); 

If it finds a record it will return a collection of key/value pairs.

I'd like to know if there is a way to make it return a collection of keys with empty values when it doesn't find a matching record instead of return null.

Thanks!



I figured it out. Not as straight forward as I wanted but it works finely. Here it goes in case someone else needs it:


PHP Code:
public static function getEmptyRecord($table) {
    $db = \Config\Database::connect();
    $builder $db->query("SHOW COLUMNS FROM $table");
    $result = [];
    foreach (json_decode(json_encode($builder->getResult()),true) as $row)
      $result[$row['Field']] = '';
    return $result;
  

Cool