8000 [File Uploads] Check if the entity contains the files by Guikingone · Pull Request #7358 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

[File Uploads] Check if the entity contains the files #7358

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
wants to merge 4 commits into from

Conversation

Guikingone
Copy link
Contributor

After many tests, it seems that if the entity does not contains the files, the event throw a Exception, by checking the presence of files, the exception isn't thrown and the process can continue, this can be usefull if the entity isn't linked with file or if the file was stored into an array but not needed at this time.

The return isn't needed after the if statement.

After many tests, it seems that if the entity does not contains the files, the event throw a Exception, by checking the presence of files, the exception isn't thrown and the process can continue, this can be usefull if the entity isn't linked with file or if the file was stored into an array but not needed at this time.

The return isn't needed after the if statement.
@Guikingone Guikingone changed the title [Validations] Check if the entity contains the files [File Uploads] Check if the entity contains the files Jan 13, 2017

$entity->setBrochure(new File($this->targetPath.'/'.$fileName));

// Only for tests purpose and proper object validation
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking for the right object below indeed is needed. However, this comment looks confusing to me and I would just remove it.

$entity->setBrochure(new File($this->targetPath.'/'.$fileName));

// Only for tests purpose and proper object validation
if (!$entity instanceof ObjectWanted) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the class should be Product here

@Guikingone
Copy link
Contributor Author

Hi @xabbuh,

Just make the correction with a new commit, should be better, for the verification, my bad, i don't look at the entity checked before.

@xabbuh
Copy link
Member
xabbuh commented Jan 15, 2017

@Guikingone Thanks for the fast reaction. If you could do one minor change, this one looks ready for me to be merged: Can you please update the code to always use four spaces for indentation?

@Guikingone
Copy link
Contributor Author

@xabbuh Done, sorry for the bad identation.

@xabbuh
Copy link
Member
xabbuh commented Jan 15, 2017

👍 Thanks for this nice fix!

Status: Reviewed

Copy link
Contributor
@HeahDude HeahDude left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}

if ($entity->getBrochure()) {
$fileName = $entity->getBrochure();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can move this initialization into the if to avoid duplicating the call.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't really understand this movement, the first if check if the entity passed is the right instance, the second if is launched only if the first pass ...

If i move the second into the first, that change the whole condition because its suggest that my entity has file (who's not always the case) ...

Or maybe, you suggest something like that :

        if (!$entity instanceof Product) {
            return;
        } elseif ($entity->getBrochure()) {
            $fileName = $entity->getBrochure();

            $entity->setBrochure(new File($this->targetPath.'/'.$fileName));
        }

Not sure about the movement in fact ...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry if I wasn't clear, I mean you can write:

if ($fileName = $entity->getBrochure()) {
    $entity->setBrochure(new File($this->targetPath.'/'.$fileName));
}

Copy link
Contributor Author
@Guikingone Guikingone Jan 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, see want you thinking about, in this case, yes, that work but let's imagine we have an array of files, with this approach, the array must be read before the instantiation of File, let's imagine the case of an article with multiples images linked :

    /**
     * @param LifecycleEventArgs $args
     *
     * @throws FileNotFoundException
     */
    public function postLoad(LifecycleEventArgs $args)
    {
        $entity = $args->getObject();

        if (!$entity instanceof Article) {
            return;
        }

        // Only if the entity has store files.
        if ($entity->getImages()) {
            $filename = $entity->getImages();

            foreach ($filename as $file) {
                $entity->addImage(new File($this->imagesDir.'/'.$file));
            }
        }
    }

In this particular case, we must read every every entry and check if the entity return any entry into the array, this way, $filename become an array of images and we can instantiate every images into a file, i know, that's a particular case but my vision was about thinking this type of case.

And yes, we can iterate about the method return :

    /**
     * @param LifecycleEventArgs $args
     *
     * @throws FileNotFoundException
     */
    public function postLoad(LifecycleEventArgs $args)
    {
        $entity = $args->getObject();

        if (!$entity instanceof Article) {
            return;
        }

        // Only if the entity has store files.
        if ($entity->getImages()) {
            foreach ($entity->getImages() as $image) {
                $entity->addImage(new File($this->imagesDir.'/'.$image));
            }
        }
    }

But i find the first vision cleaner and better for future check.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same thing applies for an array ;)

if ($images = $entity->getImages()) {
    foreach ($images as $image) {
        $entity->addImage(new File($this->imagesDir.'/'.$image));
    }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, not wrong, gonna make the correction into the file.

@Guikingone
Copy link
Contributor Author

@HeahDude Should be better :)

@xabbuh
Copy link
Member
xabbuh commented Feb 28, 2017

Thank you @Guikingone.

xabbuh added a commit that referenced this pull request Feb 28, 2017
…ikingone)

This PR was submitted for the master branch but it was merged into the 2.7 branch instead (closes #7358).

Discussion
----------

[File Uploads] Check if the entity contains the files

After many tests, it seems that if the entity does not contains the files, the event throw a Exception, by checking the presence of files, the exception isn't thrown and the process can continue, this can be usefull if the entity isn't linked with file or if the file was stored into an array but not needed at this time.

The return isn't needed after the if statement.

Commits
-------

cc0d418 [File Uploads] Check if the entity contains the files
@xabbuh xabbuh closed this Feb 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0