| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\Orm\Relation; |
| 4 |
|
| 5 |
use Generator; |
| 6 |
use ipl\Orm\Model; |
| 7 |
use ipl\Orm\Relation; |
| 8 |
use ipl\Orm\Relations; |
| 9 |
use LogicException; |
| 10 |
|
| 11 |
/** |
| 12 |
* Many-to-many relationship |
| 13 |
*/ |
| 14 |
class BelongsToMany extends Relation |
| 15 |
{ |
| 16 |
/** @var string Relation class */ |
| 17 |
protected const RELATION_CLASS = HasMany::class; |
| 18 |
|
| 19 |
protected bool $isOne = false; |
| 20 |
|
| 21 |
/** @var ?string Name of the join table or junction model class */ |
| 22 |
protected ?string $throughClass = null; |
| 23 |
|
| 24 |
/** @var ?string Alias for the join table or junction model class */ |
| 25 |
protected ?string $throughAlias = null; |
| 26 |
|
| 27 |
/** @var ?Model The junction model */ |
| 28 |
protected ?Model $through = null; |
| 29 |
|
| 30 |
/** @var string|array|null Column name(s) of the target model's foreign key found in the join table */ |
| 31 |
protected string|array|null $targetForeignKey = null; |
| 32 |
|
| 33 |
/** @var string|array|null Candidate key column name(s) in the target table which references the target foreign key */ |
| 34 |
protected string|array|null $targetCandidateKey = null; |
| 35 |
|
| 36 |
/** |
| 37 |
* Get the name of the join table or junction model class |
| 38 |
* |
| 39 |
* @return ?string |
| 40 |
*/ |
| 41 |
public function getThroughClass(): ?string |
| 42 |
{ |
| 43 |
return $this->throughClass; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Set the join table name or junction model class |
| 48 |
* |
| 49 |
* @param string $through |
| 50 |
* |
| 51 |
* @return $this |
| 52 |
*/ |
| 53 |
public function through(string $through): static |
| 54 |
{ |
| 55 |
$this->throughClass = $through; |
| 56 |
|
| 57 |
return $this; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Get the alias for the join table or junction model class |
| 62 |
* |
| 63 |
* @return string |
| 64 |
*/ |
| 65 |
public function getThroughAlias(): string |
| 66 |
{ |
| 67 |
return $this->throughAlias ?? $this->getThrough()->getTableAlias(); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Set the alias for the join table or junction model class |
| 72 |
* |
| 73 |
* @param string $throughAlias |
| 74 |
* |
| 75 |
* @return $this |
| 76 |
*/ |
| 77 |
public function setThroughAlias(string $throughAlias): static |
| 78 |
{ |
| 79 |
$this->throughAlias = $throughAlias; |
| 80 |
|
| 81 |
return $this; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Get the junction model |
| 86 |
* |
| 87 |
* @return Model |
| 88 |
* |
| 89 |
* @throws LogicException If no through class is configured |
| 90 |
*/ |
| 91 |
public function getThrough(): Model |
| 92 |
{ |
| 93 |
if ($this->through === null) { |
| 94 |
$throughClass = $this->getThroughClass(); |
| 95 |
if ($throughClass === null) { |
| 96 |
throw new LogicException( |
| 97 |
'You cannot use a many-to-many relation without a through class or a table name for the' |
| 98 |
. ' junction model' |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
if (class_exists($throughClass)) { |
| 103 |
$this->through = new $throughClass(); |
| 104 |
} else { |
| 105 |
$this->through = (new Junction()) |
| 106 |
->setTableName($throughClass); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
return $this->through; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Set the junction model |
| 115 |
* |
| 116 |
* @param Model $through |
| 117 |
* |
| 118 |
* @return $this |
| 119 |
*/ |
| 120 |
public function setThrough(Model $through): static |
| 121 |
{ |
| 122 |
$this->through = $through; |
| 123 |
|
| 124 |
return $this; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Get the column name(s) of the target model's foreign key found in the join table |
| 129 |
* |
| 130 |
* @return string|array|null Array if the foreign key is compound, string otherwise |
| 131 |
*/ |
| 132 |
public function getTargetForeignKey(): string|array|null |
| 133 |
{ |
| 134 |
return $this->targetForeignKey; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Set the column name(s) of the target model's foreign key found in the join table |
| 139 |
* |
| 140 |
* @param string|array $targetForeignKey Array if the foreign key is compound, string otherwise |
| 141 |
* |
| 142 |
* @return $this |
| 143 |
*/ |
| 144 |
public function setTargetForeignKey(string|array $targetForeignKey): static |
| 145 |
{ |
| 146 |
$this->targetForeignKey = $targetForeignKey; |
| 147 |
|
| 148 |
return $this; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Get the candidate key column name(s) in the target table which references the target foreign key |
| 153 |
* |
| 154 |
* @return string|array|null Array if the foreign key is compound, string otherwise |
| 155 |
*/ |
| 156 |
public function getTargetCandidateKey(): string|array|null |
| 157 |
{ |
| 158 |
return $this->targetCandidateKey; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Set the candidate key column name(s) in the target table which references the target foreign key |
| 163 |
* |
| 164 |
* @param string|array $targetCandidateKey Array if the foreign key is compound, string otherwise |
| 165 |
* |
| 166 |
* @return $this |
| 167 |
*/ |
| 168 |
public function setTargetCandidateKey(string|array $targetCandidateKey): static |
| 169 |
{ |
| 170 |
$this->targetCandidateKey = $targetCandidateKey; |
| 171 |
|
| 172 |
return $this; |
| 173 |
} |
| 174 |
|
| 175 |
public function resolve(): Generator |
| 176 |
{ |
| 177 |
$source = $this->getSource(); |
| 178 |
|
| 179 |
$possibleCandidateKey = [$this->getCandidateKey()]; |
| 180 |
$possibleForeignKey = [$this->getForeignKey()]; |
| 181 |
|
| 182 |
$target = $this->getTarget(); |
| 183 |
|
| 184 |
$possibleTargetCandidateKey = [$this->getTargetForeignKey() ?: static::getDefaultForeignKey($target)]; |
| 185 |
$possibleTargetForeignKey = [$this->getTargetCandidateKey() ?: static::getDefaultCandidateKey($target)]; |
| 186 |
|
| 187 |
$junction = $this->getThrough(); |
| 188 |
|
| 189 |
if (! $junction instanceof Junction) { |
| 190 |
$relations = new Relations(); |
| 191 |
$junction->createRelations($relations); |
| 192 |
|
| 193 |
if ($relations->has($source->getTableAlias())) { |
| 194 |
$sourceRelation = $relations->get($source->getTableAlias()); |
| 195 |
|
| 196 |
$possibleCandidateKey[] = $sourceRelation->getForeignKey(); |
| 197 |
$possibleForeignKey[] = $sourceRelation->getCandidateKey(); |
| 198 |
} |
| 199 |
|
| 200 |
if ($relations->has($target->getTableAlias())) { |
| 201 |
$targetRelation = $relations->get($target->getTableAlias()); |
| 202 |
|
| 203 |
$possibleTargetCandidateKey[] = $targetRelation->getCandidateKey(); |
| 204 |
$possibleTargetForeignKey[] = $targetRelation->getForeignKey(); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
$junctionClass = static::RELATION_CLASS; |
| 209 |
$toJunction = (new $junctionClass()) |
| 210 |
->setName($this->getThroughAlias()) |
| 211 |
->setSource($source) |
| 212 |
->setTarget($junction) |
| 213 |
->setCandidateKey($this->extractKey($possibleCandidateKey)) |
| 214 |
->setForeignKey($this->extractKey($possibleForeignKey)); |
| 215 |
|
| 216 |
$targetClass = static::RELATION_CLASS; |
| 217 |
$toTarget = (new $targetClass()) |
| 218 |
->setName($this->getName()) |
| 219 |
->setSource($junction) |
| 220 |
->setTarget($target) |
| 221 |
->setCandidateKey($this->extractKey($possibleTargetCandidateKey)) |
| 222 |
->setForeignKey($this->extractKey($possibleTargetForeignKey)); |
| 223 |
|
| 224 |
foreach ($toJunction->resolve() as $k => $v) { |
| 225 |
yield $k => $v; |
| 226 |
} |
| 227 |
|
| 228 |
foreach ($toTarget->resolve() as $k => $v) { |
| 229 |
yield $k => $v; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
protected function extractKey(array $possibleKey): string|array|null |
| 234 |
{ |
| 235 |
$filtered = array_filter($possibleKey); |
| 236 |
|
| 237 |
return array_pop($filtered); |
| 238 |
} |
| 239 |
} |