예제 #1
0
 /**
  * Initializes a new instance
  *
  * The $config array understands the following keys:
  *
  * - table: Name of the database table to represent
  * - alias: Alias to be assigned to this table (default to table name)
  * - connection: The connection instance to use
  * - entityClass: The fully namespaced class name of the entity class that will
  *   represent rows in this table.
  * - schema: A \Cake\Database\Schema\Table object or an array that can be
  *   passed to it.
  * - eventManager: An instance of an event manager to use for internal events
  * - behaviors: A BehaviorRegistry. Generally not used outside of tests.
  * - associations: An AssociationCollection instance.
  * - validator: A Validator instance which is assigned as the "default"
  *   validation set, or an associative array, where key is the name of the
  *   validation set and value the Validator instance.
  *
  * @param array $config List of options for this table
  */
 public function __construct(array $config = [])
 {
     if (!empty($config['registryAlias'])) {
         $this->registryAlias($config['registryAlias']);
     }
     if (!empty($config['table'])) {
         $this->table($config['table']);
     }
     if (!empty($config['alias'])) {
         $this->alias($config['alias']);
     }
     if (!empty($config['connection'])) {
         $this->connection($config['connection']);
     }
     if (!empty($config['schema'])) {
         $this->schema($config['schema']);
     }
     if (!empty($config['entityClass'])) {
         $this->entityClass($config['entityClass']);
     }
     $eventManager = $behaviors = $associations = null;
     if (!empty($config['eventManager'])) {
         $eventManager = $config['eventManager'];
     }
     if (!empty($config['behaviors'])) {
         $behaviors = $config['behaviors'];
     }
     if (!empty($config['associations'])) {
         $associations = $config['associations'];
     }
     if (!empty($config['validator'])) {
         if (!is_array($config['validator'])) {
             $this->validator(static::DEFAULT_VALIDATOR, $config['validator']);
         } else {
             foreach ($config['validator'] as $name => $validator) {
                 $this->validator($name, $validator);
             }
         }
     }
     $this->_eventManager = $eventManager ?: new EventManager();
     $this->_behaviors = $behaviors ?: new BehaviorRegistry();
     $this->_behaviors->setTable($this);
     $this->_associations = $associations ?: new AssociationCollection();
     $this->initialize($config);
     $this->_eventManager->on($this);
     $this->dispatchEvent('Model.initialize');
 }