Re: DataObjects
| From: | Xavier | Date: | Tue, 20 May 2003 07:30:55 +0000 |
| Subject: | Re: DataObjects | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-5449@lists.php.net to get a copy of this message | ||
Barry Steele wrote:
hi, hi
Does anyone have some examples of extending the Validation part of a DataObject? I'm a little slow and the stuff in the manual is not shining a great light on my issue. Essentially it's this:- I have an Actvity (in my case an audit of beach safety) which is linked to a beach and is a long transaction (that is it can take weeks). what I want to do is ensure that when a new audit is entered it cant include a beach that is already subject to an open audit. I've built an insert form that makes this easy but it is still possible for a user to create a new audit by using the back arrow and resubmitting the existing data. Well, i think the better way to avoid duplicate inserts would be delegating this job to your backend RDBMS, adding a unique key ,then on duplicate insert, the method would return a false because of an already existing tuple. You could too replace the insert() method by your own , which would include an integrity check, a thing like : function insert() {
$check = new object();
$check->whereAdd('name = '.$this->name);
$check->whereAdd('2ndkey = '.$this->2ndkey);
if ($check->find() > 0) {
return false;
}
return parent::insert();} Or using the same kind of things but out of the insert method(); a thing like: function isInDb() {
$check = new object();
$check->whereAdd('name = '.$this->name);
$check->whereAdd('2ndkey = '.$this->2ndkey);
if ($check->find() > 0) {
return true;
}
return false;
}
And then calling this method before insert();
$myobj = new object;
$myobj->setFrom($_POST);
if (!$myobj->isInDb()) {
$myobj->insert();} But maybe, there is a better way to do that ? Alan ?
how do I reference Dept details within the Employee object after a get_link() ? for example emp = new DataObject_Employee; emp->get(123); emp->get_links(); print ($emp->dept->deptName); ??? - I tried thi sonce and failed and could figure it out The way to use links , assuming you've correctly feed the links.ini file, is:$emp = new DataObject_Employee; $emp->get(123); $dept = $emp->get_link('dept'); print ($dept->deptName); ?? OR using getlinks: $emp = new DataObject_Employee; $emp->get(123); $emp->get_links(); print ($emp->_dept->deptName);
^^^^
Thanks BarryRegards -- Xavier