lithium\data\source\Database::_init PHP Method

_init() protected method

Initializer. Initializes properties like Database::$_strategies because closures cannot be created within the class definition.
See also: lithium\data\source\Database::$_columns
See also: lithium\data\source\Database::$_strings
See also: lithium\data\source\Database::$_strategies
protected _init ( )
    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.");
        });
    }

Usage Example

Example #1
0
 /**
  * Initializer. Adds MySQL-specific operators to `$_operators`.
  *
  * @see lithium\data\source\database\adapter\MySql::$_operators
  * @see lithium\data\source\Database::$_operators
  */
 protected function _init()
 {
     parent::_init();
     $this->_operators += array('REGEXP' => array(), 'NOT REGEXP' => array(), 'SOUNDS LIKE' => array());
 }