getClientOriginalName()未定义的方法

时间:2017-10-17 11:01:48

标签: php symfony doctrine vichuploaderbundle

我正在一个symfony3项目中工作,我有一个与VICH UPLOADER一起上传多个文件的关系,并将每个文件保存为独立的实体。

第一个实体是" Message",与" MessageAttachment"有一个关系。

消息实体

 image = StructBlock([
        ('file', ImageChooserBlock()),
        ('caption', RichTextBlock( classname='caption'  )),

    ])

MessageAttachment实体:

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

/**
 * @var CourseEdition
 *
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\CourseEdition")
 * @ORM\JoinColumn(name="edition_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
 */
private $edition;

/**
 * @var Message
 *
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Message", inversedBy="childs", cascade={"persist", "remove"})
 * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", nullable=true, onDelete="CASCADE")
 */
private $parent;


/**
 * @var User
 *
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User", cascade={"persist", "remove"})
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
 */
private $user;

/**
 * @var string
 *
 * @ORM\Column(name="subject", type="string", length=255)
 */
private $subject;

/**
 * @var string
 *
 * @ORM\Column(name="body", type="text")
 */
private $body;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="created_at", type="datetime")
 * @Gedmo\Timestampable(on="create")
 */
private $createdAt;

/**
 * @var ArrayCollection
 *
 * @ORM\OneToMany(targetEntity="AppBundle\Entity\Message", mappedBy="parent", orphanRemoval=true)
 */
private $childs;

/**
 * @var ArrayCollection
 *
 * @ORM\OneToMany(targetEntity="AppBundle\Entity\MessageAttachment", mappedBy="message", orphanRemoval=true, cascade={"persist", "remove"})
 * @Assert\Valid()
 */
private $attachments;

/**
 * @var ArrayCollection
 *
 * @ORM\OneToMany(targetEntity="AppBundle\Entity\MessageUser", mappedBy="message", orphanRemoval=true, cascade={"persist"})
 */
private $recipients;

public function __construct()
{
    $this->childs = new ArrayCollection();
    $this->attachments = new ArrayCollection();
    $this->recipients = new ArrayCollection();
}


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

/**
 * @param CourseEdition $edition
 *
 * @return Message
 */
public function setEdition($edition)
{
    $this->edition = $edition;

    return $this;
}

/**
 * @return CourseEdition
 */
public function getEdition()
{
    return $this->edition;
}

/**
 * @param Message $parent
 *
 * @return Message
 */
public function setParent($parent)
{
    $this->parent = $parent;

    return $this;
}

/**
 * @return Message
 */
public function getParent()
{
    return $this->parent;
}

/**
 * @param User $user
 *
 * @return Message
 */
public function setUser($user)
{
    $this->user = $user;

    return $this;
}

/**
 * @return User
 */
public function getUser()
{
    return $this->user;
}

/**
 * @param string $subject
 *
 * @return Message
 */
public function setSubject($subject)
{
    $this->subject = $subject;

    return $this;
}

/**
 * @return string
 */
public function getSubject()
{
    return $this->subject;
}

/**
 * @param string $body
 *
 * @return Message
 */
public function setBody($body)
{
    $this->body = $body;

    return $this;
}

/**
 * @return string
 */
public function getBody()
{
    return $this->body;
}

/**
 * @param \DateTime $createdAt
 *
 * @return Message
 */
public function setCreatedAt($createdAt)
{
    $this->createdAt = $createdAt;

    return $this;
}

/**
 * @return \DateTime
 */
public function getCreatedAt()
{
    return $this->createdAt;
}

/**
 * @param MessageAttachment $attachment
 *
 * @return Message
 */
public function addAttachment($attachment)
{
    $this->attachments->add($attachment);
    $attachment->setMessage($this);

    return $this;
}

/**
 * @param MessageAttachment $attachment
 *
 * @return Message
 */
public function removeAttachment($attachment)
{
    $this->attachments->removeElement($attachment);
    $attachment->setMessage(null);

    return $this;
}

/**
 * @return \Doctrine\Common\Collections\Collection
 */
public function getAttachments()
{
    return $this->attachments;
}

/**
 * @param Message $message
 *
 * @return Message
 */
public function addChild($message)
{
    $this->childs->add($message);
    $message->setParent($this);

    return $this;
}

/**
 * @param Message $message
 *
 * @return Message
 */
public function removeChild($message)
{
    $this->childs->removeElement($message);
    $message->setParent(null);

    return $this;
}

/**
 * @return ArrayCollection
 */
public function getChilds()
{
    return $this->childs;
}

