/**
  * test hasMethod returning a 'callback'
  *
  * @return void
  */
 public function testHasMethodAsCallback()
 {
     new Sample();
     $Collection = new BehaviorCollection();
     $Collection->init('Sample', array('Test', 'Test2'));
     $result = $Collection->hasMethod('testMethod', true);
     $expected = array('Test', 'testMethod');
     $this->assertEquals($expected, $result);
     $result = $Collection->hasMethod('resolveMethod', true);
     $expected = array('Test2', 'resolveMethod');
     $this->assertEquals($expected, $result);
     $result = $Collection->hasMethod('mappingRobotOnTheRoof', true);
     $expected = array('Test2', 'mapped', 'mappingRobotOnTheRoof');
     $this->assertEquals($expected, $result);
 }
Example #2
0
 /**
  * Constructor. Binds the model's database table to the object.
  *
  * If `$id` is an array it can be used to pass several options into the model.
  *
  * - id - The id to start the model on.
  * - table - The table to use for this model.
  * - ds - The connection name this model is connected to.
  * - name - The name of the model eg. Post.
  * - alias - The alias of the model, this is used for registering the instance in the `ClassRegistry`.
  *   eg. `ParentThread`
  *
  * ### Overriding Model's __construct method.
  *
  * When overriding Model::__construct() be careful to include and pass in all 3 of the
  * arguments to `parent::__construct($id, $table, $ds);`
  *
  * ### Dynamically creating models
  *
  * You can dynamically create model instances using the $id array syntax.
  *
  * {{{
  * $Post = new Model(array('table' => 'posts', 'name' => 'Post', 'ds' => 'connection2'));
  * }}}
  *
  * Would create a model attached to the posts table on connection2.  Dynamic model creation is useful
  * when you want a model object that contains no associations or attached behaviors.
  *
  * @param mixed $id Set this ID for this model on startup, can also be an array of options, see above.
  * @param string $table Name of database table to use.
  * @param string $ds DataSource connection name.
  */
 public 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 ($ds !== null) {
         $this->useDbConfig = $ds;
     }
     if (is_subclass_of($this, 'AppModel')) {
         $merge = array('actsAs', 'findMethods');
         $parentClass = get_parent_class($this);
         if ($parentClass !== 'AppModel') {
             $this->_mergeVars($merge, $parentClass);
         }
         $this->_mergeVars($merge, 'AppModel');
     }
     $this->Behaviors = new BehaviorCollection();
     if ($this->useTable !== false) {
         if ($this->useTable === null) {
             $this->useTable = Inflector::tableize($this->name);
         }
         if ($this->displayField == null) {
             unset($this->displayField);
         }
         $this->table = $this->useTable;
         $this->tableToModel[$this->table] = $this->alias;
     } elseif ($this->table === false) {
         $this->table = Inflector::tableize($this->name);
     }
     if ($this->tablePrefix === null) {
         unset($this->tablePrefix);
     }
     $this->_createLinks();
     $this->Behaviors->init($this->alias, $this->actsAs);
 }
Example #3
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' => $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 ($ds !== null) {
         $this->useDbConfig = $ds;
     }
     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);
 }