我有一个带有图像集合的产品实体。现在我要给图像排序。
我想通过拖放表单中的图像顺序来做到这一点。每个图片都有自己的名字,比如name="product['images][][url].
在简单的php中,我可以在提交后循环遍历post并将索引值设置为订单字段。但我怎么能用一个被教条浸渍的Zend形式做到这一点呢?
图像实体
<?php
/**
* BjyAuthorize Module (https://github.com/bjyoungblood/BjyAuthorize)
*
* @link https://github.com/bjyoungblood/BjyAuthorize for the canonical source repository
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace ApplicationShared'Entity;
use Doctrine'ORM'Mapping as ORM;
/**
* An example entity that represents a image.
*
* @ORM'Entity
* @ORM'Table(name="image")
*
* @author Tom Oram <tom@scl.co.uk>
*/
class Image
{
/**
* @var int
* @ORM'Id
* @ORM'Column(type="integer")
* @ORM'GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
* @ORM'Column(type="string", name="url", length=255, unique=true, nullable=true)
*/
protected $url;
/**
* @var int
* @ORM'Column(type="integer", unique=false, nullable=false)
*/
protected $order;
/**
* @ORM'ManyToOne(targetEntity="ApplicationShared'Entity'Product", inversedBy="images")
* @ORM'JoinColumn(name="product_id", referencedColumnName="id", nullable=true, onDelete="CASCADE")
*/
protected $product;
/**
* Get the id.
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set the id.
*
* @param int $id
*
* @return void
*/
public function setId($id)
{
$this->id = (int)$id;
}
/**
* Get the url.
*
* @return string
*/
public function getUrl()
{
return $this->url;
}
/**
* Set the store id.
*
* @param string $url
*
* @return void
*/
public function setUrl($url)
{
$this->url = (string) $url;
}
/**
* Allow null to remove association
*
* @param Product $product
*/
public function setProduct(Product $product = null)
{
$this->product = $product;
}
/**
* Get product.
*
* @return array
*/
public function getProduct()
{
return $this->product;
}
/**
* Get the order.
*
* @return integer
*/
public function getOrder()
{
return $this->order;
}
/**
* Set the order.
*
* @param int $order
*
* @return void
*/
public function setOrder($order)
{
$this->order = (int) $order;
}
}
产品实体
<?php
namespace ApplicationShared'Entity;
use Doctrine'Common'Collections'ArrayCollection;
use Doctrine'Common'Collections'Collection;
use Doctrine'ORM'Mapping as ORM;
use ApplicationShared'Entity'ProductInterface;
/**
*
* @ORM'Entity
* @ORM'Table(name="product")
* @ORM'Entity(repositoryClass="ApplicationShared'Repositories'ProductRepository")
*
*/
class Product implements ProductInterface
{
...
/**
* @ORM'OneToMany(targetEntity="ApplicationShared'Entity'Image", mappedBy="product", cascade={"all"},orphanRemoval=true)
* )
*/
protected $images;
/**
* Initialies the roles variable.
*/
public function __construct()
{
$this->images = new ArrayCollection();
}
...
/**
* Get images.
*
* @return array
*/
public function getImages()
{
return $this->images;
}
/**
* Add a image to the product.
*
* @param Images
*
* @return void
*/
public function addImages(Collection $images)
{
foreach ($images as $image) {
$image->setProduct($this);
$this->images->add($image);
}
}
/**
* @param Collection $images
*/
public function removeImages(Collection $images)
{
foreach ($images as $image) {
$image->setProduct(null);
$this->images->removeElement($image);
}
}
}
我的产品表格
<?php
namespace Backend'Form;
use ApplicationShared'Entity'Product;
use Doctrine'Common'Persistence'ObjectManager;
use DoctrineModule'Stdlib'Hydrator'DoctrineObject as DoctrineHydrator;
use Zend'Form'Fieldset;
use Zend'InputFilter'InputFilterProviderInterface;
class ProductFieldset extends Fieldset implements InputFilterProviderInterface
{
public function __construct(ObjectManager $objectManager, $userid, $storeid)
{
parent::__construct('product');
$this->setHydrator(new DoctrineHydrator($objectManager))
->setObject(new Product());
$imagesFieldset = new ImageFieldset($objectManager);
$this->add(array(
'type' => 'Zend'Form'Element'Collection',
'name' => 'images',
'options' => array(
'count' => 0,
'target_element' => $imagesFieldset
)
));
}
}
图片自定义字段
namespace Backend'Form;
use ApplicationShared'Entity'Image;
use Doctrine'Common'Persistence'ObjectManager;
use DoctrineModule'Stdlib'Hydrator'DoctrineObject as DoctrineHydrator;
use Zend'Form'Fieldset;
use Zend'InputFilter'InputFilterProviderInterface;
class ImageFieldset extends Fieldset implements InputFilterProviderInterface
{
public function __construct(ObjectManager $objectManager)
{
parent::__construct('image');
$this->setHydrator(new DoctrineHydrator($objectManager))
->setObject(new Image());
$this->add(array(
'type' => 'Zend'Form'Element'Hidden',
'name' => 'id',
'attributes' => array(
'class'=>'imageId'
)
));
$this->add(array(
'type' => 'Zend'Form'Element'Hidden',
'name' => 'order'
));
$this->add(array(
'name' => 'url',
'type' => 'hidden',
'attributes' => array(
'required' => 'required'
)
));
}
/**
* @return array
*/
public function getInputFilterSpecification()
{
return array(
'id' => array(
'required' => false
),
'url' => array(
'required' => true,
),
);
}
}
您可以在图像关系中使用@OrderBy
注释(文档):
/**
* @ORM'OneToMany(targetEntity="ApplicationShared'Entity'Image", mappedBy="product", cascade={"all"}, orphanRemoval=true)
* @ORM'OrderBy({"order" = "ASC"})
*/
protected $images;