1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
<?php
/*
* This file is part of the ICanBoogie package.
*
* (c) Olivier Laviale <olivier.laviale@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ICanBoogie\ActiveRecord;
use ICanBoogie\Accessor\AccessorTrait;
use ICanBoogie\OffsetNotWritable;
/**
* Relation collection of a model.
*
* @property-read Model $model The parent model.
*/
class RelationCollection implements \ArrayAccess
{
use AccessorTrait;
/**
* Parent model.
*
* @var Model
*/
protected $model;
protected function get_model()
{
return $this->model;
}
/**
* Relations.
*
* @var Relation[]
*/
protected $relations;
/**
* Initialize the {@link $model} property.
*
* @param Model $model The parent model.
*/
public function __construct(Model $model)
{
$this->model = $model;
}
public function offsetExists($relation_name)
{
return isset($this->relations[$relation_name]);
}
public function offsetGet($relation_name)
{
if (!$this->offsetExists($relation_name))
{
throw new RelationNotDefined($relation_name, $this);
}
return $this->relations[$relation_name];
}
public function offsetSet($relation_name, $relation)
{
throw new OffsetNotWritable([ $relation_name, $this ]);
}
public function offsetUnset($relation_name)
{
throw new OffsetNotWritable([ $relation_name, $this ]);
}
/**
* Add a _belongs to_ relation.
*
* <pre>
* $cars->belongs_to([ $drivers, $brands ]);
* # or
* $cars->belongs_to([ 'drivers', 'brands' ]);
* # or
* $cars->belongs_to($drivers, $brands);
* # or
* $cars->belongs_to($drivers)->belongs_to($brands);
* # or
* $cars->belongs_to([
*
* [ $drivers, [ 'local_key' => 'card_id', 'foreign_key' => 'driver_id' ] ]
* [ $brands, [ 'local_key' => 'brand_id', 'foreign_key' => 'brand_id' ] ]
*
* ]);
* </pre>
*
* @param string|array $belongs_to
*
* @return Model
*/
public function belongs_to($belongs_to)
{
if (func_num_args() > 1)
{
$belongs_to = func_get_args();
}
foreach ((array) $belongs_to as $definition)
{
if (!is_array($definition))
{
$definition = [ $definition ];
}
list($related, $options) = ((array) $definition) + [ 1 => [] ];
$relation = new BelongsToRelation($this->model, $related, $options);
$this->relations[$relation->as] = $relation;
}
return $this->model;
}
/**
* Add a one-to-many relation.
*
* <pre>
* $this->has_many('comments');
* $this->has_many([ 'comments', 'attachments' ]);
* $this->has_many([ [ 'comments', [ 'as' => 'comments' ] ], 'attachments' ]);
* </pre>
*
* @param Model|string $related The related model can be specified using its instance or its
* identifier.
* @param array $options the following options are available:
*
* - `local_key`: The name of the local key. Default: The parent model's primary key.
* - `foreign_key`: The name of the foreign key. Default: The parent model's primary key.
* - `as`: The name of the magic property to add to the prototype. Default: a plural name
* resolved from the foreign model's id.
*
* @return Model
*
* @see HasManyRelation
*/
public function has_many($related, array $options = [])
{
if (is_array($related))
{
$relation_list = $related;
foreach ($relation_list as $definition)
{
list($related, $options) = ((array) $definition) + [ 1 => [] ];
$relation = new HasManyRelation($this->model, $related, $options);
$this->relations[$relation->as] = $relation;
}
return null;
}
$relation = new HasManyRelation($this->model, $related, $options);
$this->relations[$relation->as] = $relation;
return $this->model;
}
}