|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\orm\traits; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\orm\OrmUtils; |
|
6
|
|
|
use Ubiquity\orm\core\PendingRelationsRequest; |
|
7
|
|
|
use Ubiquity\orm\parser\ManyToManyParser; |
|
8
|
|
|
|
|
9
|
|
|
trait DAORelationsPrepareTrait { |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Prepares members associated with $instance with a ManyToMany type relationship |
|
13
|
|
|
* @param $ret array of sql conditions |
|
14
|
|
|
* @param object $instance |
|
15
|
|
|
* @param string $member Member on which a ManyToMany annotation must be present |
|
16
|
|
|
* @param array $annot used internally |
|
17
|
|
|
*/ |
|
18
|
|
View Code Duplication |
protected static function prepareManyToMany(&$ret,$instance, $member, $annot=null) { |
|
|
|
|
|
|
19
|
|
|
$class=get_class($instance); |
|
20
|
|
|
if (!isset($annot)){ |
|
21
|
|
|
$annot=OrmUtils::getAnnotationInfoMember($class, "#ManyToMany", $member); |
|
22
|
|
|
} |
|
23
|
|
|
if ($annot !== false) { |
|
24
|
|
|
$key=$annot["targetEntity"]."|".$member."|".$annot["inversedBy"]; |
|
25
|
|
|
if(!isset($ret[$key])){ |
|
26
|
|
|
$parser=new ManyToManyParser($instance, $member); |
|
27
|
|
|
$parser->init($annot); |
|
28
|
|
|
$ret[$key]=$parser; |
|
29
|
|
|
} |
|
30
|
|
|
$accessor="get" . ucfirst($ret[$key]->getMyPk()); |
|
31
|
|
|
if(method_exists($instance, $accessor)){ |
|
32
|
|
|
$fkv=$instance->$accessor(); |
|
33
|
|
|
$ret[$key]->addValue($fkv); |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Prepares members associated with $instance with a oneToMany type relationship |
|
40
|
|
|
* @param $ret array of sql conditions |
|
41
|
|
|
* @param object $instance |
|
42
|
|
|
* @param string $member Member on which a OneToMany annotation must be present |
|
43
|
|
|
* @param array $annot used internally |
|
44
|
|
|
*/ |
|
45
|
|
View Code Duplication |
protected static function prepareOneToMany(&$ret,$instance, $member, $annot=null) { |
|
|
|
|
|
|
46
|
|
|
$class=get_class($instance); |
|
47
|
|
|
if (!isset($annot)) |
|
48
|
|
|
$annot=OrmUtils::getAnnotationInfoMember($class, "#oneToMany", $member); |
|
49
|
|
|
if ($annot !== false) { |
|
50
|
|
|
$fkAnnot=OrmUtils::getAnnotationInfoMember($annot["className"], "#joinColumn", $annot["mappedBy"]); |
|
51
|
|
|
if ($fkAnnot !== false) { |
|
52
|
|
|
$fkv=OrmUtils::getFirstKeyValue($instance); |
|
53
|
|
|
$key=$annot["className"]."|".$member."|".$annot["mappedBy"]; |
|
54
|
|
|
if(!isset($ret[$key])){ |
|
55
|
|
|
$ret[$key]=new PendingRelationsRequest(); |
|
56
|
|
|
} |
|
57
|
|
|
$ret[$key]->addPartObject($instance,$fkAnnot["name"] . "= ?",$fkv); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Prepares members associated with $instance with a manyToOne type relationship |
|
64
|
|
|
* @param $ret array of sql conditions |
|
65
|
|
|
* @param object $instance |
|
66
|
|
|
* @param mixed $value |
|
67
|
|
|
* @param string $fkField |
|
68
|
|
|
* @param array $annotationArray |
|
69
|
|
|
*/ |
|
70
|
|
View Code Duplication |
protected static function prepareManyToOne(&$ret, $instance,$value, $fkField,$annotationArray) { |
|
|
|
|
|
|
71
|
|
|
$member=$annotationArray["member"]; |
|
72
|
|
|
$fk=OrmUtils::getFirstKey($annotationArray["className"]); |
|
73
|
|
|
$key=$annotationArray["className"]."|".$member."|".$fkField; |
|
74
|
|
|
if(!isset($ret[$key])){ |
|
75
|
|
|
$ret[$key]=new PendingRelationsRequest(); |
|
76
|
|
|
} |
|
77
|
|
|
$ret[$key]->addPartObject($instance,$fk . "= ?",$value); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.