Skip to content

Commit 022959b

Browse files
authored
Merge pull request #2352 from Haehnchen/feature/2261-target-entity
#2260 support "targetEntity" extraction from property type for Doctrine relations
2 parents a6099a3 + 2938134 commit 022959b

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

src/main/java/fr/adrienbrault/idea/symfony2plugin/doctrine/metadata/driver/DoctrinePhpAttributeMappingDriver.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
import com.intellij.psi.PsiFile;
44
import com.jetbrains.php.lang.psi.PhpFile;
5-
import com.jetbrains.php.lang.psi.elements.Field;
6-
import com.jetbrains.php.lang.psi.elements.PhpAttribute;
7-
import com.jetbrains.php.lang.psi.elements.PhpClass;
5+
import com.jetbrains.php.lang.psi.elements.*;
86
import fr.adrienbrault.idea.symfony2plugin.doctrine.dict.DoctrineModelField;
97
import fr.adrienbrault.idea.symfony2plugin.doctrine.metadata.dict.DoctrineMetadataModel;
108
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
@@ -93,6 +91,22 @@ public DoctrineMetadataModel getMetadata(@NotNull DoctrineMappingDriverArguments
9391
String targetEntity = PhpElementsUtil.getAttributeArgumentStringByName(attribute, "targetEntity");
9492
if (StringUtils.isNotBlank(targetEntity)) {
9593
doctrineModelField.setRelation("\\" + StringUtils.stripStart(targetEntity, "\\"));
94+
} else {
95+
// #[ORM\ManyToOne]
96+
// private ?MyBike $myBike;
97+
PhpTypeDeclaration typeDeclaration = field.getTypeDeclaration();
98+
if (typeDeclaration != null) {
99+
Collection<ClassReference> classReferences = typeDeclaration.getClassReferences().stream()
100+
.filter(classReference -> !"null".equals(classReference.getCanonicalText()))
101+
.toList();
102+
103+
if (!classReferences.isEmpty()) {
104+
String fqnClass = classReferences.iterator().next().getFQN();
105+
if (fqnClass != null) {
106+
doctrineModelField.setRelation(fqnClass);
107+
}
108+
}
109+
}
96110
}
97111
}
98112
}

src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/doctrine/metadata/driver/DoctrinePhpAttributeMappingDriverTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ public void testPhpAttributesMetadata() {
5353

5454
assertEquals("\\ORM\\Foobar\\Egg", metadata.getField("eggClassStringBackslashless").getRelation());
5555
assertEquals("ManyToMany", metadata.getField("eggClassStringBackslashless").getRelationType());
56+
57+
assertEquals("\\ORM\\Foobar\\Egg", metadata.getField("eggTargetEntity").getRelation());
58+
assertEquals("ManyToMany", metadata.getField("eggTargetEntity").getRelationType());
5659
}
5760

5861
private DoctrineMetadataModel createOrmMetadata() {

src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/doctrine/metadata/driver/fixtures/attributes.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,8 @@ class AttributeEntity {
5757

5858
#[ORM\ManyToMany(targetEntity: 'ORM\Foobar\Egg')]
5959
public $eggClassStringBackslashless;
60+
61+
#[ORM\ManyToMany]
62+
public null|Egg $eggTargetEntity;
6063
};
6164
}

0 commit comments

Comments
 (0)