user1185430
user1185430

Reputation: 35

getClientOriginalName() undefined method

Im working in a symfony3 project where i have a onetomany relation to upload multiple files with VICH UPLOADER, and save each file as separated entity.

First entity is "Message", and has a onetomany relation with "MessageAttachment".

Message entity

/**
 * @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;
}

MessageAttachment Entity:

/**
 * @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;
}

**As you can see, in setFile() in MessageAttachments, im trying to retrieve original file name, and save it. Vich uploader can do it with annotations, but im not working in php 7 !

Everything is working ok, filesize and mimetypes are being saved correctly, except saving original name.**

This is the error symfony shows me:

[Tue Oct 17 11:58:19.031167 2017] [:error] [pid 9076:tid 1816] [client 127.0.0.1:53731] PHP Fatal error: Call to undefined method Symfony\Component\HttpFoundation\File\File::getClientOriginalName() in C:\xampp\htdocs\mindway2\src\AppBundle\Entity\MessageAttachment.php on line 121, referer: http://mindway.dev/messages/new

Any idea? i cant find why!

The real filename needs to be shown, and i need it to rename files for downloading.


Finally, i have decided to do it in the way i dont like, in controller, before persist entities:

 foreach($message->getAttachments() as $attachfile)
        {
            $attachfile->setRealname($attachfile->getFile()->getClientOriginalName());
        }

Upvotes: 1

Views: 2832

Answers (1)

Ollie in PGH
Ollie in PGH

Reputation: 2629

It says it's looking in Symfony\Component\HttpFoundation\File\ File and it doesn't exist in that class --that's true. The method getClientOriginalName is in Symfony\Component\HttpFoundation\File\ UploadedFile.

Does your use statement say File or UploadedFile? If it's not UploadedFile, change it and see what happens.

Upvotes: 1

Related Questions