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

relationship() public method

Defines or modifies the default settings of a relationship between two models.
public relationship ( string $class, string $type, string $name, array $config = [] ) : array
$class string the primary model of the relationship
$type string the type of the relationship (hasMany, hasOne, belongsTo)
$name string the name of the relationship
$config array relationship options
return array Returns an array containing the configuration for a model relationship.
    public function relationship($class, $type, $name, array $config = array())
    {
        $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 = array($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);
    }