class Model

Models in the system can be described only for existing ORM objects and have the same name.

Models are singletons. Models can be of two types:

  • a virtual model - each ORM object has its virtual model that contains a standard set of methods (the model does not exist as a file);
  • an extended model - a model declared as a class that extends the functionality of the standard model; unlike the virtual model, it can be instantiated using the getInstance method (the class should be inherited from the class Model).

Be extremely careful and cautious when describing your own models, the model system uses late static binding.

A Model is created the following way:

Model :: factory ($ objectName);

Extended (real) models may also have the following syntax:

Model_Page :: getInstance ();

Basic methods:

class Model 
{
    /**
     * Database object  storage
     * @var Db_Object_Store
     */
    protected $_store;
	
    /**
     * Adapter connecting to the database
     * @var Zend_Db_Adapter_Abstract
     */
    protected $_db;

    /**
     * Db_Object configuration
     * @var Db_Object_Config
     */
    protected $_objectConfig;
	
    /**
     * Object / model name (Db_Object)
     * @var string
     */
    protected $_name;	

    /**
     * Hard caching time (in seconds) for frontend (without validation)	 
     * @var integer
     */
    protected $_cacheTime;

    /**
     * Cache adapter or false
     * @var Current Cache_Interface
     */
    protected $_cache;
   /**
    * DB connections manager (since 0.9.1)
    * @var Db_Manager_Interfacee
    */
    protected $_dbManager
    /**
     * Set database table prefix
     * @param string prefix
     */	 	 	 	
    static public function setDbPrefix($prefix)
   	 	 	 	
    /**
     * Get database table prefix
     * @return string
     */
    static public function getDbPrefix()
	 	 	 		 	 	 	
    /**
     * Set caching interface
     * @param Cache_Interface $cache or false
     */
    static public function setDataCache($cache)
	 	 	 	
    /**
     * Set global database connector (for all models)
     * @param Zend_Db_Adapter_Abstract $db
     */
    static public function setGlobalDbConnection(Zend_Db_Adapter_Abstract $db)
	 	 	 	
    /**
     * Set hard cache lifetime (for all models)
     * @property integer $time
     */
    static public function setGlobalHardcacheTime($time)
	 	 	 	
    /**
     * Replace database connector for the model 
     * @param Zend_Db_Adapter_Abstract $db 
     */  
    public function setDbConnection(Zend_Db_Adapter_Abstract $db)
		 	 	 	
    /**
     * Set hard cache lifetime for a model
     * @param integer $time
     */
    public function setHardCacheTitme($time)

    /**
     * Get database connector for the current model
     * @return Zend_Db_Adapter_Abstract
     */
    public function getDbConnection()

    /**
     * Get the name of the object, which the model refers to
     * @return string
     */
    public function getObjectName()

    /**
     * Get key for cache
     * @param array $ params - parameters can not contain arrays, objects and resources
     * @return string
     */
    public function getCacheKey(array $params)

    /**
     * Get the name of the database table (with prefix)
     * @return string
     */
    public function table()

    /**
     * Get record by id
     * @param integer $id
     * @param array $fields — optional — the list of fields to retrieve
     * @return array / false
     */
    public function getItem($id , $fields = '*')

    /**
     * Get a number of entries a list of IDs
     * @param array $ids - list of IDs
     * @param array $fields - optional - the list of fields to retrieve
     * @return array / false
     */
    final public function getItems(array $ids , $fields = '*')

    /**
     * Get object data using cache
     * @param integer $id - object identifier
     * @return array
     */
    public function getCachedItem($id)
 	 	 	
    /**
     * Get object data using cache, search by unique field
     * @param string field - field name
     * @param string value - field value
     * @return array
     */
    public function getCachedItemByField($field , $value)
	 	 	 	
    /**
     * Get object data, search by unique field
     * @param string $fieldName
     * @param string $value
     * @param array $fields - optional
     * @return array
     */
    public function getItemByUniqueField($fieldName , $value , $fields = '*')

    /**
     * Add filters (where) to the query
     * @param Zend_Db_Select $sql
     * @param array $filters  key — field name, value
     * @return void
     */
     public function queryAddFilters(Zend_Db_Select $sql , $filters)

    /**
     * Add pagination parameters to a query
     * Used in CRUD-controllers for list pagination and sorting
     * @param Zend_Db_Select $sql
     * @param array $params — keys: start,limit,sort,dir
     * @return void
     */
    static public function queryAddPagerParams(Zend_Db_Select $sql , $params)

