Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Joomla-Framework

License

Content on this site is copyright © 2005 - 2008 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5. Some parts of this website may be subject to other licenses.
Source code for file /joomla/application/component/model.php

Documentation is available at model.php

  1. <?php
  2. /**
  3. @version        $Id: model.php 9764 2007-12-30 07:48:11Z ircmaxell $
  4. @package        Joomla.Framework
  5. @subpackage    Application
  6. @copyright    Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
  7. @license        GNU/GPL, see LICENSE.php
  8. *  Joomla! is free software. This version may have been modified pursuant
  9. *  to the GNU General Public License, and as distributed it includes or
  10. *  is derivative of works licensed under the GNU General Public License or
  11. *  other free or open source software licenses.
  12. *  See COPYRIGHT.php for copyright notices and details.
  13. */
  14.  
  15. // Check to ensure this file is within the rest of the framework
  16. defined('JPATH_BASE'or die();
  17.  
  18. /**
  19.  * Base class for a Joomla Model
  20.  *
  21.  * Acts as a Factory class for application specific objects and
  22.  * provides many supporting API functions.
  23.  *
  24.  * @abstract
  25.  * @package        Joomla.Framework
  26.  * @subpackage    Application
  27.  * @author        Johan Janssens <johan.janssens@joomla.org>
  28.  * @author        Louis Landry <louis.landry@joomla.org>
  29.  * @author         Andrew Eddie
  30.  * @since        1.5
  31.  */
  32. class JModel extends JObject
  33. {
  34.     /**
  35.      * The model (base) name
  36.      *
  37.      * @var string 
  38.      * @access    protected
  39.      */
  40.     var $_name;
  41.  
  42.     /**
  43.      * Database Connector
  44.      *
  45.      * @var object 
  46.      * @access    protected
  47.      */
  48.     var $_db;
  49.  
  50.     /**
  51.      * An state object
  52.      *
  53.      * @var string 
  54.      * @access    protected
  55.      */
  56.     var $_state;
  57.  
  58.     /**
  59.      * Constructor
  60.      *
  61.      * @since    1.5
  62.      */
  63.     function __construct($config array())
  64.     {
  65.         //set the view name
  66.         if (empty$this->_name ))
  67.         {
  68.             if (array_key_exists('name'$config))  {
  69.                 $this->_name = $config['name'];
  70.             else {
  71.                 $this->_name = $this->getName();
  72.             }
  73.         }
  74.  
  75.         //set the model state
  76.         if (array_key_exists('state'$config))  {
  77.             $this->_state = $config['state'];
  78.         else {
  79.             $this->_state = new JObject();
  80.         }
  81.  
  82.         //set the model dbo
  83.         if (array_key_exists('dbo'$config))  {
  84.             $this->_db = $config['dbo'];
  85.         else {
  86.             $this->_db = &JFactory::getDBO();
  87.         }
  88.  
  89.         // set the default view search path
  90.         if (array_key_exists('table_path'$config)) {
  91.             $this->addTablePath($config['table_path']);
  92.         else if (defined'JPATH_COMPONENT_ADMINISTRATOR' )){
  93.             $this->addTablePath(JPATH_COMPONENT_ADMINISTRATOR.DS.'tables');
  94.         }
  95.     }
  96.  
  97.     /**
  98.      * Returns a reference to the a Model object, always creating it
  99.      *
  100.      * @param    string    The model type to instantiate
  101.      * @param    string    Prefix for the model class name. Optional.
  102.      * @param    array    Configuration array for model. Optional.
  103.      * @return    mixed    A model object, or false on failure
  104.      * @since    1.5
  105.     */
  106.     function &getInstance$type$prefix ''$config array() )
  107.     {
  108.         $type        preg_replace('/[^A-Z0-9_\.-]/i'''$type);
  109.         $modelClass    $prefix.ucfirst($type);
  110.         $result        false;
  111.  
  112.         if (!class_exists$modelClass ))
  113.         {
  114.             jimport('joomla.filesystem.path');
  115.             $path JPath::find(
  116.                 JModel::addIncludePath(),
  117.                 JModel::_createFileName'model'array'name' => $type))
  118.             );
  119.             if ($path)
  120.             {
  121.                 require_once $path;
  122.  
  123.                 if (!class_exists$modelClass ))
  124.                 {
  125.                     JError::raiseWarning0'Model class ' $modelClass ' not found in file.' );
  126.                     return $result;
  127.                 }
  128.             }
  129.             else return $result;
  130.         }
  131.  
  132.         $result new $modelClass($config);
  133.         return $result;
  134.     }
  135.  
  136.     /**
  137.      * Method to set model state variables
  138.      *
  139.      * @access    public
  140.      * @param    string    The name of the property
  141.      * @param    mixed    The value of the property to set
  142.      * @return    mixed    The previous value of the property
  143.      * @since    1.5
  144.      */
  145.     function setState$property$value=null )
  146.     {
  147.         return $this->_state->set($property$value);
  148.     }
  149.  
  150.     /**
  151.      * Method to get model state variables
  152.      *
  153.      * @access    public
  154.      * @param    string    Optional parameter name
  155.      * @return    object    The property where specified, the state object where omitted
  156.      * @since    1.5
  157.      */
  158.     function getState($property null)
  159.     {
  160.         return $property === null $this->_state : $this->_state->get($property);
  161.     }
  162.  
  163.     /**
  164.      * Method to get the database connector object
  165.      *
  166.      * @access    public
  167.      * @return    object JDatabase connector object
  168.      * @since    1.5
  169.      */
  170.     function &getDBO()
  171.     {
  172.         return $this->_db;
  173.     }
  174.  
  175.     /**
  176.      * Method to set the database connector object
  177.      *
  178.      * @param    object    $db    A JDatabase based object
  179.      * @return    void 
  180.      * @since    1.5
  181.      */
  182.     function setDBO(&$db)
  183.     {
  184.         $this->_db =$db;
  185.     }
  186.  
  187.     /**
  188.      * Method to get the model name
  189.      *
  190.      * The model name by default parsed using the classname, or it can be set
  191.      * by passing a $config['name�] in the class constructor
  192.      *
  193.      * @access    public
  194.      * @return    string The name of the model
  195.      * @since    1.5
  196.      */
  197.     function getName()
  198.     {
  199.         $name $this->_name;
  200.  
  201.         if (empty$name ))
  202.         {
  203.             $r null;
  204.             if (!preg_match('/Model(.*)/i'get_class($this)$r)) {
  205.                 JError::raiseError (500"JModel::getName() : Can't get or parse class name.");
  206.             }
  207.             $name strtolower$r[1);
  208.         }
  209.  
  210.         return $name;
  211.     }
  212.  
  213.     /**
  214.      * Method to get a table object, load it if necessary.
  215.      *
  216.      * @access    public
  217.      * @param    string The table name. Optional.
  218.      * @param    string The class prefix. Optional.
  219.      * @param    array    Configuration array for model. Optional.
  220.      * @return    object    The table
  221.      * @since    1.5
  222.      */
  223.     function &getTable($name=''$prefix='Table'$options array())
  224.     {
  225.         if (empty($name)) {
  226.             $name $this->getName();
  227.         }
  228.  
  229.         if($table &$this->_createTable$name$prefix$options ))  {
  230.             return $table;
  231.         }
  232.  
  233.         JError::raiseError0'Table ' $name ' not supported. File not found.' );
  234.         $null null;
  235.         return $null;
  236.     }
  237.  
  238.     /**
  239.      * Add a directory where JModel should search for models. You may
  240.      * either pass a string or an array of directories.
  241.      *
  242.      * @access    public
  243.      * @param    string    A path to search.
  244.      * @return    array    An array with directory elements
  245.      * @since    1.5
  246.      */
  247.     function addIncludePath$path='' )
  248.     {
  249.         static $paths;
  250.  
  251.         if (!isset($paths)) {
  252.             $paths array();
  253.         }
  254.         if (!empty$path && !in_array$path$paths )) {
  255.             jimport('joomla.filesystem.path');
  256.             array_unshift($pathsJPath::clean$path ));
  257.         }
  258.         return $paths;
  259.     }
  260.  
  261.     /**
  262.      * Adds to the stack of model table paths in LIFO order.
  263.      *
  264.      * @static
  265.      * @param    string|arrayThe directory (-ies) to add.
  266.      * @return    void 
  267.      */
  268.     function addTablePath($path)
  269.     {
  270.         jimport('joomla.database.table');
  271.         JTable::addIncludePath($path);
  272.     }
  273.  
  274.     /**
  275.      * Returns an object list
  276.      *
  277.      * @param    string The query
  278.      * @param    int Offset
  279.      * @param    int The number of records
  280.      * @return    array 
  281.      * @access    protected
  282.      * @since    1.5
  283.      */
  284.     function &_getList$query$limitstart=0$limit=)
  285.     {
  286.         $this->_db->setQuery$query$limitstart$limit );
  287.         $result $this->_db->loadObjectList();
  288.  
  289.         return $result;
  290.     }
  291.  
  292.     /**
  293.      * Returns a record count for the query
  294.      *
  295.      * @param    string The query
  296.      * @return    int 
  297.      * @access    protected
  298.      * @since    1.5
  299.      */
  300.     function _getListCount$query )
  301.     {
  302.         $this->_db->setQuery$query );
  303.         $this->_db->query();
  304.  
  305.         return $this->_db->getNumRows();
  306.     }
  307.  
  308.     /**
  309.      * Method to load and return a model object.
  310.      *
  311.      * @access    private
  312.      * @param    string    The name of the view
  313.      * @param   string  The class prefix. Optional.
  314.      * @return    mixed    Model object or boolean false if failed
  315.      * @since    1.5
  316.      */
  317.     function &_createTable$name$prefix 'Table'$config array())
  318.     {
  319.         $result null;
  320.  
  321.         // Clean the model name
  322.         $name    preg_replace'/[^A-Z0-9_]/i'''$name );
  323.         $prefix preg_replace'/[^A-Z0-9_]/i'''$prefix );
  324.  
  325.         //Make sure we are returning a DBO object
  326.         if (!array_key_exists('dbo'$config))  {
  327.             $config['dbo'=$this->getDBO();;
  328.         }
  329.  
  330.         $instance =JTable::getInstance($name$prefix$config );
  331.         return $instance;
  332.     }
  333.  
  334.     /**
  335.      * Create the filename for a resource
  336.      *
  337.      * @access    private
  338.      * @param    string     $type  The resource type to create the filename for
  339.      * @param    array     $parts An associative array of filename information
  340.      * @return    string The filename
  341.      * @since    1.5
  342.      */
  343.     function _createFileName($type$parts array())
  344.     {
  345.         $filename '';
  346.  
  347.         switch($type)
  348.         {
  349.             case 'model':
  350.                 $filename strtolower($parts['name']).'.php';
  351.                 break;
  352.  
  353.         }
  354.         return $filename;
  355.     }
  356. }

Documentation generated on Tue, 29 Jan 2008 18:48:27 +0000 by phpDocumentor 1.3.1