lithium\data\source\Database::relationship()

public method

Defines or modifies the default settings of a relationship between two models.

Parameters

  • string $class

    the primary model of the relationship

  • string $type

    the type of the relationship (hasMany, hasOne, belongsTo)

  • string $name

    the name of the relationship

  • array $config

    relationship options

Returns

array

Returns an array containing the configuration for a model relationship.

Source

	public function relationship($class, $type, $name, array $config = []) {
		$primary = $class::meta('key');

		if (is_array($primary)) {
			$key = array_combine($primary, $primary);
		} elseif ($type === 'hasMany' || $type === 'hasOne') {
			$secondary = Inflector::underscore(Inflector::singularize($class::meta('name')));
			$key = [$primary => "{$secondary}_id"];
		} else {
			$key = Inflector::underscore(Inflector::singularize($name)) . '_id';
		}

		$from = $class;
		$fieldName = $this->relationFieldName($type, $name);
		$config += compact('type', 'name', 'key', 'from', 'fieldName');
		return $this->_instance('relationship', $config);
	}