Skip to content
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

Add support for not nullable mappings #1117

Open
CoalaJoe opened this issue Apr 10, 2020 · 5 comments
Open

Add support for not nullable mappings #1117

CoalaJoe opened this issue Apr 10, 2020 · 5 comments

Comments

@CoalaJoe
Copy link

CoalaJoe commented Apr 10, 2020

Feature Request

Q A
New Feature yes
RFC yes/no
BC Break no

Summary

I am using an entity that acts like a file object. Simple representation here:

class MediaObject
{
  /** @Vich\UploadableField(mapping="media_object", fileNameProperty="filename") */
  private ?SymfonyFile $file;

  protected string $filename;
  // ...
  public function __construct() {
    $this->file = null;
    $this->filename = '';
  }
  // ...
  public function setFilename(string $filename) {
    $this->filename = $filename;
  }
  // ...
}

This object is only valid as long as the file exists. So if I now want to remove the entity:

$em->remove($mediaObject);

I will be greeted with this Exception: Expected argument of type "string", "null" given at property path "filename"..

The question now is:

  • Do we update the current logic to handle non-nullable properties like:
if ($property->isNullable()) {
  $this->getAccessor()->setValue($propertyName, null);
}
  • Do we create a new Annotation or mechanic that disables setting of null.
@garak
Copy link
Collaborator

garak commented Apr 10, 2020

What about this?

class MediaObject
{
  /** @Vich\UploadableField(mapping="media_object", fileNameProperty="filename") */
  private ?SymfonyFile $file;

  protected string $filename;
  // ...
  public function __construct() {
    $this->file = null;
    $this->filename = '';
  }
  // ...
  public function setFilename(?string $filename) {
    $this->filename = $filename;
  }
  // ...
}

@CoalaJoe
Copy link
Author

I think this might generate an error, assigning null to a typed property that is not nullable. But my current workaround is essentially this:

public function setFilename(?string $filename) {
  $this->filename = (string) $filename;
}

This is something I can currently live with. I added the workaround into my tests and easily can remove them later.

But none the less, I think as typed properties and the type system of php advances, we have to update this repository.

@garak
Copy link
Collaborator

garak commented Apr 10, 2020

What about making property nullable?

@CoalaJoe
Copy link
Author

This would not solve my problem.

I see 2 problems:

  1. Why do we update the value with null without checking if it is possible.
  2. Why do we clean up the properties, when the entity is being removed anyway.

@garak
Copy link
Collaborator

garak commented Apr 10, 2020

This seems reasonable to me.
Feel free to open a PR.

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

2 participants