- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一对多关系
书<----* BookImage
书:
oneToMany:
bookImages:
targetEntity: BookImage
mappedBy: book
cascade: [persist,remove]
图书图片:
manyToOne:
book:
targetEntity: Book
inversedBy: bookImages
joinColumn:
name: book_id
referencedColumnName: id
所以我的表单有一个 collectionType 字段。
书籍类型:
$builder->add('bookImages', 'collection', array(
'type' => new BookImageType(),
'allow_add' => true,
// 'allow_delete' => true,
'by_reference' =>false
));
BookImageType:
$builder->add('imageName','text',array(
'constraints' => array(
new NotBlank(),
)
));
$builder->add('imageUrl','text',array(
'constraints' => array(
new NotBlank(),
)
));
Controller 通过 Ajax 调用保存数据:
public function addNewSellBookAction(Request $request)
{
$serializer = $this->container->get('jms_serializer');
$userId = $this->get('security.token_storage')->getToken()->getUser()->getId();
$em = $this->getDoctrine()->getManager();
$content = $request->get('book');
$bookData = json_decode($content, true);
$book = new Book();
$bookImage1 = new BookImage();
$bookImage1->setImageName("Amazon Book Image");
$bookImage1->setImageUrl("http://url1.com");
$book->addBookImage($bookImage1);
$bookImage2 = new BookImage();
$bookImage2->setImageName("Amazon Book Image");
$bookImage2->setImageUrl("http://url1.com");
$book->addBookImage($bookImage2);
$bookData['bookSeller']=$userId;
$bookForm = $this->createForm(new BookType(), $book);
var_dump($bookForm->getData()->getBookImages()); //Image Data Before Submit
$bookForm->submit($bookData);
var_dump($bookForm->getData()->getBookImages()); //Image Data After Submit
if($bookForm->isValid()){
$em->persist($book);
$em->flush();
return $this->createJsonResponse('success',array('successTitle'=>"Book Successfully added to sell List"));
}else{
$error= $serializer->serialize($bookForm,'json');
return new Response($error,200);
}
}
现在提交前后的 BookImage 数据是:
object(Doctrine\Common\Collections\ArrayCollection)[580]
private 'elements' =>
array (size=2)
0 =>
object(AppBundle\Entity\BookImage)[581]
protected 'id' => null
private 'imageName' => string 'Amazon Book Image' (length=17)
private 'imageUrl' => string 'http://url1.com' (length=15)
private 'titleImage' => null
private 'book' =>
object(AppBundle\Entity\Book)[578]
...
1 =>
object(AppBundle\Entity\BookImage)[582]
protected 'id' => null
private 'imageName' => string 'Amazon Book Image' (length=17)
private 'imageUrl' => string 'http://url1.com' (length=15)
private 'titleImage' => null
private 'book' =>
object(AppBundle\Entity\Book)[578]
...
object(Doctrine\Common\Collections\ArrayCollection)[580]
private 'elements' =>
array (size=2)
0 =>
object(AppBundle\Entity\BookImage)[581]
protected 'id' => null
private 'imageName' => null
private 'imageUrl' => null
private 'titleImage' => boolean false
private 'book' =>
object(AppBundle\Entity\Book)[578]
...
1 =>
object(AppBundle\Entity\BookImage)[582]
protected 'id' => null
private 'imageName' => null
private 'imageUrl' => null
private 'titleImage' => boolean false
private 'book' =>
object(AppBundle\Entity\Book)[578]
为什么提交数据后,collectionType 字段值为 null 或为空?尽管其他字段具有完美的值(value)。谁能解释一下?还是我的结构有问题?提前致谢。
图书实体
class Book
{
/**
* Constructor
*/
public function __construct()
{
$this->messages = new ArrayCollection();
$this->bookImages = new ArrayCollection();
}
/**
* @var integer
*
*/
protected $id;
/**
* @var string
*
*/
private $bookTitle;
/**
* @var string
*
*/
private $bookDirectorAuthorArtist;
/**
* @var string
*
*/
private $bookEdition;
/**
* @var string
*
*/
private $bookIsbn10;
/**
* @var string
*
*/
private $bookIsbn13;
/**
* @var string
*
*/
private $bookPublisher;
/**
* @var date
*
*/
private $bookPublishDate;
/**
* @var string
*
*/
private $bookBinding;
/**
* @var string
*
*/
private $bookPage;
/**
* @var string
*
*/
private $bookPriceSell;
/**
* @var string
*
*/
private $bookLanguage;
/**
* @var text
*
*/
private $bookDescription;
/**
* @var string
*
*/
private $bookCondition;
/**
* @var string
*
*/
private $bookIsHighlighted;
/**
* @var string
*
*/
private $bookHasNotes;
/**
* @var string
*
*/
private $bookComment;
/**
* @var string
*
*/
private $bookContactMethod;
/**
* @var string
*
*/
private $bookContactHomeNumber;
/**
* @var string
*
*/
private $bookContactCellNumber;
/**
* @var string
*
*/
private $bookContactEmail;
/**
* @var string
*
*/
private $bookIsAvailablePublic;
/**
* @var boolean
*
*/
private $bookPaymentMethodCaShOnExchange;
/**
* @var boolean
*
*/
private $bookPaymentMethodCheque;
/**
* @var date
*
*/
private $bookAvailableDate;
private $bookImages;
private $bookBuyer;
private $bookSeller;
private $messages;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set bookTitle
*
* @param string $bookTitle
* @return Book
*/
public function setBookTitle($bookTitle)
{
$this->bookTitle = $bookTitle;
return $this;
}
/**
* Get bookTitle
*
* @return string
*/
public function getBookTitle()
{
return $this->bookTitle;
}
/**
* Set bookDirectorAuthorArtist
*
* @param string $bookDirectorAuthorArtist
* @return Book
*/
public function setBookDirectorAuthorArtist($bookDirectorAuthorArtist)
{
$this->bookDirectorAuthorArtist = $bookDirectorAuthorArtist;
return $this;
}
/**
* Get bookDirectorAuthorArtist
*
* @return string
*/
public function getBookDirectorAuthorArtist()
{
return $this->bookDirectorAuthorArtist;
}
/**
* Set bookEdition
*
* @param string $bookEdition
* @return Book
*/
public function setBookEdition($bookEdition)
{
$this->bookEdition = $bookEdition;
return $this;
}
/**
* Get bookEdition
*
* @return string
*/
public function getBookEdition()
{
return $this->bookEdition;
}
/**
* Set bookIsbn10
*
* @param string $bookIsbn10
* @return Book
*/
public function setBookIsbn10($bookIsbn10)
{
$this->bookIsbn10 = $bookIsbn10;
return $this;
}
/**
* Get bookIsbn10
*
* @return string
*/
public function getBookIsbn10()
{
return $this->bookIsbn10;
}
/**
* Set bookIsbn13
*
* @param string $bookIsbn13
* @return Book
*/
public function setBookIsbn13($bookIsbn13)
{
$this->bookIsbn13 = $bookIsbn13;
return $this;
}
/**
* Get bookIsbn13
*
* @return string
*/
public function getBookIsbn13()
{
return $this->bookIsbn13;
}
/**
* Set bookPublisher
*
* @param string $bookPublisher
* @return Book
*/
public function setBookPublisher($bookPublisher)
{
$this->bookPublisher = $bookPublisher;
return $this;
}
/**
* Get bookPublisher
*
* @return string
*/
public function getBookPublisher()
{
return $this->bookPublisher;
}
/**
* Set bookPublishDate
*
* @param \DateTime $bookPublishDate
* @return Book
*/
public function setBookPublishDate($bookPublishDate)
{
$this->bookPublishDate = $bookPublishDate;
return $this;
}
/**
* Get bookPublishDate
*
* @return \DateTime
*/
public function getBookPublishDate()
{
return $this->bookPublishDate;
}
/**
* Set bookBinding
*
* @param string $bookBinding
* @return Book
*/
public function setBookBinding($bookBinding)
{
$this->bookBinding = $bookBinding;
return $this;
}
/**
* Get bookBinding
*
* @return string
*/
public function getBookBinding()
{
return $this->bookBinding;
}
/**
* Set bookPage
*
* @param string $bookPage
* @return Book
*/
public function setBookPage($bookPage)
{
$this->bookPage = $bookPage;
return $this;
}
/**
* Get bookPage
*
* @return string
*/
public function getBookPage()
{
return $this->bookPage;
}
/**
* Set bookLanguage
*
* @param string $bookLanguage
* @return Book
*/
public function setBookLanguage($bookLanguage)
{
$this->bookLanguage = $bookLanguage;
return $this;
}
/**
* Get bookLanguage
*
* @return string
*/
public function getBookLanguage()
{
return $this->bookLanguage;
}
/**
* Add messages
*
* @param \AppBundle\Entity\Message $messages
* @return Book
*/
public function addMessage(\AppBundle\Entity\Message $messages)
{
$this->messages[] = $messages;
return $this;
}
/**
* Remove messages
*
* @param \AppBundle\Entity\Message $messages
*/
public function removeMessage(\AppBundle\Entity\Message $messages)
{
$this->messages->removeElement($messages);
}
/**
* Get messages
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getMessages()
{
return $this->messages;
}
/**
* Set bookBuyer
*
* @param \AppBundle\Entity\User $bookBuyer
* @return Book
*/
public function setBookBuyer(\AppBundle\Entity\User $bookBuyer = null)
{
$this->bookBuyer = $bookBuyer;
return $this;
}
/**
* Get bookBuyer
*
* @return \AppBundle\Entity\User
*/
public function getBookBuyer()
{
return $this->bookBuyer;
}
/**
* Set bookSeller
*
* @param \AppBundle\Entity\User $bookSeller
* @return Book
*/
public function setBookSeller(\AppBundle\Entity\User $bookSeller = null)
{
$this->bookSeller = $bookSeller;
return $this;
}
/**
* Get bookSeller
*
* @return \AppBundle\Entity\User
*/
public function getBookSeller()
{
return $this->bookSeller;
}
/**
* Set bookPriceSell
*
* @param string $bookPriceSell
* @return Book
*/
public function setBookPriceSell($bookPriceSell)
{
$this->bookPriceSell = $bookPriceSell;
return $this;
}
/**
* Get bookPriceSell
*
* @return string
*/
public function getBookPriceSell()
{
return $this->bookPriceSell;
}
/**
* Set bookDescription
*
* @param string $bookDescription
* @return Book
*/
public function setBookDescription($bookDescription)
{
$this->bookDescription = $bookDescription;
return $this;
}
/**
* Get bookDescription
*
* @return string
*/
public function getBookDescription()
{
return $this->bookDescription;
}
/**
* Set bookCondition
*
* @param string $bookCondition
* @return Book
*/
public function setBookCondition($bookCondition)
{
$this->bookCondition = $bookCondition;
return $this;
}
/**
* Get bookCondition
*
* @return string
*/
public function getBookCondition()
{
return $this->bookCondition;
}
/**
* Set bookIsHighlighted
*
* @param string $bookIsHighlighted
* @return Book
*/
public function setBookIsHighlighted($bookIsHighlighted)
{
$this->bookIsHighlighted = $bookIsHighlighted;
return $this;
}
/**
* Get bookIsHighlighted
*
* @return string
*/
public function getBookIsHighlighted()
{
return $this->bookIsHighlighted;
}
/**
* Set bookHasNotes
*
* @param string $bookHasNotes
* @return Book
*/
public function setBookHasNotes($bookHasNotes)
{
$this->bookHasNotes = $bookHasNotes;
return $this;
}
/**
* Get bookHasNotes
*
* @return string
*/
public function getBookHasNotes()
{
return $this->bookHasNotes;
}
/**
* Set bookComment
*
* @param string $bookComment
* @return Book
*/
public function setBookComment($bookComment)
{
$this->bookComment = $bookComment;
return $this;
}
/**
* Get bookComment
*
* @return string
*/
public function getBookComment()
{
return $this->bookComment;
}
/**
* Set bookContactMethod
*
* @param string $bookContactMethod
* @return Book
*/
public function setBookContactMethod($bookContactMethod)
{
$this->bookContactMethod = $bookContactMethod;
return $this;
}
/**
* Get bookContactMethod
*
* @return string
*/
public function getBookContactMethod()
{
return $this->bookContactMethod;
}
/**
* Set bookContactHomeNumber
*
* @param string $bookContactHomeNumber
* @return Book
*/
public function setBookContactHomeNumber($bookContactHomeNumber)
{
$this->bookContactHomeNumber = $bookContactHomeNumber;
return $this;
}
/**
* Get bookContactHomeNumber
*
* @return string
*/
public function getBookContactHomeNumber()
{
return $this->bookContactHomeNumber;
}
/**
* Set bookContactCellNumber
*
* @param string $bookContactCellNumber
* @return Book
*/
public function setBookContactCellNumber($bookContactCellNumber)
{
$this->bookContactCellNumber = $bookContactCellNumber;
return $this;
}
/**
* Get bookContactCellNumber
*
* @return string
*/
public function getBookContactCellNumber()
{
return $this->bookContactCellNumber;
}
/**
* Set bookContactEmail
*
* @param string $bookContactEmail
* @return Book
*/
public function setBookContactEmail($bookContactEmail)
{
$this->bookContactEmail = $bookContactEmail;
return $this;
}
/**
* Get bookContactEmail
*
* @return string
*/
public function getBookContactEmail()
{
return $this->bookContactEmail;
}
/**
* Set bookIsAvailablePublic
*
* @param string $bookIsAvailablePublic
* @return Book
*/
public function setBookIsAvailablePublic($bookIsAvailablePublic)
{
$this->bookIsAvailablePublic = $bookIsAvailablePublic;
return $this;
}
/**
* Get bookIsAvailablePublic
*
* @return string
*/
public function getBookIsAvailablePublic()
{
return $this->bookIsAvailablePublic;
}
/**
* Set bookPaymentMethodCaShOnExchange
*
* @param string $bookPaymentMethodCaShOnExchange
* @return Book
*/
public function setBookPaymentMethodCaShOnExchange($bookPaymentMethodCaShOnExchange)
{
$this->bookPaymentMethodCaShOnExchange = $bookPaymentMethodCaShOnExchange;
return $this;
}
/**
* Get bookPaymentMethodCaShOnExchange
*
* @return string
*/
public function getBookPaymentMethodCaShOnExchange()
{
return $this->bookPaymentMethodCaShOnExchange;
}
/**
* Set bookPaymentMethodCheck
*
* @param string $bookPaymentMethodCheck
* @return Book
*/
public function setBookPaymentMethodCheck($bookPaymentMethodCheck)
{
$this->bookPaymentMethodCheck = $bookPaymentMethodCheck;
return $this;
}
/**
* Get bookPaymentMethodCheck
*
* @return string
*/
public function getBookPaymentMethodCheck()
{
return $this->bookPaymentMethodCheck;
}
/**
* Add bookImages
*
* @param \AppBundle\Entity\BookImage $bookImages
* @return Book
*/
public function addBookImage(\AppBundle\Entity\BookImage $bookImages)
{
$this->bookImages->add($bookImages);
$bookImages->setBook($this);
return $this;
}
/**
* Remove bookImages
*
* @param \AppBundle\Entity\BookImage $bookImages
*/
public function removeBookImage(\AppBundle\Entity\BookImage $bookImages)
{
$this->bookImages->removeElement($bookImages);
}
/**
* Get bookImages
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getBookImages()
{
return $this->bookImages;
}
/**
* Set bookPaymentMethodCheque
*
* @param boolean $bookPaymentMethodCheque
* @return Book
*/
public function setBookPaymentMethodCheque($bookPaymentMethodCheque)
{
$this->bookPaymentMethodCheque = $bookPaymentMethodCheque;
return $this;
}
/**
* Get bookPaymentMethodCheque
*
* @return boolean
*/
public function getBookPaymentMethodCheque()
{
return $this->bookPaymentMethodCheque;
}
/**
* Set bookAvailableDate
*
* @param \DateTime $bookAvailableDate
* @return Book
*/
public function setBookAvailableDate($bookAvailableDate)
{
$this->bookAvailableDate = $bookAvailableDate;
return $this;
}
/**
* Get bookAvailableDate
*
* @return \DateTime
*/
public function getBookAvailableDate()
{
return $this->bookAvailableDate;
}
public function __toString()
{
return strval($this->id);
}
}
BookImage 实体
class BookImage
{
/**
* @var integer
*
*/
protected $id;
/**
* @var string
*
*/
private $imageName;
/**
* @var string
*
*/
private $imageUrl;
/**
* @var boolean
*
*/
private $titleImage;
private $book;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set imageName
*
* @param string $imageName
* @return BookImage
*/
public function setImageName($imageName)
{
$this->imageName = $imageName;
return $this;
}
/**
* Get imageName
*
* @return string
*/
public function getImageName()
{
return $this->imageName;
}
/**
* Set imageUrl
*
* @param string $imageUrl
* @return BookImage
*/
public function setImageUrl($imageUrl)
{
$this->imageUrl = $imageUrl;
return $this;
}
/**
* Get imageUrl
*
* @return string
*/
public function getImageUrl()
{
return $this->imageUrl;
}
/**
* Set titleImage
*
* @param boolean $titleImage
* @return BookImage
*/
public function setTitleImage($titleImage)
{
$this->titleImage = $titleImage;
return $this;
}
/**
* Get titleImage
*
* @return boolean
*/
public function getTitleImage()
{
return $this->titleImage;
}
/**
* Set book
*
* @param \AppBundle\Entity\Book $book
* @return BookImage
*/
public function setBook(\AppBundle\Entity\Book $book = null)
{
$this->book = $book;
return $this;
}
/**
* Get book
*
* @return \AppBundle\Entity\Book
*/
public function getBook()
{
return $this->book;
}
public function __toString()
{
return strval($this->id);
}
}
最佳答案
我一直在尝试寻找答案近 7 天,终于想出了一个解决方案。但不知道这是否是一种不好的做法。
只需将数据作为数组 放入submitData。提交后,该值将通过表单添加到该特定实体。
public function addNewSellBookAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$serializer = $this->container->get('jms_serializer');
$userId = $this->get('security.token_storage')->getToken()->getUser()->getId();
$content = $request->get('book');
$bookData = json_decode($content, true);
$bookData['bookImages']=array();
array_push($bookData['bookImages'],array(
'imageName'=>"Amazon Book Image",
'imageUrl'=>"http://url1.com"
));
array_push($bookData['bookImages'],array(
'imageName'=>"Amazon Book Image",
'imageUrl'=>"http://url1.com"
));
$bookData['bookSeller']=$userId;
$book = new Book();
$bookForm = $this->createForm(new BookType(),$book);
var_dump($bookForm->getData()->getBookImages());
$bookForm->submit($bookData);
var_dump($book->getBookImages());
if($bookForm->isValid()){
$em->persist($book);
$em->flush();
return $this->createJsonResponse('success',array('successTitle'=>"Book Successfully added to sell List"));
}else{
$error= $serializer->serialize($bookForm,'json');
return new Response($error,200);
}
}
希望对其他人有帮助。
关于php - 手动提交 Symfony 表单后 CollectionType 字段显示为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36013387/
我开始从事一个用 Symfony 2.8 编写的大型项目。将整个项目升级到 SF 3 需要数百小时,现在还不可能。我想到了一个想法,将 symfony/symfony 包解压到它替换的单个包中(com
我在提交表单后使用 FOSUserEvents,但订阅者调用了两次。 这样我的验证码第一次有效第二次无效 这是我的代码 router = $router; $this->request
我有以下路线: blog_show: path: /test/123 defaults: { _controller: TotalcanBravofillBundle:Te
我是测试新手。我想测试我的功能。我已经成功安装了 phpUnit。我在互联网上查看了许多教程。但我无法获得有关测试的正确信息。这是我的功能代码: public function loginAction
我正在尝试重现 facebook batch requests 的行为在他们的图形 api 上运行。 所以我认为最简单的解决方案是在 Controller 上向我的应用程序发出几个请求,例如: pub
在 Symfony Progress Bar documentation有一个超酷酒吧的示例图像。不幸的是,看起来文档的其余部分没有解释如何获得这样的结果。 这是图片,以防您错过: 我怎么才能得到它?
我使用Finder发送假脱机电子邮件,但是自动名称生成器将点放在文件名中,有时它们出现在文件的开头。 查找程序似乎无法获取具有该名称的文件-那些文件被隐藏了……有人经历过这种行为吗?有什么建议如何使用
我正在尝试进行 LDAP 身份验证,我目前遇到此类错误: ServiceNotFoundException: The service "security.firewall.map.context.ma
有没有办法验证和检查集合数组是否为空。我已经尝试过: /** * @Assert\NotBlank() * @Assert\Length( min = 1) */ protected $work
使用Smyfony2和Doctrin2,可以使用以下示例创建数据固定装置:http://symfony.com/doc/current/bundles/DoctrineFixturesBundle/i
我看到在大多数Symfony 2示例中,例如,不存在记录时,Symfony 2会引发异常。我认为这种方法对最终用户不友好。为什么有人更喜欢引发异常而不在Flashbag上添加一些错误消息? 最佳答案
我对项目中的以下服务有疑问: app.security.guardAuthenticatorLoginPassword: class: AppBundle\Security\LoginPa
symfony缓存和登录Docker容器存在问题。 Web服务器从www-data用户和组执行,当我使用docker上安装的php从docker容器中清除symfony缓存时,它从root执行。 因此
我想了解 symfony 中的服务 我已阅读http://symfony.com/doc/2.3/book/service_container.html#creating-configuring-se
因为我对 Symfony 和 Doctrine 还很陌生,所以我有一个可能很愚蠢的问题;-) 有人可以用简单的词语向我解释集合(尤其是实体中的ArrayCollections)吗?它是什么以及何时以及
我收到了这个表格: {{ form_start(form) }} {{ form_end(form) }} 我想检查用户是否登录,我这样做了: {% if is_g
我的网站已准备好部署,我正在尝试将其设置为在线。 一些信息: 主持人是 OVH。 它不允许 SSH,我必须使用 FTP 发送文件。也没有命令行。 我现在希望能够在子目录中设置网站:/www/test(
过去几个月以来,我一直在尝试与symfony合作。昨晚我自动删除了不需要的存储库。之后,我无法使用symfony命令创建新的symfony项目。当我在终端中运行Symfony new Security
In the environnement variable, then in system variable, I edited the path and added在环境变量中,然后在系统变量
我们有一个 Symfony 1.4 应用程序,想升级到 Symfony 4。是否有可能或者我们必须重新编程该应用程序? 我们询问了我们附近的一家软件公司,他们告诉我们必须重新编写应用程序。 最佳答案
我是一名优秀的程序员,十分优秀!