Skip to content

Commit 338c1d3

Browse files
author
abluchet
committed
Enable fetchable attribute on property metadata
1 parent 20bd583 commit 338c1d3

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

src/Bridge/Doctrine/Orm/Extension/EagerLoadingExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ private function addSelect(QueryBuilder $queryBuilder, string $entity, string $a
184184
}
185185

186186
//the field test allows to add methods to a Resource which do not reflect real database fields
187-
if (true === $targetClassMetadata->hasField($property) && true === $propertyMetadata->isReadable()) {
187+
if (true === $targetClassMetadata->hasField($property) && (true === $propertyMetadata->getAttribute('fetchable') || true === $propertyMetadata->isReadable())) {
188188
$select[] = $property;
189189
}
190190
}

src/Metadata/Property/PropertyMetadata.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,23 @@ public function withAttributes(array $attributes): self
305305
return $metadata;
306306
}
307307

308+
/**
309+
* Gets an attribute.
310+
*
311+
* @param string $key
312+
* @param mixed $defaultValue
313+
*
314+
* @return mixed
315+
*/
316+
public function getAttribute(string $key, $defaultValue = null)
317+
{
318+
if (isset($this->attributes[$key])) {
319+
return $this->attributes[$key];
320+
}
321+
322+
return $defaultValue;
323+
}
324+
308325
/**
309326
* Is the property inherited from a child class?
310327
*
@@ -330,11 +347,23 @@ public function withChildInherited(string $childInherited): self
330347
return $metadata;
331348
}
332349

350+
/**
351+
* Does the property have a subresource?
352+
*
353+
* @return bool|null
354+
*/
333355
public function hasSubresource()
334356
{
335357
return $this->subresource;
336358
}
337359

360+
/**
361+
* Returns a new instance with the given subresource flag.
362+
*
363+
* @param bool $subresource
364+
*
365+
* @return self
366+
*/
338367
public function withSubresource(bool $subresource = null): self
339368
{
340369
$metadata = clone $this;

tests/Bridge/Doctrine/Orm/Extension/EagerLoadingExtensionTest.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function testApplyToCollection()
4949

5050
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
5151

52-
$relatedNameCollection = new PropertyNameCollection(['id', 'name', 'notindatabase', 'notreadable']);
52+
$relatedNameCollection = new PropertyNameCollection(['id', 'name', 'notindatabase', 'notreadable', 'notreadablebutfetchable']);
5353

5454
$propertyNameCollectionFactoryProphecy->create(RelatedDummy::class)->willReturn($relatedNameCollection)->shouldBeCalled();
5555

@@ -69,10 +69,15 @@ public function testApplyToCollection()
6969
$notReadablePropertyMetadata = new PropertyMetadata();
7070
$notReadablePropertyMetadata = $notReadablePropertyMetadata->withReadable(false);
7171

72+
$notReadableButFetchablePropertyMetadata = new PropertyMetadata();
73+
$notReadableButFetchablePropertyMetadata = $notReadableButFetchablePropertyMetadata->withAttributes(['fetchable' => true]);
74+
$notReadableButFetchablePropertyMetadata = $notReadableButFetchablePropertyMetadata->withReadable(false);
75+
7276
$propertyMetadataFactoryProphecy->create(RelatedDummy::class, 'id', [])->willReturn($idPropertyMetadata)->shouldBeCalled();
7377
$propertyMetadataFactoryProphecy->create(RelatedDummy::class, 'name', [])->willReturn($namePropertyMetadata)->shouldBeCalled();
7478
$propertyMetadataFactoryProphecy->create(RelatedDummy::class, 'notindatabase', [])->willReturn($notInDatabasePropertyMetadata)->shouldBeCalled();
7579
$propertyMetadataFactoryProphecy->create(RelatedDummy::class, 'notreadable', [])->willReturn($notReadablePropertyMetadata)->shouldBeCalled();
80+
$propertyMetadataFactoryProphecy->create(RelatedDummy::class, 'notreadablebutfetchable', [])->willReturn($notReadableButFetchablePropertyMetadata)->shouldBeCalled();
7681

7782
$queryBuilderProphecy = $this->prophesize(QueryBuilder::class);
7883

@@ -101,8 +106,8 @@ public function testApplyToCollection()
101106

102107
$queryBuilderProphecy->leftJoin('o.relatedDummy', 'relatedDummy_a1')->shouldBeCalled(1);
103108
$queryBuilderProphecy->innerJoin('o.relatedDummy2', 'relatedDummy2_a2')->shouldBeCalled(1);
104-
$queryBuilderProphecy->addSelect('partial relatedDummy_a1.{id,name}')->shouldBeCalled(1);
105-
$queryBuilderProphecy->addSelect('partial relatedDummy2_a2.{id,name}')->shouldBeCalled(1);
109+
$queryBuilderProphecy->addSelect('partial relatedDummy_a1.{id,name,notreadablebutfetchable}')->shouldBeCalled(1);
110+
$queryBuilderProphecy->addSelect('partial relatedDummy2_a2.{id,name,notreadablebutfetchable}')->shouldBeCalled(1);
106111

107112
$queryBuilder = $queryBuilderProphecy->reveal();
108113
$eagerExtensionTest = new EagerLoadingExtension($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $resourceMetadataFactoryProphecy->reveal(), 30, false);

0 commit comments

Comments
 (0)