Exemple #1
0
 /**
  * Constructor. Binds the Model's database table to the object.
  *
  * @param integer $id Set this ID for this model on startup
  * @param string $table Name of database table to use.
  * @param object $ds DataSource connection object.
  */
 function __construct($id = false, $table = null, $ds = null)
 {
     parent::__construct();
     if (is_array($id)) {
         extract(array_merge(array('id' => false, 'table' => null, 'ds' => null, 'name' => null, 'alias' => null), $id));
         $this->name = $name;
         $this->alias = $alias;
     }
     if ($this->name === null) {
         $this->name = get_class($this);
     }
     if ($this->alias === null) {
         $this->alias = $this->name;
     }
     if ($this->primaryKey === null) {
         $this->primaryKey = 'id';
     }
     ClassRegistry::addObject($this->alias, $this);
     $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) {
             $this->displayField = $this->hasField(array('title', 'name', $this->primaryKey));
         }
     }
     if (is_subclass_of($this, 'AppModel')) {
         $appVars = get_class_vars('AppModel');
         $merge = array();
         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} = Set::merge($appVars[$var], $this->{$var});
             }
         }
     }
     $this->Behaviors = new BehaviorCollection();
     $this->Behaviors->init($this->alias, $this->actsAs);
 }
 /**
  * Constructor. Binds the model's database table to the object.
  *
  * @param integer $id Set this ID for this model on startup
  * @param string $table Name of database table to use.
  * @param object $ds DataSource connection object.
  */
 function __construct($id = false, $table = null, $ds = null)
 {
     parent::__construct();
     if (is_array($id)) {
         extract(array_merge(array('id' => $this->id, 'table' => $this->useTable, 'ds' => $this->useDbConfig, 'name' => $this->name, 'alias' => $this->alias), $id));
     }
     if ($this->name === null) {
         $this->name = isset($name) ? $name : get_class($this);
     }
     if ($this->alias === null) {
         $this->alias = isset($alias) ? $alias : $this->name;
     }
     if ($this->primaryKey === null) {
         $this->primaryKey = 'id';
     }
     ClassRegistry::addObject($this->alias, $this);
     $this->id = $id;
     unset($id);
     if ($table === false) {
         $this->useTable = false;
     } elseif ($table) {
         $this->useTable = $table;
     }
     if (is_subclass_of($this, 'AppModel')) {
         $appVars = get_class_vars('AppModel');
         $merge = array('_findMethods');
         if ($this->actsAs !== null || $this->actsAs !== false) {
             $merge[] = 'actsAs';
         }
         $parentClass = get_parent_class($this);
         if (strtolower($parentClass) !== 'appmodel') {
             $parentVars = get_class_vars($parentClass);
             foreach ($merge as $var) {
                 if (isset($parentVars[$var]) && !empty($parentVars[$var])) {
                     $appVars[$var] = Set::merge($appVars[$var], $parentVars[$var]);
                 }
             }
         }
         foreach ($merge as $var) {
             if (isset($appVars[$var]) && !empty($appVars[$var]) && is_array($this->{$var})) {
                 $this->{$var} = Set::merge($appVars[$var], $this->{$var});
             }
         }
     }
     $this->Behaviors = new BehaviorCollection();
     if ($this->useTable !== false) {
         $this->setDataSource($ds);
         if ($this->useTable === null) {
             $this->useTable = Inflector::tableize($this->name);
         }
         if (method_exists($this, 'setTablePrefix')) {
             $this->setTablePrefix();
         }
         $this->setSource($this->useTable);
         if ($this->displayField == null) {
             $this->displayField = $this->hasField(array('title', 'name', $this->primaryKey));
         }
     } elseif ($this->table === false) {
         $this->table = Inflector::tableize($this->name);
     }
     $this->__createLinks();
     $this->Behaviors->init($this->alias, $this->actsAs);
 }
 /**
  * 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);
                         }
                     }
                 }
             }
         }
     }
 }