lithium\data\model\Relationship::__construct()
Overrides
lithium\core\Object::__construct()
Constructor. Constructs an object that represents a relationship between two model classes.
Parameters
-
array
$config
The relationship's configuration, which defines how the two models in question are bound. The available options are:
'name'
string: The name of the relationship in the context of the originating model. For example, aPosts
model might define a relationship to aUsers
model like so:public $hasMany = ['Author' => ['to' => 'Users']];
In this case, the relationship is bound to theUsers
model, but'Author'
would be the relationship name. This is the name with which the relationship is referenced in the originating model.'key'
mixed: An array of fields that define the relationship, where the keys are fields in the originating model, and the values are fields in the target model. If the relationship is not defined by keys, this array should be empty.'type'
string: The type of relationship. Should be one of'belongsTo'
,'hasOne'
or'hasMany'
.'from'
string: The fully namespaced class name of the model where this relationship originates.'to'
string: The fully namespaced class name of the model that this relationship targets.'link'
string: A constant specifying how the object bound to the originating model is linked to the object bound to the target model. For relational databases, the only valid value isLINK_KEY
, which means a foreign key in one object matches another key (usually the primary key) in the other. For document-oriented and other non-relational databases, different types of linking, including key lists, database reference objects (such as MongoDB'sMongoDBRef
), or even embedding.'fields'
mixed: An array of the subset of fields that should be selected from the related object(s) by default. If set totrue
(the default), all fields are selected.'fieldName'
string: The name of the field used when accessing the related data in a result set. For example, in the case ofPosts hasMany Comments
, the field name defaults to'comments'
, so comment data is accessed (assuming$post = Posts::first()
) as$post->comments
.'constraints'
mixed: A string or array containing additional constraints on the relationship query. If a string, can contain a literal SQL fragment or other database-native value. If an array, maps fields from the related object either to fields elsewhere, or to arbitrary expressions. In either case, the values specified here will be literally interpreted by the database.'strategy'
\Closure: An anonymous function used by an instantiating class, such as a database object, to provide additional, dynamic configuration, after theRelationship
instance has finished configuring itself.
Returns
voidSource
public function __construct(array $config = []) {
$defaults = [
'name' => null,
'key' => [],
'type' => null,
'to' => null,
'from' => null,
'link' => static::LINK_KEY,
'fields' => true,
'fieldName' => null,
'constraints' => [],
'strategy' => null
];
$config += $defaults;
if (!$config['type'] || !$config['fieldName']) {
throw new ConfigException("`'type'`, `'fieldName'` and `'from'` options can't be empty.");
}
if (!$config['to'] && !$config['name']) {
throw new ConfigException("`'to'` and `'name'` options can't both be empty.");
}
parent::__construct($config);
}