Php Symfony2-嵌入表单未显示
我有两个实体,厨房和厨房形象。厨房的子图像存储在KitchenImage实体中 我遇到的问题是,KitchenImage的文件输入框没有显示,但它的标签是什么 树枝文件:Php Symfony2-嵌入表单未显示,php,forms,symfony,doctrine-orm,symfony-2.3,Php,Forms,Symfony,Doctrine Orm,Symfony 2.3,我有两个实体,厨房和厨房形象。厨房的子图像存储在KitchenImage实体中 我遇到的问题是,KitchenImage的文件输入框没有显示,但它的标签是什么 树枝文件: <div class="row"> <div class="col-md-12"> {{ form_start(form, {'attr': {'role': 'form'}}) }} <div class="form-group">
<div class="row">
<div class="col-md-12">
{{ form_start(form, {'attr': {'role': 'form'}}) }}
<div class="form-group">
{{ form_label(form.name, 'Title') }}
{{ form_errors(form.name) }}
{{ form_widget(form.name, {'attr': {'class': 'form-control', 'placeholder': 'Enter title' }}) }}
</div>
<div class="row">
<div class="form-group col-md-3">
{{ form_label(form.image, 'Main Image') }}
{{ form_errors(form.image) }}
{{ form_widget(form.image) }}
<p class="help-block">Main Image</p>
</div>
<div class="form-group col-md-3">
{{ form_label(form.images, 'Sub Images') }}
{{ form_errors(form.images) }}
{{ form_widget(form.images) }}
<p class="help-block">Sub Images</p>
</div>
</div>
<div class="form-group">
{{ form_label(form.description) }}
{{ form_errors(form.description) }}
{{ form_widget(form.description, {'attr': {'class': 'form-control' }}) }}
</div>
<button type="submit" class="btn btn-default">Submit</button>
{{ form_end(form) }}
</div>
</div>
{{form_start(form,{'attr':{'role':'form'}}}}
{{form_标签(form.name,'Title')}
{{form_errors(form.name)}}
{{form_小部件(form.name,{'attr':{'class':'form control','placeholder':'Enter title'}}}}}
{{form_标签(form.image,'Main image')}
{{form_errors(form.image)}}
{{form_小部件(form.image)}
主映像
{{form_标签(form.images,'Sub-images')}
{{form_errors(form.images)}}
{{form_小部件(form.images)}
子图像
{{form_标签(form.description)}}
{{form_errors(form.description)}}
{{form_小部件(form.description,{'attr':{'class':'form control'}}}}
提交
{{form_end(form)}}
控制器
<?php
namespace PWD\AdminBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use PWD\AdminBundle\Form\Type\KitchenType;
use PWD\AdminBundle\Form\Type\KitchenImageType;
use PWD\WebsiteBundle\Entity\Kitchen;
use PWD\WebsiteBundle\Entity\KitchenImage;
class KitchenController extends Controller
{
public function addAction(Request $request)
{
$kitchen = new Kitchen();
$form = $this->createForm(new KitchenType(), $kitchen);
$form->handleRequest($request);
if ($form->isValid())
{
return "Yeah!";
}
return $this->render('PWDAdminBundle:Pages:add-kitchen.html.twig', array(
'form' => $form->createView(),
));
}
}
我很确定问题在于,您在KitchenImageType中有allow\u add
,而它应该在KitchenType中的集合中
class KitchenType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text')
->add('description', 'textarea')
->add('image', 'file')
->add('images', 'collection', array(
'type' => new KitchenImageType(),
'cascade_validation' => true,
'allow_add' => true, // *** Need this here, remove it from KitchenImageType
));
}
可能还有其他问题,但我想就是这样。您可能需要仔细阅读本书中的示例:非常仔细。要让新东西发挥作用,需要做很多事情
==================================
为了回应关于为测试添加厨房图像的评论,只需
public function addAction(Request $request)
{
$kitchen = new Kitchen();
$kitchen->addImage(new KitchenImage());
$form = $this->createForm(new KitchenType(), $kitchen);
图像和图像属性都有点混淆。稍后,为了清晰起见,您可能需要将图像重命名为类似子图像的名称,并将图像重命名为主图像。但它仍然应该有效
==================================
这是你的厨房实体,你需要它,这样事情才能正常地持续下去:
// Note that your are only passing one image at a time to argument is $image not $images
public function addImage(\PWD\WebsiteBundle\Entity\KitchenImage $kitchenImage)
{
$this->images[] = $kitchenImage;
$kitchenImage->setKitchen($this); // *** Need this for persisting
return $this;
}
=========================================
当你经历了一个解决问题的过程,却发现基本问题是完全不同的,这总是很有趣的。您需要了解文件上传的工作原理,不仅仅是在Symfony/doctor中,而是在web上
对于新项目,仔细阅读并实施以下内容:
一旦您了解了如何上传文件(即图像)以及如何存储它们(数据库中的路径,文件本身在web服务器上的某个位置),您就可以返回厨房并正确处理图像。一旦你这样做了,只需让你的主厨房图像工作,然后添加一组子图像。首先制作一个新的小树枝模板,其中只包含:{{form(form)}}。看起来不漂亮,但看看是否显示输入框。这将使问题与代码或模板隔离。@Cerad Hi-发生相同的问题-不显示图像的文件输入。确定。我假设厨房图像输入框出现了?。你们真的有厨房图像附在厨房上吗?在任何情况下,让您的控制器创建一个KitchenImageFormType并将其馈送到{{form(form)}}模板。确保模板中没有其他内容。我只是想看看表格。这将使问题与KitcheImage本身或某种收集问题隔离开来。KitchenImage具有公共属性,并且其中一些属性没有映射到数据库,这有点奇怪。@Cerad文件输入在生成KitchenImageForm类型的表单时效果良好。Ok。我假设您正在测试的厨房实体还没有任何KitchemImages,对吗?我怀疑您可能对允许添加内容有问题。在您的控制器中,将kitchenImage添加到您的厨房,然后查看发生了什么。谢谢您的帮助。我做了建议的更改,但没有区别。添加了额外的代码位。我得到了一个奇怪的结果-输入框出现了,但有两个标签和一个0打印-我是否设置了错误的关系?这是相当混乱,因为这是我第一次使用条令-由于两个文件输入而变得更加困难。重新开始会更好吗?是的,重新开始。您正在尝试学习一个更复杂的工作流程。我建议您保留现有内容,创建一个全新的Symfony 2.3项目,然后实际实施手册中的示例:。这将需要一些时间,但在它结束时,我认为你将有一个更好的理解学说和集合。另外,您将有一个独立的应用程序,您可以重新引用它。在最后一段代码中添加了一个,但留下了相同的问题。
<?php
namespace PWD\WebsiteBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* @ORM\Entity
* @ORM\Table(name="kitchen")
*/
class Kitchen
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=100)
* @Assert\NotBlank()
*/
protected $name;
/**
* @ORM\Column(type="text")
* @Assert\NotBlank()
*/
protected $description;
/**
* @Assert\File(maxSize="6000000")
* @Assert\NotNull()
* @Assert\Image(
* minWidth = 800,
* maxWidth = 800,
* minHeight = 467,
* maxHeight = 467
* )
*/
protected $image;
/**
* @ORM\OneToMany(targetEntity="KitchenImage", mappedBy="kitchen")
* @Assert\Type(type="PWD\WebsiteBundle\Entity\KitchenImage")
*/
protected $images;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
public $imagePath;
public function __construct()
{
$this->images = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Kitchen
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set description
*
* @param string $description
* @return Kitchen
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set image
*
* @param UploadedFile $image
* @return Kitchen
*/
public function setImage(UploadedFile $image = null)
{
$this->image = $image;
}
/**
* Get image
*
* @return string
*/
public function getImage()
{
return $this->image;
}
/**
* Add images
*
* @param \PWD\WebsiteBundle\Entity\KitchenImage $images
* @return Kitchen
*/
public function addImage(\PWD\WebsiteBundle\Entity\KitchenImage $images)
{
$this->images[] = $images;
return $this;
}
/**
* Remove images
*
* @param \PWD\WebsiteBundle\Entity\KitchenImage $images
*/
public function removeImage(\PWD\WebsiteBundle\Entity\KitchenImage $images)
{
$this->images->removeElement($images);
}
/**
* Get images
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getImages()
{
return $this->images;
}
/**
* Get absolute path
*/
public function getAbsolutePath()
{
return null === $this->imagePath
? null
: $this->getUploadRootDir().'/'.$this->imagePath;
}
/**
* Get web path
*/
public function getWebPath()
{
return null === $this->path
? null
: $this->getUploadDir().'/'.$this->imagePath;
}
/**
* Get upload root directory
*/
protected function getUploadRootDir()
{
return __DIR__.'/../../../../web/'.$this->getUploadDir();
}
/**
* Get upload directory
*/
protected function getUploadDir()
{
return 'uploads/our-work';
}
/**
* Upload image
*/
public function upload()
{
// The file property can be empty if the field is not required
if (null === $this->getImage()) {
return;
}
// move takes the target directory and then the
// target filename to move to
$this->getImage()->move(
$this->getUploadRootDir(),
$this->getImage()->getClientOriginalName()
);
// set the path property to the filename where you've saved the file
$this->imagePath = $this->getImage()->getClientOriginalName();
// clean up the file property as you won't need it anymore
$this->image = null;
}
/**
* Set imagePath
*
* @param string $imagePath
* @return Kitchen
*/
public function setImagePath($imagePath)
{
$this->imagePath = $imagePath;
return $this;
}
/**
* Get imagePath
*
* @return string
*/
public function getImagePath()
{
return $this->imagePath;
}
}
class KitchenType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text')
->add('description', 'textarea')
->add('image', 'file')
->add('images', 'collection', array(
'type' => new KitchenImageType(),
'cascade_validation' => true,
'allow_add' => true, // *** Need this here, remove it from KitchenImageType
));
}
public function addAction(Request $request)
{
$kitchen = new Kitchen();
$kitchen->addImage(new KitchenImage());
$form = $this->createForm(new KitchenType(), $kitchen);
// Note that your are only passing one image at a time to argument is $image not $images
public function addImage(\PWD\WebsiteBundle\Entity\KitchenImage $kitchenImage)
{
$this->images[] = $kitchenImage;
$kitchenImage->setKitchen($this); // *** Need this for persisting
return $this;
}