DB_DataObject and NULL values
| From: | Chris Keating | Date: | Tue, 01 Jul 2003 17:05:57 +0000 |
| Subject: | DB_DataObject and NULL values | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-17912@lists.php.net to get a copy of this message | ||
hey I've come across a little problem while trying to update a row in
the database with a NULL value.
what I'm trying to do is basically this:
$dbObject->Col1 = null;
$dbObject->Col2 = null;
$dbObject->update();
But what ends up happening is that no SQL gets generated for the 2
columns I've set to null. (code snippet pasted below) This is because
the isset check on line 756 fails, and the is equal to null check on
line 768 is never reached. I've done some tinkering and it appears that
unset($var) and $var = null have both the same meaning in php.
Is this a problem that can be corrected by tweaking some php settings? I
haven't found anything in the unset() or isset() documentation that
points to this being a possibility.
Regardless of whether or not this can be fixed by a minor php tweak, I
think the "correct" solution is to create a simple dbNULL class. This
would look something like this:
$dbObject->Col1 = dbNULL;
$dbObject->Col2 = dbNULL;
$dbObject->update();
DataObject.php:
768 if ($this->$k === dbNULL) {
769 $settings .= "$k = NULL";
770 }
So now the !isset check would fail since $this->$k is actually set to
something and the check to see if $this->$k should be NULL in the
database would succeed.
Am I on the right track here or have people found a better solution?
--chris
This is from DB_DataObject-0.19.
DataObject.php:
735 function update($dataObject = false)
736 {
737 global $_DB_DATAOBJECT;
738 // connect will load the config!
739 $this->_connect();
740
741 $items = $this->_get_table(); 742 $keys = $this->_get_keys();743
744 if (!$items) {
745 DB_DataObject::raiseError("update:No table definition
for {$this->__table}", DB_DATAOBJECT_ERROR_INVALIDCONFIG);
746 return false; 747 } 748 $datasaved = 1; 749 $settings = '';750 751 752
753 $__DB =&$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; 754
755 foreach($items as $k => $v) {
756 if (!isset($this->$k)) {
757 continue;
758 }
759 if (($dataObject !== false) && (@$dataObject->$k ==
$this->$k)) {
760 continue; 761 }762 763
764 if ($settings) {
765 $settings .= ', ';
766 }
767 /* special values ... at least null is handled...*/
768 if ($this->$k === null) {
769 $settings .= "$k = NULL";
770 }