Skip to content

Commit 79d4fd2

Browse files
authored
Nullable support for __set.
I wrote it and works like a charm. Slightly more efficient, because I some of array_key_exists don't exist anymore ;). But... I noticed an issue when I am trying to save this entity. Property which is set to null is excluded from INSERT what should cause problem if default column value is not set to null but different value - in that case we cannot save null during insert. In my case should be: ``` INSERT INTO `t_table_search` (`dcs_identifier`, `dcs_conditions`, `dcs_dsc_id`) VALUES ('e77d630b7be1484cb4b7b4f8dbc15c45', '{\"v-school\":[\"1\"],\"v-center\":[\"44\"]}', NULL) // dcs_dsc_id = null; ``` it is: ``` INSERT INTO `t_table_search` (`dcs_identifier`, `dcs_conditions`) VALUES ('e77d630b7be1484cb4b7b4f8dbc15c45', '{\"v-school\":[\"1\"],\"v-center\":[\"44\"]}')// no null for dcs_dsc_id ``` I think it is because of entity value of property in _original is the same as actual value of property (i.e.: dcs_dsc_id == null) ```object(App\Entities\entityname)#40 (7) { ["dcs_identifier":protected]=> string(32) "e77d630b7be1484cb4b7b4f8dbc15c45" ["dcs_conditions":protected]=> string(52) "{"driving-school":["1"],"examination-center":["44"]}" ["dcs_dsc_id":protected]=> NULL ["dcs_updated_at":protected]=> NULL ["_options":protected]=> array(3) { ["casts"]=> array(3) { ["dcs_identifier"]=> string(6) "string" ["dcs_conditions"]=> string(10) "json-array" ["dcs_dsc_id"]=> string(11) "?json-array" } ["dates"]=> array(2) { [0]=> NULL [1]=> string(14) "dcs_updated_at" } ["datamap"]=> array(0) { } } ["_original":protected]=> array(4) { ["dcs_identifier"]=> NULL ["dcs_conditions"]=> NULL ["dcs_dsc_id"]=> NULL ["dcs_updated_at"]=> NULL } ["_cast":"CodeIgniter\Entity":private]=> bool(true)``` I saw PR month or two ago which was changing entity saving but IMHO it should be turned ON for update or replace but should NOT work for INSERT.
1 parent 3a4ade9 commit 79d4fd2

1 file changed

Lines changed: 25 additions & 10 deletions

File tree

system/Entity.php

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -267,24 +267,38 @@ public function __set(string $key, $value = null)
267267
$value = $this->mutateDate($value);
268268
}
269269

270-
// Array casting requires that we serialize the value
271-
// when setting it so that it can easily be stored
272-
// back to the database.
273-
if (array_key_exists($key, $this->_options['casts']) && $this->_options['casts'][$key] === 'array')
270+
$is_nullable = false;
271+
$cast_to = false;
272+
273+
if(array_key_exists($key, $this->_options['casts']))
274274
{
275-
$value = serialize($value);
275+
$is_nullable = substr($this->_options['casts'][$key],0,1) === '?';
276+
$cast_to = $is_nullable ? substr($this->_options['casts'][$key], 1) : $this->_options['casts'][$key];
276277
}
277278

278-
// JSON casting requires that we JSONize the value
279-
// when setting it so that it can easily be stored
280-
// back to the database.
281-
if (function_exists('json_encode') && array_key_exists($key, $this->_options['casts']) && ($this->_options['casts'][$key] === 'json' || $this->_options['casts'][$key] === 'json-array'))
279+
if(!$is_nullable || !is_null($value))
282280
{
283-
$value = json_encode($value);
281+
// Array casting requires that we serialize the value
282+
// when setting it so that it can easily be stored
283+
// back to the database.
284+
if ($cast_to === 'array')
285+
{
286+
$value = serialize($value);
287+
}
288+
289+
// JSON casting requires that we JSONize the value
290+
// when setting it so that it can easily be stored
291+
// back to the database.
292+
if (($cast_to === 'json' || $cast_to === 'json-array') && function_exists('json_encode'))
293+
{
294+
$value = json_encode($value);
295+
}
296+
284297
}
285298

286299
// if a set* method exists for this key,
287300
// use that method to insert this value.
301+
// *) should be outside $is_nullable check - SO maybe wants to do sth with null value automatically
288302
$method = 'set' . str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $key)));
289303
if (method_exists($this, $method))
290304
{
@@ -304,6 +318,7 @@ public function __set(string $key, $value = null)
304318
return $this;
305319
}
306320

321+
307322
//--------------------------------------------------------------------
308323

309324
/**

0 commit comments

Comments
 (0)