    /**
     * Transfer an array to a list of integers for inserting into an SQL-query form,
     * is used to improve the performance of Zend_Select queries setup
     * @param array $ids
     * @return string
     */
    static public function listIntegers(array $ids)
   
    /**
     * Get a number of objects (rows in a table)
     * @param array $ filters - optional - filters (where) the key - the field name, value
     * @param string $ query - optional - search query - search query
     * @param boolean $ useCache - use hard cache
     * it is necessary to remember that hard cache gets invalidated only at the end of its life cycle 
     *(configs / main.php), is used in case update triggers can’t be applied
     * @return integer
     */
    public function getCount($filters = false , $query = false , $useCache = false)

    /**
     * Get a list of records
     * @param array $ params - optional parameters array ('start' => 0, 'limit' => 10, 'sort' => 'fieldname', 
     * 'dir' => 'DESC')
     * @param array $ filters - optional filters (where) the key - the field name, value
     * @param array $ fields - optional list of fields to retrieve
     * @param boolean $ useCache - use hard cache
     * it is necessary to remember that hard cache gets invalidated only at the end of its life cycle 
     * (configs / main.php), is used in case update triggers can’t be applied
     */
    public function getList($params = false, $filters = false , $fields = '*' , $useCache = false )
  
    /**
     * Get a list of records (is used by CRUD_VC controllers)
     * @ param array $ params - parameters
     * array ('start' => 0, 'limit' => 10, 'sort' => 'fieldname', '
     * dir' => 'DESC')
     * @param array $ filters - filters
     * @param string $ query - optional string for search
     * @param mixed $ fields - optional list of fields
     * @param string $ author - optional key for storing entry author id
     * @param strong $ lastEditor - optional key  for storing the last editor’s ID
     * @param array $ joins - optional, inclusion config for Zend_Select:
     * array(
     *          array(
     *                'joinType'=>   joinLeft/left , joinRight/right , joinInner/inner
     *                'table' => array / string
     *                'fields => array / string
     *                'condition'=> string
     *          )...
     * )
     */
    public function getListVc($params = false , $filters = false , $query = false , $fields = '*' ,
 $author = false , $lastEditor = false , $joins = false)

    /**
     * Delete record
     * @param integer $ recordId -  record ID
     * @param boolean $ log - log changes
     * /
    public function remove ($recordId, $log = true)     

    /**
     * Check whether the field value is unique
     * @param integer $ recordId - record ID
     * @param string $ fieldName - field name
     * @param mixed $ fieldValue - field value
     * @return boolean
     */
    public function checkUnique($recordId , $fieldName , $fieldValue)

    /**
     * factory method of model instantiation
     * @param string $ objectName - the name of the object in ORM
     * @return Model
     */
    static public function factory($objectName)

    /**
     * Set DB_Object storage adapter used by default
     * @param Db_Object_Store $store
     * @deprecated since 0.9.1
     */
    static public function setGlobalObjectStore(Db_Object_Store $store)

    /**
     * Set storage adapter for model object
     * @param Db_Object_Store $store
     */ 	 	 	
    public function setObjectsStore(Db_Object_Store $store)
	
    /**
     * Get storage adapter
     * @return Db_Object_Store
     */
    public function getStore()

    /**
     * Get primary key name
     * @return string
     */
    public function getPrimaryKey()

    /**
     * Set default DB connections manager (version 0.9.1 and higher)
     * @param Db_Manager_Interface $manager
     */
    static public function setDefaultDbManager(Db_Manager_Interface $manager)

    /**
     * Set DB connections manager for the current model (version 0.9.1 and higher)
     * @param Db_Manager_Interface $manager
     */
    public function setDbManager(Db_Manager_Interface $manager)

    /**
     * Set default logging adapter
     * @param Log $log
     */
    static public function setDefaultLog(Log $log)

    /**
     * Set current logging adapter (version 0.9.2.5 and higher)
     * @param mixed Log | false $log
     */
    public function setLog($log)

    /**
     * Log error message (version 0.9.2.5 and higher)
     * @param string $message
     */
    public function logError($message)
}

Example:


$data = Model::factory('News')->getList(
          array(
                'sort'=>'news_date',
                'dir'=>'DESC',
                'start'=>0,
                'limit'=>10
          ),
          array('published'=>true),
          array('id','title','news_date'),
          true
);

comments powered by Disqus