将文件上载添加到Symfony2表单会抛出一个“;“未找到实体”;并且会话为活动错误
我有一个Symfony2表单,我想在其中添加一个文件上传对话框 根据Symfony文件(http://symfony.com/doc/2.0/cookbook/doctrine/file_uploads.html),我创建了一个文档类:将文件上载添加到Symfony2表单会抛出一个“;“未找到实体”;并且会话为活动错误,symfony,Symfony,我有一个Symfony2表单,我想在其中添加一个文件上传对话框 根据Symfony文件(http://symfony.com/doc/2.0/cookbook/doctrine/file_uploads.html),我创建了一个文档类: <?php namespace Acme\AppBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\HttpFoundation\File\UploadedFile
<?php
namespace Acme\AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
*/
class Document
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
public $path;
/**
* @Assert\File(maxSize="6000000")
*/
public $file;
public function getAbsolutePath()
{
return null === $this->path ? null : $this->getUploadRootDir().'/'.$this->path;
}
public function getWebPath()
{
return null === $this->path ? null : $this->getUploadDir().'/'.$this->path;
}
protected function getUploadRootDir()
{
// the absolute directory path where uploaded documents should be saved
return __DIR__.'/../../../../web/'.$this->getUploadDir();
}
protected function getUploadDir()
{
// get rid of the __DIR__ so it doesn't screw when displaying uploaded doc/image in the view.
return 'uploads/documents';
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
if (null !== $this->file) {
$this->path = sha1(uniqid(mt_rand(), true)).'.'.$this->file->guessExtension();
}
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
if (null === $this->file) {
return;
}
$this->file->move($this->getUploadRootDir(), $this->path);
unset($this->file);
}
/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
if ($file = $this->getAbsolutePath()) {
unlink($file);
}
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set path
*
* @param string $path
*/
public function setPath($path)
{
$this->path = $path;
}
/**
* Get path
*
* @return string
*/
public function getPath()
{
return $this->path;
}
}
但是页面标题是“找不到实体。(500内部服务器错误)”
有人能发现它找不到的实体吗?或者这就是问题所在
我用谷歌搜索了一下,检查了php.ini中session.auto_start是否设置为0,我已经清除了所有会话和缓存。。。我被难住了 原来我收到这个奇怪的会话错误消息是因为我的nginx配置中有一个错误页面定义
通过更正“我的实体”中的一些错误,解决了“未找到实体”问题。Symfony开发者栏为我提供了足够的信息来追踪问题。如果没有更多信息,很难说出哪里出了问题。我建议您直接转到
classes.php
并导航到第421行。您可能会得到一些提示,实体是您的问题还是其他问题……我也有同样的问题,无法找到未找到的实体。我还检查了开发人员栏,看不出哪个实体失败了。你能不能提供一些信息,如何处理找不到的实体。对不起,贝特里斯特,我上面发布的信息是我能提供的全部。我在调试栏中查看了我的实体,其中一些实体显示了错误。解决错误后,EntityNotFound错误消失。也许您应该创建一个新的SO问题,详细说明您的问题,并将其发布在irc.freenode.net上的#symfony room中,让一些symfony专家来看看。非常感谢您的回复和建议。
<?php
namespace Acme\AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class DocumentType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('file')
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\AppBundle\Entity\Document',
));
}
public function getName()
{
return 'document_form';
}
}
<?php
namespace Acme\AppBundle\Entity\Profile;
use Doctrine\ORM\Mapping as ORM;
use Acme\AppBundle\Entity\jDocument;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity
* @ORM\Table(name="UserProfile")
*/
class UserProfile extends GenericProfile
{
//... Other entity params
/**
* @ORM\OneToOne(cascade={"persist", "remove"}, targetEntity="Acme\AppBundle\Entity\Document")
* @ORM\JoinColumn(name="picture_id", referencedColumnName="id", onDelete="set null")
*/
protected $picture;
/**
* Set picture
*
* @param Acme\AppBundle\Entity\Document $picture
*/
//\Acme\AppBundle\Entity\Document
public function setPicture($picture)
{
$this->picture = $picture;
}
/**
* Get picture
*
* @return Acme\AppBundle\Entity\Document
*/
public function getPicture()
{
return $this->picture;
}
//... Other entity getters and setters
}
ErrorException: Warning: ini_set(): A session is active. You cannot change the session module's ini settings at this time in /var/www/wolseley-integrated-services/builds/dev/app/cache/prod/classes.php line 421