我已经把头烧了两天而且我不知道要去哪里:我需要上传一个物体的多个图像。
请注意,我有一个名为REALTY的实体,我可以为其上传一个或多个图像。所以我提出了这个实体:
<?php
namespace AppBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Realty
*
* @ORM\Table(name="realty")
* @ORM\Entity(repositoryClass="AppBundle\Repository\RealtyRepository")
*/
class Realty extends Timestampable
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=125)
*/
private $title;
/**
* @var string
*
* @ORM\Column(name="description", type="text")
*/
private $description;
/**
* @var string
*
* @ORM\Column(name="price", type="decimal", precision=8, scale=2)
*/
private $price;
/**
* @var Advertiser
*
* @ORM\ManyToOne(targetEntity="Advertiser", inversedBy="realties")
* @ORM\JoinColumn(name="adivertiser_id", referencedColumnName="id", nullable=false)
*/
private $advertiser;
/**
* @var ArrayCollection
*
* @ORM\OneToMany(targetEntity="RealtyImage", mappedBy="realty")
*/
private $images;
/**
* Constructor
*/
public function __construct()
{
$this->images = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* @param string $title
*
* @return Realty
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set description
*
* @param string $description
*
* @return Realty
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set price
*
* @param string $price
*
* @return Realty
*/
public function setPrice($price)
{
$this->price = $price;
return $this;
}
/**
* Get price
*
* @return string
*/
public function getPrice()
{
return $this->price;
}
/**
* Set advertiser
*
* @param \AppBundle\Entity\Advertiser $advertiser
*
* @return Realty
*/
public function setAdvertiser(\AppBundle\Entity\Advertiser $advertiser)
{
$this->advertiser = $advertiser;
return $this;
}
/**
* Get advertiser
*
* @return \AppBundle\Entity\Advertiser
*/
public function getAdvertiser()
{
return $this->advertiser;
}
/**
* Add image
*
* @param \AppBundle\Entity\RealtyImage $image
*
* @return Realty
*/
public function addImage(\AppBundle\Entity\RealtyImage $image)
{
$this->images[] = $image;
return $this;
}
/**
* Remove image
*
* @param \AppBundle\Entity\RealtyImage $image
*/
public function removeImage(\AppBundle\Entity\RealtyImage $image)
{
$this->images->removeElement($image);
}
/**
* Get images
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getImages()
{
return $this->images;
}
}
然后,我创建了另一个实体来表示一对多(不动产有很多图像)。
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* RealtyImage
*
* @ORM\Table(name="realty_image")
* @ORM\Entity(repositoryClass="AppBundle\Repository\RealtyImageRepository")
*/
class RealtyImage
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="description", type="string", length=255)
*/
private $description;
/**
* @var int
*
* @ORM\Column(name="realty", type="integer")
*/
private $realty;
/**
* @var string
*
* @ORM\Column(name="path", type="string", length=255)
*/
private $path;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set description
*
* @param string $description
*
* @return RealtyImage
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set realty
*
* @param integer $realty
*
* @return RealtyImage
*/
public function setRealty($realty)
{
$this->realty = $realty;
return $this;
}
/**
* Get realty
*
* @return int
*/
public function getRealty()
{
return $this->realty;
}
/**
* Set path
*
* @param string $path
*
* @return RealtyImage
*/
public function setPath($path)
{
$this->path = $path;
return $this;
}
/**
* Get path
*
* @return string
*/
public function getPath()
{
return $this->path;
}
}
到目前为止没问题(我猜)。但后来我有FormType。这就是出问题的地方。如果我将其声明为:
<?php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class RealtyType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title')
->add('description')
->add('price')
->add('images', FileType::class, [
'multiple' => true,
])
->add('advertiser')
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Realty'
));
}
}
我会收到以下错误:
表单的视图数据应该是类的实例 Symfony \ Component \ HttpFoundation \ File \ File,但是是一个实例 class Doctrine \ Common \ Collections \ ArrayCollection。你可以避免这种情况 将“data_class”选项设置为null或添加视图时出错 转换类实例的转换器 Doctrine \ Common \ Collections \ ArrayCollection到一个实例 的Symfony \组件\ HttpFoundation \文件\文件。
我完全迷失在这里。我知道我缺少几个概念,但我不知道从哪里开始查看,在我让表单工作后我甚至没有启动“处理文件”过程。
非常感谢
答案 0 :(得分:3)
该错误的含义与它所说的一样,您正在尝试使用属性图像创建一个表单,声明类型为FileType :: class,因此您说这是一个文件,并且在实体中&#34; images& #34;被宣布为集合;属性var button = $('.confirm-box .yes-button');
$('#button1').click(function() {
button.removeClass("b").addClass("a");
});
$('#button2').click(function() {
button.removeClass("a").addClass("b");
});
button.click(function() {
if($(this).hasClass("a")){
callBackForButton1();
} else {
callBackForButton2();
}
});
无法解决您的问题。
要解决此问题,您必须执行许多bundles所做的事情,创建包含一个媒体或图像的实体,然后在与第一个实体的OneToMany关系中配置其他实体。然后,您必须使用embedded forms添加许多图像。
希望这能帮到你