【发布时间】:2020-06-18 06:29:13
【问题描述】:
我正在尝试在 Symfony 4.4 中上传几张图片,但出现此错误:
在字符串上调用成员函数guessExtension()
我在 Event 和 Picture 之间有 ManyToOne 关系。 每个事件可以关联多张图片,但每张图片只能关联一个事件。
我的实体事件:
/**
* @ORM\Entity(repositoryClass="App\Repository\EventRepository")
*/
class Event
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $title;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Picture", mappedBy="event")
*/
private $pictures;
/**
* getter and setter for $this->title
*/
public function __construct()
{
$this->pictures = new ArrayCollection();
}
/**
* @return Collection|Picture[]
*/
public function getPictures()
{
return $this->pictures;
}
public function addPicture(Picture $picture)
{
if (!$this->pictures->contains($picture)) {
$this->pictures[] = $picture;
$picture->setEvent($this);
}
return $this;
}
public function removePicture(Picture $picture)
{
if ($this->pictures->contains($picture)) {
$this->pictures->removeElement($picture);
// set the owning side to null (unless already changed)
if ($picture->getEvent() === $this) {
$picture->setEvent(null);
}
}
return $this;
}
}
我的实体图片:
/**
* @ORM\Entity(repositoryClass="App\Repository\PictureRepository")
*/
class Picture
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Event", inversedBy="pictures")
* @ORM\JoinColumn(nullable=false)
*/
private $event;
public function getId(): ?int
{
return $this->id;
}
/**
* getter and setter for $this->name
*/
public function getEvent()
{
return $this->event;
}
public function setEvent(?Event $event)
{
$this->event = $event;
return $this;
}
}
EventType 表单:
$builder
->add('title', TextType::class)
->add('pictures', CollectionType::class, [
'entry_type' => PictureType::class,
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'by_reference' => false,
'label' => false
])
;
图片类型表单
$builder
->add('name', FileType::class, [
'data_class' => null,
'label' => ' '
])
;
我的控制器
/**
* @Route("/new", name="admin-spectacle-new")
*/
public function new(Request $request)
{
$event = new Event();
$form = $this->createForm(EventType::class, $event);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$images = $form->get('pictures')->getData();
foreach ($images as $image) {
$fileName = md5(uniqid()).'.'.$image->getName()->guessExtension();
$image->move($this->getParameter('image_spectacle'), $fileName);
$image->setName($fileName);
}
//...
}
return $this->render(...);
}
知道为什么我会收到此错误吗?
转储 $images
object(Doctrine\Common\Collections\ArrayCollection)[1082]
private 'elements' =>
array (size=2)
0 =>
object(App\Entity\Picture)[1183]
private 'id' => null
private 'name' => string 'C:\wamp64\tmp\php35B3.tmp' (length=25)
private 'event' =>
object(App\Entity\Event)[705]
...
$image的转储
object(App\Entity\Picture)[1581]
private 'id' => null
private 'name' => string 'C:\wamp64\tmp\phpD132.tmp' (length=25)
private 'event' =>
object(App\Entity\Event)[1103]
private 'id' => null
private 'title' => string 'azed' (length=4)
private 'description' => null
private 'age' => null
private 'synopsis' => null
private 'resume' => null
private 'details' => null
private 'pdf' => null
private 'address' => null
private 'schedule' => null
private 'minia_picture' => string 'azed-5e620d41cbcc8.jpeg' (length=23)
private 'header_picture' => string 'azed-5e620d41cc71e.jpeg' (length=23)
private 'cover_picture' => string 'azed-5e620d41cce11.png' (length=22)
private 'is_active' => int 1
private 'categoryEvent' => null
private 'pictures' =>
object(Doctrine\Common\Collections\ArrayCollection)[1104]
private 'elements' =>
array (size=2)
...
有什么想法吗?
【问题讨论】:
-
因为
$image->getName()返回字符串? -
我尝试使用
$image->guessExtension(),但也不起作用。我尝试调用类“App\Entity\Picture”的名为“guessExtension”的未定义方法 -
能否添加
$images和$image的转储?
标签: php symfony file-upload symfony4