class Db_Object (ORM objects)

ORM objects represent the fundamental class for working with ORM data.

Once, the ORM object structure has been described, the object data may be handled by means of the Db_Object class:

class Db_Object {
/**

* the object constructor takes its name and identifier,
* (the parameter is not required), if absent,
* there will be created a new object. If ORM lacks the object with the specified
* identifier, an Exception will show up
* Using this method is highly undesirable,
the factory method Db_Object::factory() is more advisable to use
* @param string $name
* @param integer $id - optional
* @throws Exception
*/
public function __construct($name, $id = false)

/**
* Get object fields with their type descriptions
* @return array
*/

public function getFields()

/**
* Get the object data, returns the associative array ‘field name’
* «property name» => «property value»
* @return array
*/

public function getData()

/**
* Get object name
* @return string
*/

public function getName()

/**
* Get object identifier
* @return integer
*/

public function getId()

/**
* Get the full name of the database storing the object data (with prefix)
* @return string
*/

public function getTable()

/**
* Check if there are object property changes
* not saved in the database
* @return boolean
*/

public function hasUpdates()

/**
* Get ORM configuration object (data structure helper)
* @return Db_Object_Config
*/

public function getConfig()

/**
* Get updated, but not saved object data
* @return array
*/

public function getUpdates()

/**
* Set the object identifier
* @param integer $id
*/

public function setId($id)

/**
* Commit the object data changes (without saving)
* @return void
*/

public function commitChanges()

/**
* Check if the object field exists
* @param string $name
* @return boolean
*/

public function fieldExists($name)

/**
* Get the related object name for the field
* (available if the object field is a link to another object)
* @param string $field - field name
* @return string
*/

public function getLinkedObject($field)

/**
* Check if the listed objects exist
* @param string $name
* @param mixed integer/array $ids
* @return boolean
*/

static public function objectExists($name , $ids)

/**
* Set the object properties using the associative array of fields and values
* @param array $values
* @throws Exception
* @return void
*/

public function setValues(array $values)

/**
* Set the object field val
* @param string $name
* @param mixed $value
* @throws Exception
*/

public function set($name , $value)

/**
* Get the object field value
* In case the field was updated, returns the new value
* @param string $name - field name
* @throws Exception
* @return mixed
*/

public function get($name)

/**
* Get the initial object field value (received from the database)
* whether the field value was updated or not
* @param string $name - field name
* @return mixed
*/

public function getOld($name)

/**
* Save changes
* @param boolean $log  - log changes
* @param boolean $useTransdaction — using a transaction when changing data is optional.
* If data update in your code is carried out within an external transaction
* set the value to  false,
* otherwise, the first update will lead to saving the changes
* @return boolean;
*/

public function save($log = true , $useTransdaction = true)

/**
* Deleting an object
* @param boolean $useTransdaction — using a transaction when changing data is optional.
* If data update in your code is carried out within an external transaction
* set the value to  false,
* otherwise, the first update will lead to saving the changes
* @return boolean - success
*/

public function delete($useTransdaction = true)

/**
* Validate unique fields, object field groups
* Returns errors array or returns false, is used for ExtJS forms
* @property boolean $new
* @return mixed false / array
*/

public function validateUniqueValues()

/** Factory method of object creation is preferable to use, cf. method  __construct() description
* @param string $name — object name
* @param integer $id — object identifier
* @return Db_Object
*/

static public function factory($name , $id = false)

}

EXAMPLE:

$news = new Db_Object('news');

$news->title = 'News 1';

$news->text = 'News content';

$news->news_date = date('Y-m-d');

$news->save();

As the above example shows, the object values (fields) are defined by a regular method. In case we are trying to mention an non-existing parameter or define a value not matching the configuration type, an exception will show up. The data is saved by calling the Save method.

comments powered by Disqus