我有2个实体(帖子和图片),字段集和表单(带有控制器)。
发表: ID 标题
图片 ID 文件名 题词 POST_ID
所有形式,但我想用AJAX将(可以是多个)图像保存为png文件,当保存文件时,我想显示缩略图和输入文本(题词)。
然后,当我想上传到数据库(提交表单)时,将文件名保存在table.images中。
PostEntity
<?php
namespace Admin\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection as Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="posts")
*/
class Posts
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/** @ORM\Column(name="title", type="string", length=48) */
protected $title;
/**
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* @param string $title
* @return Posts
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* @return string
*/
public function getTitle()
{
return $this->title;
}
}
ImagesEntity
<?php
namespace Admin\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="images")
*/
class Images
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/** @ORM\Column(name="filename", type="string", length=255) */
protected $filename;
/**
* @ORM\ManyToOne(targetEntity="Admin\Entity\Posts", inversedBy="images")
* @ORM\JoinColumn(name="id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $post_id;
/**
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* @param Images $filename
* @return string
*/
public function setFilename($filename)
{
if (is_array($filename) && isset($filename['tmp_name'])) {
$filename = $filename['tmp_name'];
}
$this->filename = $filename;
}
/**
* @return string
*/
public function getFilename()
{
return $this->filename;
}
/**
* @param Images $post_id
* @return integer
*/
public function setPost_id(Posts $post_id = null)
{
$this->post_id = $post_id;
return $this;
}
/**
* @return integer
*/
public function getPost_id()
{
return $this->post_id;
}
}
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\Form\Element;
class ImagesFieldset extends Fieldset
{
public function __construct(ObjectManager $objectManager)
{
parent::__construct('image');
$this->setHydrator(new DoctrineHydrator($objectManager, 'Admin\Entity\Images'))->setObject(new Imagenes());
$this->add(array(
'name' => 'filename',
'type' => 'Zend\Form\Element\File',
));
$this->add(array(
'name' => 'epigraph',
'type' => 'Zend\Form\Element\Textarea',
));
$this->add(array(
'name' => 'post_id',
'type' => 'Zend\Form\Element\Hidden',
));
}
}
PostsFieldset
<?php
namespace Admin\Form;
use Admin\Entity\Posts;
use Doctrine\Common\Persistence\ObjectManager;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
use Zend\Form\Fieldset;
use Zend\Form\Element;
class PostsFieldset extends Fieldset
{
public function __construct(ObjectManager $objectManager)
{
parent::__construct('noticias');
$this->setHydrator(new DoctrineHydrator($objectManager, 'Admin\Entity\Posts'))->setObject(new Posts());
$this->add(array(
'name' => 'id',
'type' => 'Zend\Form\Element\Hidden',
));
$this->add(array(
'name' => 'title',
'type' => 'Zend\Form\Element\Text',
));
$imagesFieldset = new \Admin\Form\ImagesFieldset($objectManager);
$this->add(array(
'type' => 'Zend\Form\Element\Collection',
'name' => 'images',
'options' => array(
'count' => 1,
'target_element' => $imagesFieldset
)
));
}
}
PostsForm
<?php
namespace Admin\Form;
use Admin\Entity\Posts;
use Doctrine\Common\Persistence\ObjectManager;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
use Zend\Form\Form;
class PostsForm extends Form
{
public function __construct(ObjectManager $objectManager)
{
parent::__construct('new_post');
$this->setHydrator(new DoctrineHydrator($objectManager, '\Admin\Entity\Posts'));
$postsFieldset = new \Admin\Form\PostsFieldset($objectManager);
$postsFieldset->setUseAsBaseFieldset(true);
$this->add($postsFieldset);
$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',
),
));
$this->setValidationGroup([
'posts' => [
'id',
'title',
'images' => [
'filename',
'post_id',
],
],
]);
}
}
CreateAction
public function crearAction()
{
$objectManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
$form = new \Admin\Form\NoticiasForm($objectManager);
$posts= new \Admin\Entity\Posts();
$form->bind($posts);
if ($this->request->isPost()) {
$formData = array_merge_recursive(
$this->request->getPost()->toArray(),
$this->request->getFiles()->toArray()
);
$form->setData($formData);
if ($form->isValid()) {
$objectManager->persist($noticia);
$objectManager->flush();
die;
}
}
return new ViewModel([
'form' => $form,
]);
}
查看
<?php
$form->setAttribute('action', $this->url('posts', ['action' => 'create']));
$form->prepare();
echo $this->form()->openTag($form);
$posts = $form->get('posts');
?>
<?php echo $this->formHidden($posts->get('id')); ?>
<?php echo $this->formRow($posts->get('title')); ?>
<?php echo $this->formCollection($posts->get('images')); ?>
<?php echo $this->formRow($form->get('submit')); ?>
我懒得做这个问题,不做这个问题, 我正在接受来自各地的概念,但三个月前我一直试图这样做,这一次特别问我要实施的想法。 非常感谢你帮助我。