- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我一直在尝试使用 Symfony2 中的文件上传制作一个表单集合并遵循本指南
http://symfony.com/doc/master/cookbook/form/form_collections.html
但似乎无法使这部分工作:
// src/Acme/TaskBundle/Entity/Task.php
// ...
public function setTags(ArrayCollection $tags)
{
foreach ($tags as $tag) {
$tag->addTask($this);
}
$this->tags = $tags;
}
.基本上,我有一个属性实体和一个具有一对多关系的图像实体。我已经使它们的每个 FormType 和 Property Entity 保持得很好,另一方面,Image 实体的 property_id 列总是为 NULL,即使 Image 实体的其他属性得到了正确的保持。
这是属性实体:
<?php
namespace Mata\MainBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Mata\MainBundle\Entity\Image;
class Property
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="description", type="text")
*/
private $description;
/**
* @var float
*
* @ORM\Column(name="price", type="float")
*/
private $price;
/**
* @var string
*
* @ORM\Column(name="type", type="string", length=255)
*/
private $type;
/**
* @var integer
*
* @ORM\Column(name="owner", type="integer")
*/
private $owner;
/**
* @var boolean
*
* @ORM\Column(name="available", type="boolean")
*/
private $available;
/**
*
*
* @ORM\OneToMany(targetEntity="Image", mappedBy="property", cascade={"persist"})
*/
private $images;
public function __construct()
{
$this->images = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Property
*/
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 Property
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set price
*
* @param float $price
* @return Property
*/
public function setPrice($price)
{
$this->price = $price;
return $this;
}
/**
* Get price
*
* @return float
*/
public function getPrice()
{
return $this->price;
}
/**
* Set type
*
* @param string $type
* @return Property
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* Get type
*
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* Set owner
*
* @param integer $owner
* @return Property
*/
public function setOwner($owner)
{
$this->owner = $owner;
return $this;
}
/**
* Get owner
*
* @return integer
*/
public function getOwner()
{
return $this->owner;
}
/**
* Set available
*
* @param boolean $available
* @return Property
*/
public function setAvailable($available)
{
$this->available = $available;
return $this;
}
/**
* Get available
*
* @return boolean
*/
public function getAvailable()
{
return $this->available;
}
/**
* Add images
*
* @param \Mata\MainBundle\Entity\Image $images
* @return Property
*/
public function addImage(\Mata\MainBundle\Entity\Image $images)
{
$this->images[] = $images;
return $this;
}
public function setImages(ArrayCollection $images)
{
foreach ($images as $image) {
$image->setProperty($this);
}
$this->images = $images;
}
/**
* Remove images
*
* @param \Mata\MainBundle\Entity\Image $images
*/
public function removeImage(\Mata\MainBundle\Entity\Image $images)
{
$this->images->removeElement($images);
}
/**
* Get images
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getImages()
{
return $this->images;
}
}
图像实体:
namespace Mata\MainBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Image
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Mata\MainBundle\Entity\ImageRepository")
* @ORM\HasLifecycleCallbacks
*/
class Image
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @Assert\File(maxSize="6000000")
*/
public $file;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="path", type="string", length=255)
*/
private $path;
/**
* @ORM\ManyToOne(targetEntity="Property", inversedBy="images")
* @ORM\JoinColumn(name="property_id", referencedColumnName="id")
*/
protected $property;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Image
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set path
*
* @param string $path
* @return Image
*/
public function setPath($path)
{
$this->path = $path;
return $this;
}
/**
* Get path
*
* @return string
*/
public function getPath()
{
return $this->path;
}
public function getAbsolutePath()
{
return null === $this->path
? null
: $this->getUploadRootDir().'/'.$this->path;
}
public function getWebPath()
{
return null === $this->path
? null
: $this->getUploadDir().'/'.$this->path;
}
protected function getUploadRootDir()
{
// the absolute directory path where uploaded
// documents should be saved
return __DIR__.'/../../../../web/'.$this->getUploadDir();
}
protected function getUploadDir()
{
// get rid of the __DIR__ so it doesn't screw up
// when displaying uploaded doc/image in the view.
return 'uploads/documents';
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
if (null !== $this->file) {
// do whatever you want to generate a unique name
$filename = sha1(uniqid(mt_rand(), true));
$this->path = $filename.'.'.$this->file->guessExtension();
}
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
if (null === $this->file) {
return;
}
// if there is an error when moving the file, an exception will
// be automatically thrown by move(). This will properly prevent
// the entity from being persisted to the database on error
$this->file->move($this->getUploadRootDir(), $this->path);
unset($this->file);
}
/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
if ($file = $this->getAbsolutePath()) {
unlink($file);
}
}
/**
* Set property
*
* @param \Mata\MainBundle\Entity\Property $property
* @return Image
*/
public function setProperty(\Mata\MainBundle\Entity\Property $property)
{
$this->property = $property;
return $this;
}
/**
* Get property
*
* @return \Mata\MainBundle\Entity\Property
*/
public function getProperty()
{
return $this->property;
}
}
属性(property)形式类型:
namespace Mata\AdminBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Mata\AdminBundle\Form\Type\ImageType;
class PropertyType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name');
$builder->add('description');
$builder->add('price');
$builder->add('type');
$builder->add('owner');
$builder->add('available');
$builder->add('images', 'collection', array(
'type' => new ImageType(),
'allow_add' => true,
'by_reference' => false,
'allow_delete' => true,
'prototype' => true
));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Mata\MainBundle\Entity\Property',
));
}
public function getName()
{
return 'property';
}
}
图像格式类型:
namespace Mata\AdminBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class ImageType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name','text');
$builder->add('file', 'file');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Mata\MainBundle\Entity\Image',
));
}
public function getName()
{
return 'images';
}
}
Controller Action :
public function addAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$property = new Property();
$form = $this->createForm(new PropertyType(), $property);
if ($request->isMethod('POST')) {
$form->bind($request);
if ($form->isValid()) {
$em->persist($property);
$em->flush();
$this->get('session')->getFlashBag()->add('notice', 'Successfully added new Property');
return $this->redirect($this->generateUrl('mata_admin.property.create'));
}
}
return $this->render('MataAdminBundle:Property:add.html.twig',array(
'title' => 'Property',
'form' => $form->createView()
)
);
}
最佳答案
当您添加 by_reference => false 时,您应该在添加函数中设置计数器实体。
因此您的 property_id 应该正确地保留在图像中,将您属性中的 addImage 函数更改为:
public function addImage(\Mata\MainBundle\Entity\Image $image)
{
$this->images[] = $image;
$image->setProperty($this);
return $this;
}
关于php - 在 Symfony2 中使用 Form Collections 和 Doctrine 上传图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14175463/
我想知道存储库在 Doctrine 中意味着什么? 有人可以解释一下吗? 最佳答案 许多 ORM(对象关系映射器)使用的术语“存储库”, Doctrine 只是其中之一。 它意味着可以从数据存储库访问
我尝试使用 进行数据库查询 $position = $repository->findBy( array('id' => $profileId,'datum' => '10.07.2011')
几个月前,有一个命令可以将 Doctrine 1 架构文件转换为 Doctrine 2 实体,但是,该命令不再存在。 我刚刚开始使用 Symfony2 RC1。 有没有被删除的原因?我有一个 600
我有一个实体,称为 Stones,Stones 与 Attributes 有 ManyToMany 关系。 所以我查询实体以获取石头,然后将其水化以将其转换为数组。 $result = $t
我在 zend 框架 2 中使用了学说 2。要使用数据库表生成实体,使用的控制台命令是: php doctrine-module orm:convert-mapping --force --from-
我需要一个具体的代码示例,其中包含使用“多态关联”的 Doctrine 2。 让我澄清一下自己。我有一个名为 Contract 的实体,一个契约(Contract)可以有许多价格规则,这些价格规则可以
我知道条件过滤器尚不可用于查询(根据 Doctrine2 手册已知限制部分中的“26.1.4. Applying Filter Rules to any Query”),所以我想问问专家他们首选的解决
我在所有网络上搜索..但没有找到这个问题的回复。为了记录我的项目实体中的更改,我想使用 stof 条令扩展 - 可记录,但对我来说并不明显,实体的更改如何存储在数据库中?我可以定义我自己的历史表并在那
我使用两个类: namespace Test; use Doctrine\ORM\Mapping as ORM; /** *@Table() *@InheritanceType("Joined")
A previous question I asked与使用 Doctrine 和查询生成器时对结果集进行水化有关。我的问题是如何返回数组及其子集: 这是针对单个结果集的,答案很简单: $qb
我有两个实体 User和 Article多对多关系为 Article可以有很多作者。 class User { /** @var string */ public $name;
我有一个看起来像这样的查询: 我的用户实体具有如下所示的一对一关系: /** * @var UserProfile * * @ORM\OneToOne(targetEntity="UserPro
我最近正在阅读Doctrine 2's best practices并被此阻止: 25.3. Avoid composite keys Even though Doctrine fully suppo
如何在服务容器中使用 Doctrine? 该代码只会导致错误消息“ fatal error :调用未定义的方法 ...::get()”。 em = $em; } public func
向一条记录加载 Doctrine 的首选方式是什么? 我正在使用 $em = EntityManager::create($connectionOptions, $config); $dql = "s
我正在使用 Doctrine,并且想要记录所有生成的 SQL 查询。 我知道我可以使用 $q->getSqlQuery() 但我不想每次都手动执行此操作。 有没有办法自动完成? 最佳答案 如果您打开日
如何在 Doctrine 中找到分组最大值或包含最大值的行?在 SQL 中,我通常会使用自连接来执行此操作,如 here 中所述。 . 虽然可以在 Doctrine 中建立 self 关系,但有更好的
我在谷歌上浪费了数万亿个小时,但没有一个解决方案是好的。 我有这个查询生成器: $qb2=$this->createQueryBuilder('s') ->addSel
我有以下问题: 实体具有经典的创建/更新时间戳字段,应将其设置为 UTC 中的当前时间戳。 在实体上,我使用 columnDefinition 属性将它们设置为 "TIMESTAMP NOT NULL
我需要从多个相关表中执行 DQL 删除。 在 SQL 中,它是这样的: DELETE r1,r2 FROM ComRealty_objects r1, com_realty_objects_p
我是一名优秀的程序员,十分优秀!