Setting Object Fields
Once you've got an Object, you can change the values of the record by setting fields. Setting the fields only changes the values in the object, and stores them until you save the object to the database. Setting can be done multiple ways.
set
The primary way is via the set method:
$myBox->set('width',10); $myBox->set('height',4);
set() also takes a 3rd $vType parameter, which is either a string indicating the format of provided value, or a callable function that should be used to set the field value, overriding the default behavior. For example:
/* Set date as an unix timestamp integer rather than a string date */ $myBox->set('birthday',432432543,'integer'); /* Set dimension of box based on width and height via custom function */ function setDim($k,$v,&$obj) { $w = $obj->get('width'); $h = $obj->get('height'); $dim = $w * $h; $obj->set($k,$dim); } $myBox->set('dimension','','setDim');
fromArray
Another way Objects can be set in xPDO is through the fromArray() method:
$myBox->fromArray(array( 'width' => 5, 'height' => 10, ));
xPDOObject::fromArray takes 5 parameters in total. We've seen the first in use. The second is $keyPrefix, which when set, will strip the passed value from the array you are passing in the first parameter. A good example of this is when passing $_POST vars to an Object:
// Let us say that: // $_POST = array( // 'test_w' => 12, // 'test_h' => 13, // ); $myBox->fromArray($_POST,'test_'); echo $myBox->get('w'); // prints '12'
The third parameter, $setPrimaryKeys, is a boolean value that defaults to false. When set, it will allow Primary Keys in the object to be set. This is useful for creating new objects that you want to specify the ID of:
$myBox = $xpdo->newObject('Box'); $myBox->fromArray(array( 'id' => 23, 'width' => 5, 'height' => 5, ),'',true); echo $myBox->get('id'); // prints '23'
The fourth parameter, $rawValues, is set to false by default. If true, the object will set its values without calling set() internally. What does this mean? Well, it means field type validation wont happen; nor will that field be set as 'dirty'.
The fifth parameter, $adhocValues, when true, will automatically set any passed values as object vars, regardless of whether or not they are actually fields in the object. For example:
$myBox->fromArray(array( 'width' 5', 'notRealField' => 'boo', ),'',false,false,true);
save
This function allows you to save your set fields.
$myBox->save();
This will immediately execute the UPDATE (or CREATE) query that will save the record to the database.
Suggest an edit to this page on GitHub (Requires GitHub account. Opens a new window/tab).