lithium\data\Model::_initialize()

protected static method

Init default connection options and connects default finders.

This method will set the Model::$_schema, Model::$_meta, Model::$_finders class attributes, as well as obtain a handle to the configured persistent storage connection

Parameters

  • string $class

    The fully-namespaced class name to initialize.

Returns

object

Returns the initialized model instance.

Source

	protected static function _initialize($class) {
		$self = static::$_instances[$class];

		if (isset(static::$_initialized[$class]) && static::$_initialized[$class]) {
			return $self;
		}
		static::$_initialized[$class] = true;

		$properties = [
			'validates',
			'belongsTo',
			'hasMany',
			'hasOne',
			'_meta',
			'_finders',
			'_query',
			'_classes',
			'_initializers'
		];
		if (is_array($self->_schema)) {
			$properties[] = '_schema';
		}
		if ($self->_inherits) {
			$properties = array_merge($self->_inherits, $properties);
		}
		$self->_inherit($properties);

		$source = [
			'classes' => [], 'meta' => [], 'finders' => [], 'schema' => []
		];

		$meta = $self->_meta;
		if ($meta['connection']) {
			$classes = $self->_classes;
			$conn = $classes['connections']::get($meta['connection']);
			$source = (($conn) ? $conn->configureClass($class) : []) + $source;
		}

		$self->_classes += $source['classes'];
		$self->_meta = compact('class') + $self->_meta + $source['meta'];

		$self->_initializers += [
			'name' => function($self) {
				return basename(str_replace('\\', '/', $self));
			},
			'source' => function($self) {
				return Inflector::tableize($self::meta('name'));
			},
			'title' => function($self) {
				$titleKeys = ['title', 'name'];
				$titleKeys = array_merge($titleKeys, (array) $self::meta('key'));
				return $self::hasField($titleKeys);
			}
		];

		if (is_object($self->_schema)) {
			$self->_schema->append($source['schema']);
		} else {
			$self->_schema += $source['schema'];
		}

		$self->_finders += $source['finders'] + static::_finders();

		$self->_classes += [
			'query'       => 'lithium\data\model\Query',
			'validator'   => 'lithium\util\Validator',
			'entity'      => 'lithium\data\Entity'
		];

		static::_relationsToLoad();
		return $self;
	}