Doctrine orm ZF2-使用条令2上传文件
我有两个实体(新闻和图像),分别是字段集和表单 我想将数据(文件名的图像)上传到我的表中,并将文件保存在某个文件夹中 表格:图像[id,文件名,图像文本,新闻id],新闻[id,标题,段落]id,图像id] 实体\新闻Doctrine orm ZF2-使用条令2上传文件,doctrine-orm,zend-framework2,Doctrine Orm,Zend Framework2,我有两个实体(新闻和图像),分别是字段集和表单 我想将数据(文件名的图像)上传到我的表中,并将文件保存在某个文件夹中 表格:图像[id,文件名,图像文本,新闻id],新闻[id,标题,段落]id,图像id] 实体\新闻 <?php namespace Admin\Entity; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\
<?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'];
}
}
查看
<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>
非常感谢。它不会从图像输入文件返回数组。Admin\Entity\News Object([id:protected]=>[tile:protected]=>Title[imagenes:protected]=>条令\Common\Collections\ArrayCollection:private]=>数组())尝试将筛选器添加到表单的InputFilter中与文件字段相对应的输入中。@AdamLundrigan谢谢,如何将文件\RenameUpload添加到表单中?我在我的操作中使用它,但我需要在Ajax中上载我的图像。您可以构建一个与表单结构匹配的完整InputFilter树(),然后将InputFilter附加到表单对象。或者,您可以在字段集上提示InputFilter规范
<?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>