Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a0f277a

Browse filesBrowse files
committed
[Doctrine][PropertyInfo] Detect if the ID is writeable
1 parent 6c5faef commit a0f277a
Copy full SHA for a0f277a

File tree

3 files changed

+87
-13
lines changed
Filter options

3 files changed

+87
-13
lines changed

‎src/Symfony/Bridge/Doctrine/PropertyInfo/DoctrineExtractor.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/PropertyInfo/DoctrineExtractor.php
+40-13Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
use Doctrine\Common\Persistence\Mapping\MappingException;
1616
use Doctrine\DBAL\Types\Type as DBALType;
1717
use Doctrine\ORM\EntityManagerInterface;
18+
use Doctrine\ORM\Mapping\ClassMetadata;
1819
use Doctrine\ORM\Mapping\ClassMetadataInfo;
1920
use Doctrine\ORM\Mapping\MappingException as OrmMappingException;
21+
use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface;
2022
use Symfony\Component\PropertyInfo\PropertyListExtractorInterface;
2123
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
2224
use Symfony\Component\PropertyInfo\Type;
@@ -26,7 +28,7 @@
2628
*
2729
* @author Kévin Dunglas <dunglas@gmail.com>
2830
*/
29-
class DoctrineExtractor implements PropertyListExtractorInterface, PropertyTypeExtractorInterface
31+
class DoctrineExtractor implements PropertyListExtractorInterface, PropertyTypeExtractorInterface, PropertyAccessExtractorInterface
3032
{
3133
private $entityManager;
3234
private $classMetadataFactory;
@@ -51,12 +53,8 @@ public function __construct($entityManager)
5153
*/
5254
public function getProperties($class, array $context = [])
5355
{
54-
try {
55-
$metadata = $this->entityManager ? $this->entityManager->getClassMetadata($class) : $this->classMetadataFactory->getMetadataFor($class);
56-
} catch (MappingException $exception) {
57-
return;
58-
} catch (OrmMappingException $exception) {
59-
return;
56+
if (null === $metadata = $this->getMetadata($class)) {
57+
return null;
6058
}
6159

6260
$properties = array_merge($metadata->getFieldNames(), $metadata->getAssociationNames());
@@ -77,12 +75,8 @@ public function getProperties($class, array $context = [])
7775
*/
7876
public function getTypes($class, $property, array $context = [])
7977
{
80-
try {
81-
$metadata = $this->entityManager ? $this->entityManager->getClassMetadata($class) : $this->classMetadataFactory->getMetadataFor($class);
82-
} catch (MappingException $exception) {
83-
return;
84-
} catch (OrmMappingException $exception) {
85-
return;
78+
if (null === $metadata = $this->getMetadata($class)) {
79+
return null;
8680
}
8781

8882
if ($metadata->hasAssociation($property)) {
@@ -176,6 +170,39 @@ public function getTypes($class, $property, array $context = [])
176170
}
177171
}
178172

173+
/**
174+
* {@inheritdoc}
175+
*/
176+
public function isReadable($class, $property, array $context = [])
177+
{
178+
return null;
179+
}
180+
181+
/**
182+
* {@inheritdoc}
183+
*/
184+
public function isWritable($class, $property, array $context = [])
185+
{
186+
if (
187+
null === ($metadata = $this->getMetadata($class))
188+
|| $metadata->generatorType === ClassMetadata::GENERATOR_TYPE_NONE
189+
|| !in_array($property, $metadata->getIdentifierFieldNames(), true)
190+
) {
191+
return null;
192+
}
193+
194+
return false;
195+
}
196+
197+
private function getMetadata(string $class): ?ClassMetadata
198+
{
199+
try {
200+
return $this->entityManager ? $this->entityManager->getClassMetadata($class) : $this->classMetadataFactory->getMetadataFor($class);
201+
} catch (MappingException | OrmMappingException $exception) {
202+
return null;
203+
}
204+
}
205+
179206
/**
180207
* Determines whether an association is nullable.
181208
*

‎src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/DoctrineExtractorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/DoctrineExtractorTest.php
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Doctrine\ORM\Tools\Setup;
1717
use PHPUnit\Framework\TestCase;
1818
use Symfony\Bridge\Doctrine\PropertyInfo\DoctrineExtractor;
19+
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineGeneratedValue;
1920
use Symfony\Component\PropertyInfo\Type;
2021

2122
/**
@@ -223,4 +224,13 @@ private function doTestGetTypesCatchException(bool $legacy)
223224
{
224225
$this->assertNull($this->createExtractor($legacy)->getTypes('Not\Exist', 'baz'));
225226
}
227+
228+
public function testGeneratedValueNotWritable() {
229+
$extractor = $this->createExtractor();
230+
$this->assertFalse($extractor->isWritable(DoctrineGeneratedValue::class, 'id'));
231+
$this->assertNull($extractor->isReadable(DoctrineGeneratedValue::class, 'id'));
232+
$this->assertNull($extractor->isWritable(DoctrineGeneratedValue::class, 'foo'));
233+
$this->assertNull($extractor->isReadable(DoctrineGeneratedValue::class, 'foo'));
234+
235+
}
226236
}
+37Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures;
13+
14+
use Doctrine\ORM\Mapping\Column;
15+
use Doctrine\ORM\Mapping\Entity;
16+
use Doctrine\ORM\Mapping\GeneratedValue;
17+
use Doctrine\ORM\Mapping\Id;
18+
19+
/**
20+
* @author Kévin Dunglas <dunglas@gmail.com>
21+
*
22+
* @Entity
23+
*/
24+
class DoctrineGeneratedValue
25+
{
26+
/**
27+
* @Id
28+
* @GeneratedValue(strategy="AUTO")
29+
* @Column(type="integer")
30+
*/
31+
public $id;
32+
33+
/**
34+
* @Column
35+
*/
36+
public $foo;
37+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.