|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kaliop\eZObjectWrapperBundle\Core\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Kaliop\eZObjectWrapperBundle\Core\EntityInterface; |
|
6
|
|
|
|
|
7
|
|
|
use eZ\Publish\Core\FieldType\RelationList; |
|
8
|
|
|
use eZ\Publish\Core\FieldType\Relation; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Adds the capability to fetch related objects as Entity. |
|
12
|
|
|
* ** Requires the EntityManagerAwareEntity trait as well ** |
|
13
|
|
|
*/ |
|
14
|
|
|
trait RelationTraversingEntity |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @param string $fieldName |
|
18
|
|
|
* @return EntityInterface[] |
|
19
|
|
|
*/ |
|
20
|
|
|
protected function getRelations($fieldName) |
|
21
|
|
|
{ |
|
22
|
|
|
$relatedEntities = array(); |
|
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
$fieldValue = $this->content()->getFieldValue($fieldName); |
|
|
|
|
|
|
25
|
|
|
if (! $fieldValue instanceof RelationList\Value) { |
|
26
|
|
|
throw new \RuntimeException("Field '$fieldName' is not of type RelationList"); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
$relatedContentItems = array(); |
|
30
|
|
|
foreach($fieldValue->destinationContentIds as $contentId) { |
|
31
|
|
|
// Just in case the object has been pu tin the trash or hidden and they have not updated the related fields to remove from view. |
|
32
|
|
|
try { |
|
33
|
|
|
$relatedContentItems[] = $this->getContentService()->loadContent($contentId); |
|
|
|
|
|
|
34
|
|
|
} catch (\Exception $e) { |
|
|
|
|
|
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** @var \Kaliop\eZObjectWrapperBundle\Core\EntityManager $em */ |
|
39
|
|
|
$em = $this->getEntityManager(); |
|
|
|
|
|
|
40
|
|
|
$relatedEntities = $em->loadMany($relatedContentItems); |
|
41
|
|
|
|
|
42
|
|
|
return $relatedEntities; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string $fieldName |
|
47
|
|
|
* @return EntityInterface |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function getRelation($fieldName) |
|
50
|
|
|
{ |
|
51
|
|
|
$fieldValue = $this->content()->getFieldValue($fieldName); |
|
|
|
|
|
|
52
|
|
|
if (! $fieldValue instanceof Relation\Value) { |
|
53
|
|
|
throw new \RuntimeException("Field '$fieldName' is not of type Relation"); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$relatedContentItem = $this->getContentService()->loadContent($fieldValue->destinationContentIds); |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
/** @var \Kaliop\eZObjectWrapperBundle\Core\EntityManager $em */ |
|
59
|
|
|
$em = $this->getEntityManager(); |
|
|
|
|
|
|
60
|
|
|
$relatedEntity = $em->load($relatedContentItem); |
|
61
|
|
|
|
|
62
|
|
|
return $relatedEntity; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.