10000 [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
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update upload_file.rst
  • Loading branch information
Guikingone authored Jan 15, 2017
commit 13908eb3cb464eb63dcd80be38ae5665c7b23b5a
6 changes: 3 additions & 3 deletions controller/upload_file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -444,13 +444,13 @@ controller.
$entity = $args->getEntity();

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

if ($entity->getBrochure()) {
$fileName = $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.


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