Catchable致命错误:参数1传递给? Symfony2.1 [英] Catchable Fatal Error: Argument 1 passed to ? Symfony2.1

查看:357
本文介绍了Catchable致命错误:参数1传递给? Symfony2.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被此错误停止:


可捕获致命错误:参数1传递给
Joker\CoreBundle\ Entity \Incidentfile :: setFile()必须是
的实例Symfony \Component\HttpFoundation\File\UploadedFile,字符串给定,

中调用Applications / MAMP / htdocs / joker-repo / vendor / symfony / symfony / src / Symfony / Component / Form / Util / PropertyPath.php
在538行,并在
中定义Applications / MAMP / htdocs / joker-repo / src / Joker / CoreBundle / Entity / Incidentfile.php
line 157

Catchable Fatal Error: Argument 1 passed to Joker\CoreBundle\Entity\Incidentfile::setFile() must be an instance of Symfony\Component\HttpFoundation\File\UploadedFile, string given, called in /Applications/MAMP/htdocs/joker-repo/vendor/symfony/symfony/src/Symfony/Component/Form/Util/PropertyPath.php on line 538 and defined in /Applications/MAMP/htdocs/joker-repo/src/Joker/CoreBundle/Entity/Incidentfile.php line 157

在我的实体中,我有setter和getter for File 。我使用此方法进行文件上传:

In my entity I have setters and getters for File. I used this method to make file Upload: THIS

文件夹upload_images有权限问题,但我解决了一切正常,但现在出了问题。

And there was permission problem for folder upload_images but after i solved it everything worked fine but now something went wrong.

是实体

/**
 * Incidentfile
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Joker\CoreBundle\Entity\IncidentfileRepository")
 * @ORM\HasLifecycleCallbacks
 */
class Incidentfile
{
//
//    /**
//     * @ORM\ManyToOne(targetEntity="Incidentlist", inversedBy="ListFiles")
//     * @ORM\JoinColumn(name="IncidentListId", referencedColumnName="id")
//     */
//    protected $FileList;

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="createdat", type="datetime")
     */
    private $createdat;

    /**
     * Image path
     *
     * @var string
     *
     * @ORM\Column(type="text", length=255, nullable=false)
     */
    protected $path;

    /**
     * Image file
     *
     * @var File
     *
     * @Assert\File(
     *     maxSize = "5M",
     *     mimeTypes = {"image/jpeg", "image/gif", "image/png", "image/tiff"},
     *     maxSizeMessage = "The maxmimum allowed file size is 5MB.",
     *     mimeTypesMessage = "Only the filetypes image are allowed."
     * )
     */
    protected $file;

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set createdat
     *
     * @param \DateTime $createdat
     * @return Incidentfile
     */
    public function setCreatedat($createdat)
    {
        $this->createdat = $createdat;

        return $this;
    }

    /**
     * Get createdat
     *
     * @return \DateTime 
     */
    public function getCreatedat()
    {
        return $this->createdat;
    }

    /**
     * Called before saving the entity
     *
     * @ORM\PrePersist()
     * @ORM\PreUpdate()
     */
    public function preUpload()
    {
        if (null !== $this->file) {
            // do whatever you want to generate a unique name
            $filename = sha1(uniqid(mt_rand(), true));
            $this->path = $filename.'.'.$this->file->guessExtension();
        }
    }

    /**
     * Called before entity removal
     *
     * @ORM\PreRemove()
     */
    public function removeUpload()
    {
        if ($file = $this->getAbsolutePath()) {
            unlink($file);
        }
    }

    public function getWebPath()
    {
        return null === $this->path
            ? null
            : $this->getUploadDir().'/'.$this->path;
    }

    /**
     * Called after entity persistence
     *
     * @ORM\PostPersist()
     * @ORM\PostUpdate()
     */

    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 up
        // when displaying uploaded doc/image in the view.
        return 'uploaded_images';
    }

    /**
     * Sets file.
     *
     * @param UploadedFile $file
     */
    public function setFile(UploadedFile $file = null)
    {    if (null === $file) {
        return;
    }
        $this->file = $file;

        if(in_array($file->getMimeType(),array("image/jpeg", "image/gif", "image/png", "image/tiff"))){
            $filename=rand(100000,1000000).'_'.$file->getClientOriginalName();
            $file->move($this->getUploadRootDir(),$filename
            );

            $this->path =$filename;
            $this->file = null;
        }
        else $this->path=null;
    }

    /**
     * Get file.
     *
     * @return UploadedFile
     */
    public function getFile()
    {
        return $this->file;
    }
    public function upload() {
        // the file property can be empty if the field is not required
        if (null === $this->getFile()) {
            return;
        }
        $file=$this->getFile();
        $filename=rand(100000,1000000).'_'.$file->getClientOriginalName();
        // we use the original file name here but you should
        // sanitize it at least to avoid any security issues
        // move takes the target directory and target filename as params
        $file->move($this->getUploadRootDir(),$filename
        );

        // set the path property to the filename where you've saved the file
        $this->path =$filename;

        // clean up the file property as you won't need it anymore
        $this->setFile(null);
    }

    /**
     * Set path
     *
     * @param string $path
     * @return Incidentfile
     */
    public function setPath($path)
    {
        $this->path = $path;

        return $this;
    }

    /**
     * Get path
     *
     * @return string
     */
    public function getPath()
    {
        return $this->path;
    }

    /**
     * Set FileList
     *
     * @param \Joker\CoreBundle\Entity\Incidentlist $fileList
     * @return Incidentfile
     */
    public function setFileList(\Joker\CoreBundle\Entity\Incidentlist $fileList = null)
    {
        $this->FileList = $fileList;

        return $this;
    }

    /**
     * Get FileList
     *
     * @return \Joker\CoreBundle\Entity\Incidentlist 
     */
    public function getFileList()
    {
        return $this->FileList;
    }


}

这是我的控制器:

public function TestAction(Request $request){
        $incfileRepo = new Incidentfile();
        $formfile = $this->createForm(new IncfileType(), $incfileRepo);


        if ($request->isMethod('POST')) {
            $formfile->bind($request);

            if ($formfile->isValid()) {


                $em = $this->getDoctrine()->getManager();

                $em->persist($incfileRepo);
                $em->flush();

                return $this->redirect($this->generateUrl('incident_show'));
            }
        }

        $spotEntity = $this->getCurrentSpot();
        $sentryEntity = $this->getCurrentSentry();
        $messagesCollection = $this->getDoctrine()->getRepository('JokerCoreBundle:Message')->findBy(array(
            'spot'  =>  $spotEntity,
            'sentry'    =>  $sentryEntity
        ), array(
            'sent_date' =>  'DESC'
        ));

        return $this->render('JokerAdminBundle:incidents:addfullform.html.twig', $this->getViewConstants(array(
            'formfile'=>$formfile->createView(),
            'spot'  =>  $spotEntity,
            'sentry'    =>  $sentryEntity,
            'messagesCollection'    =>  $messagesCollection,
        )));


    }

/ strong>

Form in twig:

 <form action="" method="post">
                    {{ form_rest(formfile) }}

                    <div class="singleTableForm">
                        <input style="float: left; margin-left: 338px; " type="submit" name="" class="button" value="Add" />
                    </div>
                </form>


推荐答案

我发现什么错了。问题:

I found out what was wrong.. In twig was problem:

<form action="" method="post" {{ form_enctype(formfile) }}>

只需添加 ** {{form_enctype(formfile)}} **

这篇关于Catchable致命错误:参数1传递给? Symfony2.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