|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* To change this license header, choose License Headers in Project Properties. |
|
5
|
|
|
* To change this template file, choose Tools | Templates |
|
6
|
|
|
* and open the template in the editor. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Maslosoft\Mangan\Annotations; |
|
10
|
|
|
|
|
11
|
|
|
use Maslosoft\Addendum\Helpers\ParamsExpander; |
|
12
|
|
|
use Maslosoft\Mangan\Decorators\DbRefDecorator; |
|
13
|
|
|
use Maslosoft\Mangan\Decorators\EmbedRefDecorator; |
|
14
|
|
|
use Maslosoft\Mangan\Meta\ManganPropertyAnnotation; |
|
15
|
|
|
use Maslosoft\Mangan\Meta\RelatedMeta; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* RelatedAnnotation |
|
19
|
|
|
* Shorthand notation: |
|
20
|
|
|
* |
|
21
|
|
|
* Related(Company\Project\Projects, '_id' => 'entity_id', true) |
|
22
|
|
|
* |
|
23
|
|
|
* Expanded notation: |
|
24
|
|
|
* |
|
25
|
|
|
* Related(class = Company\Project\Projects, join = {'_id' => 'entity_id'}, updatable = true) |
|
26
|
|
|
* |
|
27
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
|
28
|
|
|
*/ |
|
29
|
|
|
class RelatedAnnotation extends ManganPropertyAnnotation |
|
30
|
|
|
{ |
|
31
|
|
|
|
|
32
|
|
|
public $class; |
|
33
|
|
|
public $join; |
|
34
|
|
|
public $updatable; |
|
35
|
|
|
public $value; |
|
36
|
|
|
|
|
37
|
|
|
public function init() |
|
38
|
|
|
{ |
|
39
|
|
|
$relMeta = $this->_getMeta(); |
|
40
|
|
|
$relMeta->single = true; |
|
41
|
|
|
$relMeta->isArray = false; |
|
42
|
|
|
$this->_entity->related = $relMeta; |
|
43
|
|
|
$this->_entity->propagateEvents = true; |
|
44
|
|
|
$this->_entity->owned = true; |
|
45
|
|
|
$this->_entity->decorators[] = DbRefDecorator::class; |
|
46
|
|
|
$this->_entity->decorators[] = EmbedRefDecorator::class; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* |
|
51
|
|
|
* @return RelatedMeta |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function _getMeta() |
|
54
|
|
|
{ |
|
55
|
|
|
$data = ParamsExpander::expand($this, ['class', 'join', 'updatable']); |
|
56
|
|
|
$relMeta = new RelatedMeta($data); |
|
57
|
|
|
if (!$relMeta->class) |
|
58
|
|
|
{ |
|
59
|
|
|
$relMeta->class = $this->_meta->type()->name; |
|
60
|
|
|
} |
|
61
|
|
|
return $relMeta; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|