Ada*_*ney 5 symfony doctrine-orm is-uploaded-file symfony-2.3
我一直在使用基于食谱食谱如何使用 Symfony 2.3中的Doctrine处理文件上传的附件实体。
即使在功能测试中,它也可以正常工作。但是,将它与Doctrine DataFixtures一起使用会引起我一些问题。
[Symfony\Component\HttpFoundation\File\Exception\FileException]
The file "o-rly-copy.jpg" was not uploaded due to an unknown error.
这没有帮助,但是我确实运行php app/console doctrine:fixtures:load -v了堆栈跟踪,并且似乎不是在持久化方法上抛出了异常,而是在$manager->flush()
Attachment::setFile()需要一个实例,UploadedFile所以我想知道是否有解决方法。
看来错误发生在225行 Symfony\Component\HttpFoundation\File\UploadedFile
return $this->test ? $isOk : $isOk && is_uploaded_file($this->getPathname())
Run Code Online (Sandbox Code Playgroud)
is_uploaded_file()返回的条件,false因为文件已在服务器上。
<?php
/**
* Prepopulate the database with image attachments.
*/
final class AttachmentFixtures extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface
{
private static $imageData = array(
array(
'name' => "O RLY?",
'file' => "o-rly",
'type' => "jpg",
),
//...
);
public function getPathToImages()
{
return $this->container->get('kernel')->getRootDir() . '/../src/Acme/DemoBundle/Resources/public/default/images';
}
public function getPathToUploads()
{
return $this->container->get('kernel')->getRootDir() . '/../web/uploads/fixtures';
}
/**
* {@inheritDoc}
*/
public function load(ObjectManager $manager)
{
$imageReferences = array();
$filesystem = $this->container->get('filesystem');
foreach (self::$imageData as $image) {
$imageFilename = sprintf('%s.%s', $image['file'], $image['type']);
$copiedImageFilename = sprintf('%s-copy.%s', $image['file'], $image['type']);
$pathToImageFile = sprintf('%s/%s', $this->getPathToImages(), $imageFilename);
try {
$filesystem->copy($pathToImageFile, $pathToCopiedFile = sprintf('%s/%s', $this->getPathToUploads(), $copiedImageFilename));
$filesystem->chmod($pathToCopiedFile, 0664);
} catch (IOException $e) {
$this->container->get('logger')->err("An error occurred while copying the file or changing permissions.");
}
$imageFile = new UploadedFile(
$pathToCopiedFile, // The full temporary path to the file
$copiedImageFilename, // The original file name
'image/' . 'jpg' === $image['type'] ? 'jpeg' : $image['type'], // Mime type - The type of the file as would be provided by PHP
filesize($pathToCopiedFile),
null,
null,
true
);
$imageAttachment = new Attachment();
$imageAttachment->setName($image['name']);
$imageAttachment->setFile($imageFile);
// Populate a reference array for later use
$imageReferences['attachment-'.$image['file']] = $imageAttachment;
$manager->persist($imageAttachment);
}
$manager->flush(); // <-- Exception throw here
// Create references for each image to be used by other entities that
// maintain a relationship with that image.
foreach ($imageReferences as $referenceName => $image) {
$this->addReference($referenceName, $image);
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在有一个更好的解决方案:
的构造函数UploadedFile具有boolean $test参数,可使用禁用检查is_uploaded_file。此参数已添加用于测试/灯具代码。
只需将其设置为true,对的isValid()检查UploadedFile将不再是问题。
例:
// My data fixture code.
$test = true;
$userPhoto->setImageFile(new UploadedFile($photoDir . $photoFile, $photoFile, null, null, null, $test));
Run Code Online (Sandbox Code Playgroud)
感谢 stof ,解决方案是为的父类的实例创建Attachment::setFile()(或者Document::setFile()如果使用说明书示例)提示,并在fixtures类中创建一个新实例并将其传递给 setFile 方法UploadedFileSymfony\Component\HttpFoundation\File\File
附件.php
<?php
namespace Acme\DemoBundle\Entity;
use Symfony\Component\HttpFoundation\File\File;
//...
class Attachment
{
/**
* Sets file.
*
* @param File $file
*/
public function setFile(File $file = null)
{
$this->file = $file;
// check if we have an old image path
if (isset($this->path)) {
// store the old name to delete after the update
$this->temp = $this->path;
$this->path = null;
} else {
$this->path = 'initial';
}
}
//...
}
Run Code Online (Sandbox Code Playgroud)
附件Fixtures.php
<?php
namespace Acme\DemoBundle\DataFixtures\ORM;
use Symfony\Component\HttpFoundation\File\File;
//...
class AttachmentFixtures //...
{
//...
public function load(ObjectManager $manager)
{
//...
$imageFile = new File($pathToCopiedFile);
$imageAttachment = new Attachment();
$imageAttachment->setFile($imageFile);
//...
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10990 次 |
| 最近记录: |