/**
 * @param NoticeUser $recipient
 *
 * @return Message
 */
public function addRecipient($recipient)
{
    $this->recipients->add($recipient);

    return $this;
}

/**
 * @param NoticeUser $recipient
 *
 * @return Message
 */
public function removeRecipient($recipient)
{
    $this->recipients->removeElement($recipient);

    return $this;
}

/**
 * @return ArrayCollection
 */
public function getRecipients()
{
    return $this->recipients;
}

**正如您所看到的,在MessageAttachments中的setFile()中,我试图检索原始文件名,并保存它。 Vich上传器可以使用注释来完成,但我不能在php 7中工作!

除了保存原始名称外,一切正常,文件大小和mimetypes正确保存。**

这是symfony向我显示的错误:

[Tue Oct 17 11:58:19.031167 2017] [:error] [pid 9076:tid 1816] [client 127.0.0.1:53731] PHP致命错误:调用未定义的方法Symfony \ Component \ HttpFoundation \ File \ File :: getClientOriginalName()位于第121行的C:\ xampp \ htdocs \ mindway2 \ src \ AppBundle \ Entity \ MessageAttachment.php,引用者:http://mindway.dev/messages/new

有什么想法吗?我找不到原因!

需要显示真实的文件名,我需要它来重命名文件以供下载。

最后,我决定以持有人的实体之前的方式在控制器中执行此操作:

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

/**
 * @var Message
 *
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Message", inversedBy="attachments", cascade={"persist", "remove"})
 * @ORM\JoinColumn(name="message_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
 */
private $message;

/**
 * @var Filesys
 *
 * @Vich\UploadableField(mapping="doc_message_attach",fileNameProperty="name")
 */
private $file;

/**
 * @var string
 *
 * @ORM\Column(name="name", type="string", length=255)
 */
private $name;

/**
 * @var string
 *
 * @ORM\Column(name="realname", type="string", length=255)
 */
private $realname;

/**
 * @var int
 *
 * @ORM\Column(name="size", type="integer")
 */
private $size;

/**
 * @var string
 *
 * @ORM\Column(name="mime_type", type="string", length=100)
 */
private $mimeType;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="created_at", type="datetime")
 * @Gedmo\Timestampable(on="create")
 */
private $createdAt;


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

/**
 * @param Message $message
 *
 * @return MessageAttachment
 */
public function setMessage($message)
{
    $this->message = $message;

    return $this;
}

/**
 * @return Message
 */
public function getMessage()
{
    return $this->message;
}

/**
 * @param Filesys|\Symfony\Component\HttpFoundation\File\UploadedFile $file
 *
 * @return MessageAttachment
 */
public function setFile($file =  null)
{
    $this->file = $file;

    if ($file) {
        $this->size = $file->getSize();
        $this->mimeType = $file->getMimeType();
        $this->realname = $file->getClientOriginalName();
    }
    return $this;
}

/**
 * @return Filesys|null
 */
public function getFile()
{
    return $this->file;
}

/**
 * @param string $name
 *
 * @return MessageAttachment
 */
public function setName($name)
{
    $this->name = $name;

    return $this;
}

/**
 * @return string
 */
public function getName()
{
    return $this->name;
}

/**
 * @param integer $size
 *
 * @return MessageAttachment
 */
public function setSize($size)
{
    $this->size = $size;

    return $this;
}

/**
 * @return int
 */
public function getSize()
{
    return $this->size;
}

/**
 * @param string $mimeType
 *
 * @return MessageAttachment
 */
public function setMimeType($mimeType)
{
    $this->mimeType = $mimeType;

    return $this;
}

/**
 * @return string
 */
public function getMimeType()
{
    return $this->mimeType;
}

/**
 * @param \DateTime $createdAt
 *
 * @return MessageAttachment
 */
public function setCreatedAt($createdAt)
{
    $this->createdAt = $createdAt;

    return $this;
}

/**
 * @return \DateTime
 */
public function getCreatedAt()
{
    return $this->createdAt;
}

/**
 * Set realname
 *
 * @param string $realname
 *
 * @return MessageAttachment
 */
public function setRealname($realname)
{
    $this->realname = $realname;

    return $this;
}

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

1 个答案:

答案 0 :(得分:1)

它说它正在查看Symfony \ Component \ HttpFoundation \ File \ 文件,并且它在该类中不存在 - 这是真的。 getClientOriginalName方法位于Symfony \ Component \ HttpFoundation \ File \ UploadedFile 中。

您的使用声明是说文件还是UploadedFile?如果它不是UploadedFile,请更改它,看看会发生什么。