-
-
Notifications
You must be signed in to change notification settings - Fork 504
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use typed properties for default metadata #2411
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# UPGRADE FROM 2.3 to 2.4 | ||
|
||
## Typed properties as default mapping metadata | ||
|
||
When using typed properties on Document classes, Doctrine will use these types to set defaults mapping types. | ||
|
||
If you have defined some properties like: | ||
|
||
```php | ||
#[Field] | ||
private int $myProp; | ||
``` | ||
|
||
This property will be stored in DB as `string` but casted back to `int`. Please note that at this | ||
time, due to backward compatibility reasons, nullable type does not imply `nullable` mapping. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,9 @@ | |
use Documents\User; | ||
use Documents\UserName; | ||
use Documents\UserRepository; | ||
use Documents74\CustomCollection; | ||
use Documents74\TypedEmbeddedDocument; | ||
use Documents74\UserTyped; | ||
use Generator; | ||
use InvalidArgumentException; | ||
use ProxyManager\Proxy\GhostObjectInterface; | ||
|
@@ -136,6 +139,54 @@ public function testFieldIsNullable(): void | |
$this->assertFalse($cm->isNullable('name'), 'By default a field should not be nullable.'); | ||
} | ||
|
||
/** | ||
* @requires PHP >= 7.4 | ||
*/ | ||
public function testFieldTypeFromReflection(): void | ||
{ | ||
$cm = new ClassMetadata(UserTyped::class); | ||
|
||
// String | ||
$cm->mapField(['fieldName' => 'username', 'length' => 50]); | ||
self::assertEquals(Type::STRING, $cm->getTypeOfField('username')); | ||
|
||
// DateTime object | ||
$cm->mapField(['fieldName' => 'dateTime']); | ||
self::assertEquals(Type::DATE, $cm->getTypeOfField('dateTime')); | ||
|
||
// DateTimeImmutable object | ||
$cm->mapField(['fieldName' => 'dateTimeImmutable']); | ||
self::assertEquals(Type::DATE_IMMUTABLE, $cm->getTypeOfField('dateTimeImmutable')); | ||
|
||
// array as hash | ||
$cm->mapField(['fieldName' => 'array']); | ||
self::assertEquals(Type::HASH, $cm->getTypeOfField('array')); | ||
|
||
// bool | ||
$cm->mapField(['fieldName' => 'boolean']); | ||
self::assertEquals(Type::BOOL, $cm->getTypeOfField('boolean')); | ||
|
||
// float | ||
$cm->mapField(['fieldName' => 'float']); | ||
self::assertEquals(Type::FLOAT, $cm->getTypeOfField('float')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
// int | ||
$cm->mapField(['fieldName' => 'int']); | ||
self::assertEquals(Type::INT, $cm->getTypeOfField('int')); | ||
|
||
$cm->mapOneEmbedded(['fieldName' => 'embedOne']); | ||
self::assertEquals(TypedEmbeddedDocument::class, $cm->getAssociationTargetClass('embedOne')); | ||
|
||
$cm->mapOneReference(['fieldName' => 'referenceOne']); | ||
self::assertEquals(UserTyped::class, $cm->getAssociationTargetClass('referenceOne')); | ||
|
||
$cm->mapManyEmbedded(['fieldName' => 'embedMany']); | ||
self::assertEquals(CustomCollection::class, $cm->getAssociationCollectionClass('embedMany')); | ||
|
||
$cm->mapManyReference(['fieldName' => 'referenceMany']); | ||
self::assertEquals(CustomCollection::class, $cm->getAssociationCollectionClass('referenceMany')); | ||
} | ||
|
||
/** | ||
* @group DDC-115 | ||
*/ | ||
|
25 changes: 25 additions & 0 deletions
25
tests/Doctrine/ODM/MongoDB/Tests/Mapping/xml/Documents74.UserTyped.dcm.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<doctrine-mongo-mapping xmlns="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping | ||
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd"> | ||
|
||
<document name="Documents74\UserTyped"> | ||
<id field-name="id" /> | ||
|
||
<field name="username"/> | ||
<field name="dateTime"/> | ||
<field name="dateTimeImmutable"/> | ||
<field name="array"/> | ||
<field name="boolean"/> | ||
<field name="float"/> | ||
<field name="int"/> | ||
|
||
<embed-one field="embedOne" /> | ||
<reference-one field="referenceOne" /> | ||
|
||
<embed-many field="embedMany" /> | ||
<reference-many field="referenceMany" /> | ||
</document> | ||
</doctrine-mongo-mapping> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd add similar note about
nullable
as in upgrade docs as the behaviour is not fully intuitional