|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of slick/orm package |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Slick\Orm\Mapper\Relation; |
|
11
|
|
|
use Slick\Common\Utils\Text; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* HasAndBelongsToMany relation |
|
15
|
|
|
* |
|
16
|
|
|
* @package Slick\Orm\Mapper\Relation |
|
17
|
|
|
* @author Filipe Silva <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class HasAndBelongsToMany extends HasMany |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @readwrite |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $relatedForeignKey; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @readwrite |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $relationTable; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Gets the related foreign key |
|
36
|
|
|
* |
|
37
|
|
|
* @return string |
|
38
|
|
|
*/ |
|
39
|
|
|
public function getRelatedForeignKey() |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->relatedForeignKey; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Gets the related table |
|
46
|
|
|
* |
|
47
|
|
|
* @return string |
|
48
|
|
|
*/ |
|
49
|
2 |
|
public function getRelationTable() |
|
50
|
|
|
{ |
|
51
|
2 |
|
if (is_null($this->relationTable)) { |
|
52
|
2 |
|
$parentTable = $this->getParentTableName(); |
|
53
|
2 |
|
$table = $this->getEntityDescriptor()->getTableName(); |
|
54
|
2 |
|
$names = [$parentTable, $table]; |
|
55
|
2 |
|
asort($names); |
|
56
|
2 |
|
$first = array_shift($names); |
|
57
|
2 |
|
$tableName = Text::camelCaseToSeparator($first, '#'); |
|
58
|
2 |
|
$parts = explode('#', $tableName); |
|
59
|
2 |
|
$lastName = array_pop($parts); |
|
60
|
2 |
|
$lastName = Text::singular(strtolower($lastName)); |
|
61
|
2 |
|
array_push($parts, ucfirst($lastName)); |
|
62
|
2 |
|
$tableName = lcfirst(implode('', $parts)); |
|
63
|
2 |
|
array_unshift($names, $tableName); |
|
64
|
2 |
|
$this->relationTable = implode('_', $names); |
|
65
|
|
|
|
|
66
|
2 |
|
} |
|
67
|
2 |
|
return $this->relationTable; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
} |