Skip to content

Latest commit

 

History

History
48 lines (37 loc) · 1.23 KB

replacing_file.md

File metadata and controls

48 lines (37 loc) · 1.23 KB

Inject files from other sources

The bundle provides a way to inject files into entities coming from other sources than HTTP uploads via forms.

When you have configured your entity like laid out in the usage page, you can use code like the one below to inject files which are coming from other sources like

  • already a file on the server,
  • downloaded with curl/wget or
  • migrate old uploads.

Example

// ...
use Acme\DemoBundle\Entity\Product;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Command\Command;
use Vich\UploaderBundle\FileAbstraction\ReplacingFile;

class MigrationCommand extends Command
{
    public function __construct(
        protected EntityManagerInterface $em,
    ) {
        parent::__construct();
    }
    // ...

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $product = new Product();
        $product->imageFile = new ReplacingFile('myFile.png');
        // ...

        $this->em->persist($product);
        $this->em->flush();

        return Command::SUCCESS;
    }
}

That was it!

Check out the docs for information on how to use the bundle! Return to the index.