->setFrom()
Synopsis
boolean $DB_DataObject->setFrom (
array or object $from
,
string $format = '%s'
,
bool $skipEmpty
= false
)
Описание
Copies items that are in the table definitions from an array or object into the current object (It will not override key values). This can be used to process form posts (if the field names match the database), or cloning similar objects.
You can not set the value of a key column using setFrom, It is silently ignored for security reasons. (you can still assign it manually)
setFrom will attempt to call the setters set{columnname} methods if they exist, It will also call fromValue(), which formats dates correctly.
You may realize that this method overlaps the overloaded method for the column name from, due to this, the associated methods for the column name 'from' are set_from and getFrom.
Parameter
-
array or Object $from
- what to copy from -
string $format
- the format of the array or object variables and how they relate to this object. For example if your input array is in the format prefix_COLNAME, then you can use 'prefix_%s'. -
bool $skipEmpty
- If set to true, DB_DataObject will not assign empty values if a column is empty (eg. '' / 0 etc)
Return value
array or boolean
- TRUE on success or an array of set*() return values in PHP4.3.2 upwards
Замечание
This function can not be called statically.
Пример
Using setFrom()
<?php
// Person contains name,age
// $_POST contains 'name'=>'fred', 'age'=>'22'
$person = new DataObjects_Person;
$person->get(12);
$person->setFrom($_POST);
$person->update();
// or using the formating
// person contains name,age
// $_POST contains 'person_name'=>'fred', 'person_age'=>'22'
$person = new DataObjects_Person;
$person->get(12);
$ret = $person->setFrom($_POST,'person_%s');
// use the return value from setFrom to test for errors (PHP4.3.2)
if ($ret === true) {
$person->update();
} else {
// $ret is an array or strings..
echo 'There were some errors: ' . implode(', ', $ret);
}
// or copying from another object
$personA = new DataObjects_Person;
$personA->get(12);
$personB = new DataObjects_Person;
$personB->get(14);
$personA->setFrom($personB);
$person->update();
?>