Skip to content

Validation breaks when indexBy="myDate" (date field) #35604

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

Closed
victormacko opened this issue Feb 5, 2020 · 0 comments
Closed

Validation breaks when indexBy="myDate" (date field) #35604

victormacko opened this issue Feb 5, 2020 · 0 comments

Comments

@victormacko
Copy link
Contributor

victormacko commented Feb 5, 2020

When validating a doctrine object, the following error is shown;

[TypeError]                                                                                                                                                    
  Argument 1 passed to Symfony\Component\PropertyInfo\Type::__construct() must be of the type string, null given, called in /Users/victor/Documents/dev/www/sha  
  res/vendor/symfony/doctrine-bridge/PropertyInfo/DoctrineExtractor.php on line 121 

My field is declared as follows;

/**
     * @var ArrayCollection
     * @ORM\OneToMany(targetEntity="App\Entity\Announcement", mappedBy="company", orphanRemoval=true, cascade={"persist", "merge", "remove"}, indexBy="posted_date")
     * @ORM\OrderBy({"posted_date"="DESC"})
     */
    private $announcements;

The problem resides due to my indexBy declaration refers to a date column, and there's no support in the DoctrineExtractor class for determining what kind of php-type it refers to.

Something similar is raised here;
#35542

In vendor/symfony/doctrine-bridge/PropertyInfo/DoctrineExtractor.php:252 ... there's no support for 'date' fields .. eg;

    case DBALType::DATE:
                return Type::BUILTIN_TYPE_STRING;

And I think it's possibly related to a commit here;
b50360f

I started putting together a PR for this but don't understand it well enough to confidently say it won't break something else if it's just declared as a string as above.

My Symfony version is the latest - 5.0.4

fabpot added a commit that referenced this issue Feb 21, 2020
…m and some core types (fancyweb)

This PR was merged into the 3.4 branch.

Discussion
----------

[DoctrineBridge][DoctrineExtractor] Fix indexBy with custom and some core types

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | #35542 and #35604
| License       | MIT
| Doc PR        | -

For #35604:
To guess the collection key type, the `getPhpType()` method is called. But it does not handle most objects and arrays core types. This is why an indexBy datetime does not work.

For #35542:
When the php type cannot be guessed, null is returned. In this case, we cannot pass a valid builtin type to PropertyInfo Type, so we should return null.

Commits
-------

018ec1a [DoctrineBridge][DoctrineExtractor] Fix indexBy with custom and some core types
@fabpot fabpot closed this as completed Feb 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants