Example #1
0
 /**
  * Load dependencies for given element (controller/component/helper)
  *
  * @param string $element Element to load dependency for
  * @access private
  */
 function __loadDependencies($element)
 {
     switch (low($element)) {
         case 'behavior':
             loadModel(null);
             loadBehavior(null);
             break;
         case 'controller':
             loadController(null);
             break;
         case 'component':
             loadComponent(null);
             break;
         case 'helper':
             uses('view' . DS . 'helper');
             loadHelper(null);
             break;
         case 'model':
             loadModel(null);
             break;
     }
 }
Example #2
0
 /**
  * Open the persistent class file for reading
  * Used by Object::_persist()
  *
  * @param string $name Name of persisted class
  * @param string $type Type of persistance (e.g: registry)
  * @access private
  */
 function __openPersistent($name, $type = null)
 {
     $file = CACHE . 'persistent' . DS . strtolower($name) . '.php';
     include $file;
     switch ($type) {
         case 'registry':
             $vars = unserialize(${$name});
             foreach ($vars['0'] as $key => $value) {
                 if (strpos($key, '_behavior')) {
                     loadBehavior(Inflector::classify(str_replace('_behavior', '', $key)));
                 } else {
                     loadModel(Inflector::classify($key));
                 }
             }
             unset($vars);
             $vars = unserialize(${$name});
             foreach ($vars['0'] as $key => $value) {
                 ClassRegistry::addObject($key, $value);
                 unset($value);
             }
             unset($vars);
             break;
         default:
             $vars = unserialize(${$name});
             $this->{$name} = $vars['0'];
             unset($vars);
             break;
     }
 }
Example #3
0
 /**
  * Constructor. Binds the Model's database table to the object.
  *
  * @param integer $id
  * @param string $table Name of database table to use.
  * @param DataSource $ds DataSource connection object.
  */
 function __construct($id = false, $table = null, $ds = null)
 {
     parent::__construct();
     if (is_array($id) && isset($id['name'])) {
         $options = am(array('id' => false, 'table' => null, 'ds' => null, 'alias' => null), $id);
         list($id, $table, $ds) = array($options['id'], $options['table'], $options['ds']);
         $this->name = $options['name'];
     }
     if ($this->name === null) {
         $this->name = get_class($this);
     }
     if ($this->primaryKey === null) {
         $this->primaryKey = 'id';
     }
     if (isset($options['alias']) || !empty($options['alias'])) {
         $this->currentModel = Inflector::underscore($options['alias']);
         unset($options);
     } else {
         $this->currentModel = Inflector::underscore($this->name);
     }
     ClassRegistry::addObject($this->currentModel, $this);
     ClassRegistry::map($this->currentModel, $this->currentModel);
     $this->id = $id;
     unset($id);
     if ($table === false) {
         $this->useTable = false;
     } elseif ($table) {
         $this->useTable = $table;
     }
     if ($this->useTable !== false) {
         $this->setDataSource($ds);
         if ($this->useTable === null) {
             $this->useTable = Inflector::tableize($this->name);
         }
         if (in_array('settableprefix', get_class_methods($this))) {
             $this->setTablePrefix();
         }
         $this->setSource($this->useTable);
         $this->__createLinks();
         if ($this->displayField == null) {
             if ($this->hasField('title')) {
                 $this->displayField = 'title';
             }
             if ($this->hasField('name')) {
                 $this->displayField = 'name';
             }
             if ($this->displayField == null) {
                 $this->displayField = $this->primaryKey;
             }
         }
     }
     if (is_subclass_of($this, 'AppModel')) {
         $appVars = get_class_vars('AppModel');
         $actsAs = $appVars['actsAs'];
         $merge = array('actsAs');
         if ($this->actsAs !== null || $this->actsAs !== false) {
             $merge[] = 'actsAs';
         }
         foreach ($merge as $var) {
             if (isset($appVars[$var]) && !empty($appVars[$var]) && is_array($this->{$var})) {
                 $this->{$var} = array_merge($this->{$var}, array_diff($appVars[$var], $this->{$var}));
             }
         }
     }
     if ($this->actsAs !== null && empty($this->behaviors)) {
         $callbacks = array('setup', 'beforeFind', 'afterFind', 'beforeSave', 'afterSave', 'beforeDelete', 'afterDelete', 'afterError');
         $this->actsAs = Set::normalize($this->actsAs);
         foreach ($this->actsAs as $behavior => $config) {
             $className = $behavior . 'Behavior';
             if (!loadBehavior($behavior)) {
                 // Raise an error
             } else {
                 if (ClassRegistry::isKeySet($className)) {
                     if (PHP5) {
                         $this->behaviors[$behavior] = ClassRegistry::getObject($className);
                     } else {
                         $this->behaviors[$behavior] =& ClassRegistry::getObject($className);
                     }
                 } else {
                     if (PHP5) {
                         $this->behaviors[$behavior] = new $className();
                     } else {
                         $this->behaviors[$behavior] =& new $className();
                     }
                     ClassRegistry::addObject($className, $this->behaviors[$behavior]);
                 }
                 $this->behaviors[$behavior]->setup($this, $config);
                 $methods = $this->behaviors[$behavior]->mapMethods;
                 foreach ($methods as $method => $alias) {
                     if (!array_key_exists($method, $this->__behaviorMethods)) {
                         $this->__behaviorMethods[$method] = array($alias, $behavior);
                     }
                 }
                 $methods = get_class_methods($this->behaviors[$behavior]);
                 $parentMethods = get_class_methods('ModelBehavior');
                 foreach ($methods as $m) {
                     if (!in_array($m, $parentMethods)) {
                         if (strpos($m, '_') !== 0 && !array_key_exists($m, $this->__behaviorMethods) && !in_array($m, $callbacks)) {
                             $this->__behaviorMethods[$m] = array($m, $behavior);
                         }
                     }
                 }
             }
         }
     }
 }