Skip to content

Commit bcf03b4

Browse files
committed
Fixed missing support for embedded #2
1 parent 5102925 commit bcf03b4

File tree

8 files changed

+200
-1
lines changed

8 files changed

+200
-1
lines changed

src/EntityGenerator/EntityGenerator.php

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
/**
3434
* @phpstan-import-type FieldMapping from ClassMetadataInfo
35+
* @phpstan-import-type EmbeddedClassMapping from ClassMetadataInfo
3536
* @phpstan-import-type AssociationMapping from ClassMetadataInfo
3637
* @phpstan-import-type FileParts from GenerateEntityRequest
3738
*/
@@ -111,7 +112,11 @@ public function generate(string $className): void
111112
}
112113

113114
if ($metadata->hasField($property)) {
114-
$this->addField($request, $metadata->fieldMappings[$property]);
115+
if (\array_key_exists($property, $metadata->fieldMappings)) {
116+
$this->addField($request, $metadata->fieldMappings[$property]);
117+
} else {
118+
$this->addEmbedded($request, $property, $metadata->embeddedClasses[$property]);
119+
}
115120
} elseif ($metadata->hasAssociation($property)) {
116121
$this->addAssociation($request, $metadata->associationMappings[$property]);
117122
}
@@ -300,6 +305,47 @@ protected function addField(GenerateEntityRequest $request, array $fieldMapping)
300305
}
301306
}
302307

308+
/**
309+
* @param EmbeddedClassMapping $embeddedMapping
310+
*/
311+
protected function addEmbedded(GenerateEntityRequest $request, string $fieldName, array $embeddedMapping): void
312+
{
313+
$targetClass = $embeddedMapping['class'];
314+
$phpType = (new \ReflectionProperty($request->reflectionClass->getName(), $fieldName))->getType();
315+
316+
$targetClassAlias = $request->useStatementManipulator->addUseStatementIfNecessary($targetClass);
317+
if ($request->reflectionClass->getName() === $targetClass) {
318+
$targetClassAlias = 'self';
319+
}
320+
321+
$setMethodName = $this->buildMethodName(self::TYPE_SET, $fieldName);
322+
if (!$this->methodIsDefinedOutsideBlock($request, $setMethodName)) {
323+
$request->newBlockContents[] = $this->renderBlock($request->reflectionClass, 'embedded_set', [
324+
'methodName' => $setMethodName,
325+
'fieldName' => $fieldName,
326+
'variableName' => $this->buildVariableName(self::TYPE_SET, $fieldName),
327+
'targetClass' => $targetClass,
328+
'targetClassAlias' => $targetClassAlias,
329+
'phpType' => $phpType,
330+
'request' => $request,
331+
'embeddedMapping' => $embeddedMapping,
332+
]);
333+
}
334+
335+
$getMethodName = $this->buildMethodName(self::TYPE_GET, $fieldName);
336+
if (!$this->methodIsDefinedOutsideBlock($request, $getMethodName)) {
337+
$request->newBlockContents[] = $this->renderBlock($request->reflectionClass, 'embedded_get', [
338+
'methodName' => $getMethodName,
339+
'fieldName' => $fieldName,
340+
'targetClass' => $targetClass,
341+
'targetClassAlias' => $targetClassAlias,
342+
'phpType' => $phpType,
343+
'request' => $request,
344+
'embeddedMapping' => $embeddedMapping,
345+
]);
346+
}
347+
}
348+
303349
/**
304350
* @param AssociationMapping $associationMapping
305351
*/

templates/Theme/base.php.twig

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,24 @@
7676
{% endblock %}
7777
{% endautoescape %}
7878

