我有 2个实体 (新闻和图片) Fieldset 和表单。
我想将数据(文件名的图像)上传到我的表格并将文件保存在某个文件夹中 表: IMAGES [id,filename,image_text,news_id],NEWS [id,title,sec(tion)_id,images_id] 。
实体\新闻
<?php
namespace Admin\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="news")
*/
class News
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue
*/
protected $id;
/** @ORM\Column(type="string", length=48) */
protected $titulo;
/**
* @var \Admin\Entity\Sections
* @ORM\ManyToOne(targetEntity="Admin\Entity\Sections")
* @ORM\JoinColumn(name="sec_id", referencedColumnName="id")
*/
protected $section;
/**
* @ORM\Column(name="images_id")
* @ORM\OneToMany(targetEntity="Admin\Entity\Images", mappedBy="news_id", cascade={"persist"})
*/
protected $images;
public function __construct()
{
$this->images = new ArrayCollection();
}
/*************
** SETTERS **
*************/
/**
* @param integer $id
* @return Noticias
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* @param string $title
* @return Noticias
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* @param integer $section
* @return Noticias
*/
public function setSection($section)
{
$this->section = $section;
return $this;
}
/**
* @param string $images
* @return Noticias
*/
public function setImages($images)
{
$this->images = $images;
return $this;
}
/*************
** GETTERS **
*************/
/**
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Get Seccion
* @return \Admin\Entity\Sections
*/
public function getSection()
{
return $this->section;
}
/**
* Get images
* @return \Doctrine\Common\Collections\Collection
*/
public function getImages()
{
return $this->images;
}
/**
* Convert the object to an array.
*
* @return array
*/
public function getArrayCopy()
{
return get_object_vars($this);
}
/**
* Populate from an array.
*
* @param array $data
*/
public function exchangeArray ($data = array())
{
$this->id = $data['id'];
$this->title = $data['title'];
$this->section = $data['sec_id'];
$this->images_id = $data['images_id'];
}
}
实体\图片
<?php
namespace Admin\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="images")
*/
class Images
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue
*/
protected $id;
/**
* @ORM\Column(name="images", type="string", length=255)
*/
protected $images;
/** @ORM\Column(type="string") */
protected $image_text;
/**
* @ORM\ManyToOne(targetEntity="Admin\Entity\News", inversedBy="images")
* @ORM\JoinColumn(name="news_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $news_id;
/**
* @param integer $id
* @return Images
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* @param string $images
* @return Images
*/
public function setNombre($images)
{
$this->images = $images;
return $this;
}
/**
* @param string $image_text
* @return Images
*/
public function setImage_text($image_text)
{
$this->image_text = $image_text;
return $this;
}
/**
* Get images
* @return string
*/
public function getFileName()
{
return $this->filename;
}
/**
* Get FileName
* @return string
*/
public function getFilename()
{
return $this->filename;
}
/**
* @param integer $news_id
* @return Images
*/
public function setNews_id($news_id)
{
$this->news_id = $news_id;
return $this;
}
/**
* Get not_id
* @return integer
*/
public function getNews_id()
{
return $this->news_id;
}
/**
* Convert the object to an array.
*
* @return array
*/
public function getArrayCopy()
{
return get_object_vars($this);
}
/**
* Populate from an array.
*
* @param array $data
*/
public function exchangeArray ($data = array())
{
$this->filename = $data['filename'];
$this->image_text = $data['image_text'];
$this->news_id = $data['news_id'];
}
}
表单\ NewsFieldset
<?php
namespace Admin\Form;
use Admin\Entity\News;
use Doctrine\Common\Persistence\ObjectManager;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;
class NewsFieldset extends Fieldset implements InputFilterProviderInterface
{
public function __construct(ObjectManager $objectManager)
{
parent::__construct('news');
$this->setHydrator(new DoctrineHydrator($objectManager, 'Admin\Entity\News'))->setObject(new News());
$this->add(array(
'name' => 'id',
'type' => 'Zend\Form\Element\Hidden',
));
$this->add(array(
'name' => 'title',
'type' => 'Zend\Form\Element\Text',
'attributes' => array(
'required' => 'required',
'class' => 'form-control',
'placeholder' => 'Título',
'autocomplete' => 'off',
'maxlength' => '100',
),
));
$imagesFieldset = new \Admin\Form\ImagesFieldset($objectManager);
$this->add(array(
'type' => 'Zend\Form\Element\Collection',
'name' => 'images',
'options' => array(
'count' => 1,
'target_element' => $imagesFieldset
)
));
}
public function getInputFilterSpecification()
{
return array(
'title' => array(
'required' => true
),
);
}
}
Form\ImagesFieldset
<?php
namespace Admin\Form;
use Admin\Entity\Images;
use Doctrine\Common\Persistence\ObjectManager;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;
use Zend\Form\Element;
class ImagenesFieldset extends Fieldset implements InputFilterProviderInterface
{
public function __construct(ObjectManager $objectManager)
{
parent::__construct('images');
$this->setHydrator(new DoctrineHydrator($objectManager, 'Admin\Entity\Images'))->setObject(new Images());
$this->add(array(
'name' => 'filename',
'type' => 'Zend\Form\Element\File',
));
$this->add(array(
'name' => 'image_text',
'type' => 'Zend\Form\Element\Textarea',
));
$this->add(array(
'name' => 'news_id',
'type' => 'Zend\Form\Element\Hidden',
));
}
public function getInputFilterSpecification()
{
return array(
/*'imagen' => array(
'required' => true
),*/
);
}
}
表单\ NewsForm
<?php
namespace Admin\Form;
use Admin\Entity\News;
use Doctrine\Common\Persistence\ObjectManager;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
use Zend\Form\Form;
class NewsForm extends Form
{
public function __construct(ObjectManager $objectManager)
{
parent::__construct('new_news');
$this->setAttribute('method', 'post');
$this->setAttribute('enctype','multipart/form-data');
$this->setHydrator(new DoctrineHydrator($objectManager, '\Admin\Entity\News'));
$newsFieldset = new \Admin\Form\NewsFieldset($objectManager);
$newsFieldset->setUseAsBaseFieldset(true);
$this->add($newsFieldset);
$this->add(array(
'type' => 'Zend\Form\Element\Csrf',
'name' => 'csrf',
));
$this->add(array(
'name' => 'submit',
'type' => 'Zend\Form\Element\Submit',
'attributes' => array(
'class' => 'btn btn-primary',
'id' => 'enviar',
),
));
}
}
制作行动
public function createAction()
{
$objectManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
$form = new \Admin\Form\NewsForm($objectManager);
$noticia = new \Admin\Entity\News();
$imagenes = new \Admin\Entity\Images();
$form->bind($noticia);
if ($this->request->isPost()) {
$data = array_merge_recursive(
$this->getRequest()->getPost()->toArray(),
$this->getRequest()->getFiles()->toArray()
);
$form->setData($data);
if ($form->isValid()) {
print_r($form->getData());
}
}
return new ViewModel([
'form' => $form,
]);
}
查看
<table>
<td><?php echo $this->formRow($noticia->get('title')); ?></td>
<td><?php echo $this->formRow($noticia->get('images')); ?></td>
<td><?php echo $this->formRow($noticia->get('image_text')); ?></td>
</table>
非常感谢你。