|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LaravelDoctrine\Fluent\Relations; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\Mapping\Builder\AssociationBuilder; |
|
6
|
|
|
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; |
|
7
|
|
|
use Doctrine\ORM\Mapping\NamingStrategy; |
|
8
|
|
|
use LaravelDoctrine\Fluent\Relations\Traits\ManyTo; |
|
9
|
|
|
use LaravelDoctrine\Fluent\Relations\Traits\Owning; |
|
10
|
|
|
use LaravelDoctrine\Fluent\Relations\Traits\Primary; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @method $this inversedBy($fieldName) |
|
14
|
|
|
* @method $this foreignKey($foreignKey) |
|
15
|
|
|
* @method $this localKey($localKey) |
|
16
|
|
|
* @method $this target($foreignKey) |
|
17
|
|
|
* @method $this source($localKey) |
|
18
|
|
|
* @method $this setJoinColumn($joinColumn) |
|
19
|
|
|
* @method $this setReferenceColumn($referenceColumn) |
|
20
|
|
|
* @method $this nullable() |
|
21
|
|
|
* @method $this unique() |
|
22
|
|
|
* @method $this onDelete($onDelete = null) |
|
23
|
|
|
*/ |
|
24
|
|
|
class ManyToOne extends AbstractRelation |
|
25
|
|
|
{ |
|
26
|
|
|
use ManyTo, Owning, Primary; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param ClassMetadataBuilder $builder |
|
30
|
|
|
* @param NamingStrategy $namingStrategy |
|
31
|
|
|
* @param string $relation |
|
32
|
|
|
* @param string $entity |
|
33
|
|
|
*/ |
|
34
|
45 |
|
public function __construct(ClassMetadataBuilder $builder, NamingStrategy $namingStrategy, $relation, $entity) |
|
35
|
|
|
{ |
|
36
|
45 |
|
parent::__construct($builder, $namingStrategy, $relation, $entity); |
|
37
|
|
|
|
|
38
|
45 |
|
$this->addJoinColumn($relation); |
|
39
|
45 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param ClassMetadataBuilder $builder |
|
43
|
|
|
* @param string $relation |
|
44
|
|
|
* @param string $entity |
|
45
|
|
|
* |
|
46
|
|
|
* @return AssociationBuilder |
|
47
|
|
|
*/ |
|
48
|
45 |
|
protected function createAssociation(ClassMetadataBuilder $builder, $relation, $entity) |
|
49
|
|
|
{ |
|
50
|
45 |
|
return $this->builder->createManyToOne( |
|
51
|
45 |
|
$relation, |
|
52
|
|
|
$entity |
|
53
|
45 |
|
); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param callable|null $callback |
|
58
|
|
|
* |
|
59
|
|
|
* @return JoinColumn|false |
|
60
|
|
|
*/ |
|
61
|
15 |
|
public function getJoinColumn(callable $callback = null) |
|
62
|
|
|
{ |
|
63
|
15 |
|
$joinColumn = reset($this->joinColumns); |
|
64
|
|
|
|
|
65
|
15 |
|
if (is_callable($callback)) { |
|
66
|
1 |
|
$callback($joinColumn); |
|
67
|
1 |
|
} |
|
68
|
|
|
|
|
69
|
15 |
|
return $joinColumn; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Magic call method works as a proxy for the Doctrine associationBuilder |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $method |
|
76
|
|
|
* @param array $args |
|
77
|
|
|
* |
|
78
|
|
|
* @throws \BadMethodCallException |
|
79
|
|
|
* @return $this |
|
80
|
|
|
*/ |
|
81
|
14 |
|
public function __call($method, $args) |
|
82
|
|
|
{ |
|
83
|
14 |
|
if (method_exists($this->getJoinColumn(), $method)) { |
|
84
|
6 |
|
call_user_func_array([$this->getJoinColumn(), $method], $args); |
|
85
|
|
|
|
|
86
|
6 |
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
8 |
|
parent::__call($method, $args); |
|
90
|
7 |
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|