Re: How DataObjects Handles Nulls on Insert

From: Date: Thu, 17 Apr 2003 22:04:37 +0000
Subject: Re: How DataObjects Handles Nulls on Insert
References: 1  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-4940@lists.php.net to get a copy of this message
I had the same situation. One solution would be to subclass DataObject::setFrom in your extended base class so that it would first call parent::setFrom(), ie, the real one, then walk through the $from array and fix up the nulls, zeros, blanks, etc. as needed. Then, your actual data objects, which would be derived from your base class, would get your custom setFrom. So the whole thing is like this: class DB_DataObject { function setFrom() {...} // the original } class MyBase extends DB_DataObject { function setFrom() {...} // your version, see below } class DataObjects_MyFoo extends MyBase { ... } class DataObjects_MyBar extends MyBase { ... } ... Here's my setFrom, but I'm sure the experts would find something to improve. /** * override this DB_DataObject::setFrom because it creates * 0's for empty keys; not good for search queries. * * @param array | object $from * @access public * @return boolean , true on success */ function setFrom(&$from) { $res = parent::setFrom($from); if ($res !== true) { return $res; } $items = $this->_get_table(); foreach ($items as $fieldname=>$fieldtype) { if (!strcmp('0', $this->$fieldname)) { $this->$fieldname = null; } elseif (empty($this->$fieldname)) { $this->$fieldname = null; } } return true; } Hth, -Mitch On Thu, 2003-04-17 at 12:31, Peter Meth wrote: > I am using DataObjects 0.17 and I have some fields which are numeric. I > have a form for adding a new record and a handler which creates the > desired DataObject and uses setFrom($_POST). It then does an insert on > the object. I have noticed that if I do not specify a value for the > numeric fields, they are inserted as 0, when in fact, I would like them > inserted as NULL (since this is different to me). Does DO have this > capability? If not, is it something that can be added? Is this also > the same behaviour for strings, with "" being inserted (I have not > checked)? Is there a work-around? -- Mitchell Perilstein mitch at enetis dot net www.enetis.net/~mitch

« previous php.pear.general (#4940) next »