79+
{# Embedded #}
80+
81+
{% block embedded_get %}
82+
public function {{ methodName }}(): ?{{ targetClassAlias }}
83+
{
84+
return $this->{{ fieldName }};
85+
}
86+
{% endblock %}
87+
88+
{% block embedded_set %}
89+
public function {{ methodName }}(?{{ targetClassAlias }} ${{ variableName }}): self
90+
{
91+
$this->{{ fieldName }} = ${{ variableName }};
92+
93+
return $this;
94+
}
95+
{% endblock %}
96+
7997
{# Associations To One #}
8098

8199
{% block assocation_to_one_get %}

tests/App/Entity/Address.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the EcommitDoctrineEntitiesGeneratorBundle package.
7+
*
8+
* (c) E-commit <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Ecommit\DoctrineEntitiesGeneratorBundle\Tests\App\Entity;
15+
16+
use Doctrine\ORM\Mapping as ORM;
17+
18+
#[ORM\Embeddable]
19+
class Address
20+
{
21+
#[ORM\Column(type: 'string', length: 20)]
22+
protected $postalCode;
23+
24+
#[ORM\Column(type: 'string', length: 20)]
25+
protected $city;
26+
27+
public function getPostalCode()
28+
{
29+
return $this->postalCode;
30+
}
31+
32+
public function setPostalCode($postalCode)
33+
{
34+
$this->postalCode = $postalCode;
35+
36+
return $this;
37+
}
38+
39+
public function getCity()
40+
{
41+
return $this->city;
42+
}
43+
44+
public function setCity($city)
45+
{
46+
$this->city = $city;
47+
48+
return $this;
49+
}
50+
51+
/*
52+
* Getters / Setters (auto-generated)
53+
*/
54+
}

tests/App/Entity/Author.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ class Author
2929
#[ORM\Column(type: 'string', length: 255)]
3030
protected $lastName;
3131

32+
#[ORM\Embedded(class: 'Ecommit\DoctrineEntitiesGeneratorBundle\Tests\App\Entity\Address')]
33+
protected $address;
34+
3235
/**
3336
* Not generated (public field).
3437
*/
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the EcommitDoctrineEntitiesGeneratorBundle package.
7+
*
8+
* (c) E-commit <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Ecommit\DoctrineEntitiesGeneratorBundle\Tests\App\GeneratedEntity;
15+
16+
use Doctrine\ORM\Mapping as ORM;
17+
18+
#[ORM\Embeddable]
19+
class Address
20+
{
21+
#[ORM\Column(type: 'string', length: 20)]
22+
protected $postalCode;
23+
24+
#[ORM\Column(type: 'string', length: 20)]
25+
protected $city;
26+
27+
public function getPostalCode()
28+
{
29+
return $this->postalCode;
30+
}
31+
32+
public function setPostalCode($postalCode)
33+
{
34+
$this->postalCode = $postalCode;
35+
36+
return $this;
37+
}
38+
39+
public function getCity()
40+
{
41+
return $this->city;
42+
}
43+
44+
public function setCity($city)
45+
{
46+
$this->city = $city;
47+
48+
return $this;
49+
}
50+
51+
/*
52+
* Getters / Setters (auto-generated)
53+
*/
54+
}

tests/App/GeneratedEntity/Author.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ class Author
3131
#[ORM\Column(type: 'string', length: 255)]
3232
protected $lastName;
3333

34+
#[ORM\Embedded(class: 'Ecommit\DoctrineEntitiesGeneratorBundle\Tests\App\GeneratedEntity\Address')]
35+
protected $address;
36+
3437
/**
3538
* Not generated (public field).
3639
*/
@@ -117,4 +120,16 @@ public function getBooks(): Collection
117120
{
118121
return $this->books;
119122
}
123+
124+
public function setAddress(?Address $address): self
125+
{
126+
$this->address = $address;
127+
128+
return $this;
129+
}
130+
131+
public function getAddress(): ?Address
132+
{
133+
return $this->address;
134+
}
120135
}

tests/GeneratedEntityTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Doctrine\ORM\EntityManagerInterface;
1818
use Doctrine\ORM\Tools\SchemaTool;
1919
use Doctrine\ORM\Tools\SchemaValidator;
20+
use Ecommit\DoctrineEntitiesGeneratorBundle\Tests\App\GeneratedEntity\Address;
2021
use Ecommit\DoctrineEntitiesGeneratorBundle\Tests\App\GeneratedEntity\Author;
2122
use Ecommit\DoctrineEntitiesGeneratorBundle\Tests\App\GeneratedEntity\Book;
2223
use Ecommit\DoctrineEntitiesGeneratorBundle\Tests\App\GeneratedEntity\Category;
@@ -618,10 +619,15 @@ protected function createCategory(int $id, ?Book $book = null): Category
618619

619620
protected function createAuthor(int $id, ?Book $book = null): Author
620621
{
622+
$address = new Address();
623+
$address->setPostalCode('74000')
624+
->setCity('Annecy');
625+
621626
$author = new Author();
622627
$author->setAuthorId($id)
623628
->setFirstName('First name '.$id)
624629
->setLastName('Last name '.$id)
630+
->setAddress($address)
625631
->phoneNumber = 'PHONE';
626632
if ($book) {
627633
$author->addBook($book);

tests/fixtures/getFileParts/author/beforeBlock.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ class Author
2929
#[ORM\Column(type: 'string', length: 255)]
3030
protected $lastName;
3131

32+
#[ORM\Embedded(class: 'Ecommit\DoctrineEntitiesGeneratorBundle\Tests\App\Entity\Address')]
33+
protected $address;
34+
3235
/**
3336
* Not generated (public field).
3437
*/

0 commit comments

Comments
 (0)