_init() protected method

protected _init ( )
Exemplo n.º 1
0
 protected function _init()
 {
     $config = $this->_config;
     unset($config['type']);
     $this->connection = $this->_instance('service', $config);
     parent::_init();
 }
Exemplo n.º 2
0
 protected function _init()
 {
     parent::_init();
     $this->_operators += array('like' => function ($key, $value) {
         return new MongoRegex($value);
     });
 }
Exemplo n.º 3
0
 /**
  * Initializer. Adds operator handlers which will
  * later allow to correctly cast any values.
  *
  * @see lithium\data\source\MongoDb::$_operators
  * @see lithium\data\source\MongoDb::_operators()
  * @return void
  */
 protected function _init()
 {
     parent::_init();
     $this->_operators += array('like' => function ($key, $value) {
         return new MongoRegex($value);
     }, '$exists' => function ($key, $value) {
         return array('$exists' => (bool) $value);
     }, '$type' => function ($key, $value) {
         return array('$type' => (int) $value);
     }, '$mod' => function ($key, $value) {
         $value = (array) $value;
         return array('$mod' => array(current($value), next($value) ?: 0));
     }, '$size' => function ($key, $value) {
         return array('$size' => (int) $value);
     }, '$elemMatch' => function ($operator, $values, $options = array()) {
         $options += array('castOpts' => array(), 'field' => '');
         $options['castOpts'] += array('pathKey' => $options['field']);
         $values = (array) $values;
         if (empty($options['castOpts']['schema'])) {
             return array('$elemMatch' => $values);
         }
         foreach ($values as $key => &$value) {
             $value = $options['castOpts']['schema']->cast(null, $key, $value, $options['castOpts']);
         }
         return array('$elemMatch' => $values);
     });
 }
Exemplo n.º 4
0
 /**
  * Initializer. Initializes properties like `Database::$_strategies` because
  * closures cannot be created within the class definition.
  *
  * @see lithium\data\source\Database::$_columns
  * @see lithium\data\source\Database::$_strings
  * @see lithium\data\source\Database::$_strategies
  */
 protected function _init()
 {
     parent::_init();
     $formatters = $this->_formatters();
     foreach ($this->_columns as $type => $column) {
         if (isset($formatters[$type])) {
             $this->_columns[$type]['formatter'] = $formatters[$type];
         }
     }
     $this->_strings += array('read' => 'SELECT {:fields} FROM {:source} {:alias} {:joins} {:conditions} {:group} ' . '{:having} {:order} {:limit};{:comment}');
     $this->_strategies += array('joined' => function ($self, $model, $context) {
         $with = $context->with();
         $strategy = function ($me, $model, $tree, $path, $from, &$deps) use($self, $context, $with) {
             foreach ($tree as $name => $childs) {
                 if (!($rel = $model::relations($name))) {
                     throw new QueryException("Model relationship `{$name}` not found.");
                 }
                 $constraints = array();
                 $alias = $name;
                 $relPath = $path ? $path . '.' . $name : $name;
                 if (isset($with[$relPath])) {
                     list($unallowed, $allowed) = Set::slice($with[$relPath], array('alias', 'constraints'));
                     if ($unallowed) {
                         $message = "Only `'alias'` and `'constraints'` are allowed in ";
                         $message .= "`'with'` using the `'joined'` strategy.";
                         throw new QueryException($message);
                     }
                     extract($with[$relPath]);
                 }
                 $to = $context->alias($alias, $relPath);
                 $deps[$to] = $deps[$from];
                 $deps[$to][] = $from;
                 if ($context->relationships($relPath) === null) {
                     $context->relationships($relPath, array('type' => $rel->type(), 'model' => $rel->to(), 'fieldName' => $rel->fieldName(), 'alias' => $to));
                     $self->join($context, $rel, $from, $to, $constraints);
                 }
                 if (!empty($childs)) {
                     $me($me, $rel->to(), $childs, $relPath, $to, $deps);
                 }
             }
         };
         $tree = Set::expand(array_fill_keys(array_keys($with), false));
         $alias = $context->alias();
         $deps = array($alias => array());
         $strategy($strategy, $model, $tree, '', $alias, $deps);
         $models = $context->models();
         foreach ($context->fields() as $field) {
             if (!is_string($field)) {
                 continue;
             }
             list($alias, $field) = $self->invokeMethod('_splitFieldname', array($field));
             $alias = $alias ?: $field;
             if ($alias && isset($models[$alias])) {
                 foreach ($deps[$alias] as $depAlias) {
                     $depModel = $models[$depAlias];
                     $context->fields(array($depAlias => (array) $depModel::meta('key')));
                 }
             }
         }
     }, 'nested' => function ($self, $model, $context) {
         throw new QueryException("This strategy is not yet implemented.");
     });
 }
Exemplo n.º 5
0
	protected function _init() {
		parent::_init();

		$this->_operators += array(
			'like' => function($key, $value) { return new MongoRegex($value); }
		);

		$this->_handlers += array(
			'id' => function($v) {
				return is_string($v) && preg_match('/^[0-9a-f]{24}$/', $v) ? new MongoId($v) : $v;
			},
			'date' => function($v) {
				$v = is_numeric($v) ? intval($v) : strtotime($v);
				return (!$v || time() == $v) ? new MongoDate() : new MongoDate($v);
			},
			'regex'   => function($v) { return new MongoRegex($v); },
			'integer' => function($v) { return (integer) $v; },
			'float'   => function($v) { return (float) $v; },
			'boolean' => function($v) { return (boolean) $v; },
			'code'    => function($v) { return new MongoCode($v); },
			'binary'  => function($v) { return new MongoBinData($v); }
		);
	}
Exemplo n.º 6
0
 protected function _init()
 {
     parent::_init();
     $this->_operators += array('like' => function ($key, $value) {
         return new MongoRegex($value);
     });
     $this->_handlers += array('id' => function ($v) {
         return is_string($v) && preg_match('/^[0-9a-f]{24}$/', $v) ? new MongoId($v) : $v;
     }, 'date' => function ($v) {
         return new MongoDate(is_numeric($v) ? intval($v) : strtotime($v));
     }, 'regex' => function ($v) {
         return new MongoRegex($v);
     }, 'integer' => function ($v) {
         return (int) $v;
     }, 'float' => function ($v) {
         return (double) $v;
     }, 'boolean' => function ($v) {
         return (bool) $v;
     });
 }
Exemplo n.º 7
0
 protected function _init()
 {
     $this->_connection = new $this->_classes['service']($this->_config);
     parent::_init();
 }