【发布时间】:2013-12-26 11:45:41
【问题描述】:
表单集合、ZF2和Doctrine2如何上传?
当我上传图像文件时,实体Image 方法setFileName($value) 接收输入数组$_FILE('name'=>'image.jpg', 'tmp_name'=>....)。
这是我的代码:
实体图片
namespace Application\Entity;
class Image
{
/**
* @var int
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
* @ORM\Column(type="string")
*/
protected $filename;
/**
* @ORM\ManyToOne(targetEntity="Application\Entity\Product", inversedBy="images")
* @ORM\JoinColumn(name="product_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $product;
...
public function getFileName()
{
return $this->filename;
}
public function setFileName($name)
{
$this->filename = $name;
}
public function setProduct(Product $product = null)
{
$this->product = $product;
}
public function getProduct()
{
return $this->product;
}
}
实体产品
namespace Application\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection as Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="product")
*/
class Product
{
/**
* @var int
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*
*/
protected $id;
/**
* @var string
* @ORM\Column(type="string", length=255, unique=true, nullable=true)
*/
protected $name;
/**
* @ORM\OneToMany(targetEntity="Application\Entity\Image",
mappedBy="product", cascade={"persist"})
*/
protected $images;
public function __construct()
{
$this->images = new ArrayCollection();
}
/**
* Get id.
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set id.
*
* @param int $id
*
* @return void
*/
public function setId($id)
{
$this->id = (int) $id;
}
....
/**
* Add images
*
* @param Collection $image
*/
public function addImages($images)
{
foreach ($images as $image)
{
$tag->setProduct($this);
$this->images->add($image);
}
}
/**
* Remove images
*
* @param \Application\Entity\Image $images
*/
public function removeImages($images)
{
foreach ($images as $image)
{
$tag->setProduct(null);
$this->images->removeElement($image);
}
}
/**
* Get images
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getImages()
{
return $this->images;
}
}
字段集图像
class ImageFieldset extends Fieldset implements InputFilterProviderInterface
{
public function __construct(ObjectManager $objectManager)
{
parent::__construct('product-image');
$this->setHydrator(new DoctrineHydrator($objectManager,
'Application\Entity\Image'))
->setObject(new Image());
$this->add(array(
'name' => 'filename',
'type' => 'Zend\Form\Element\File',
'options' => array(
'label' => 'Photo Upload',
'label_attributes' => array(
'class' => 'form-label'
),
'multiple' => true,
'id' => 'filename'
)
)
);
}
表单产品
class ProductForm extends Form
{
public function __construct(ObjectManager $objectManager, $name)
{
parent::__construct($name);
$this->setAttribute('enctype', 'multipart/form-data');
$this->setAttribute('method', 'post')
->setHydrator(new DoctrineHydrator($objectManager,
'Application\Entity\Product'));
$imageFieldset = new ImageFieldset($objectManager);
$imageFieldset->setName("images");
$this->add(array(
'type' => 'Zend\Form\Element\Collection',
'name' => 'images',
'options' => array(
'allow_add' => true,
'allow_remove' => false,
'count' => 1,
'target_element' => $imageFieldset
)
));
$this->setValidationGroup(array(
'name',
'images',
...
));
}
产品控制器
class ProductController extends AbstractActionController
{
public function addAction()
{
$em = $this->getEntityManager();
$request = $this->getRequest();
$form = new ProductForm($em, 'product');
$product = new Product();
$form->bind($product);
if ($request->isPost()):
$dataForm = array_merge_recursive(
$request->getPost()->toArray(),
$request->getFiles()->toArray()
);
$form->setData($dataForm);
if ($form->isValid()):
$em->persist($product);
$em->flush();
endif;
endif;
}
}
我发现最接近的东西是在 Symfony2 中: Uploading images using Form Collections and Doctrine in Symfony2
【问题讨论】:
-
您好!有什么问题,你有没有尝试写一些代码?
标签: php forms file-upload doctrine-orm zend-framework2