在Symfony2中嵌入一个集合类型(Embed a collection type in Symfony2)
试图嵌入一个集合类型,基本上遵循这里的一步一步的方法。
我收到以下错误:
The form's view data is expected to be an instance of class AppBundle\Entity\BenefitGroup, but is an instance of class AppBundle\Entity\BenefitItem. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms an instance of class AppBundle\Entity\BenefitItem to an instance of AppBundle\Entity\BenefitGroup.
只是为了澄清,BenefitItem是父亲,而BenefitGroup是孩子。
我基本上得到了这个错误。
我还没有实现(还)允许你动态添加BenefitGroup元素的部分,我甚至没有尝试持久化对象或删除它(所以我没有实现Doctrine的最后一部分,如例)。
这是我的代码:
BenefitItem实体:
<?php // src/AppBundle/Entity/BenefitItem.php namespace AppBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Doctrine\Common\Collections\ArrayCollection; /** * @ORM\Entity(repositoryClass="AppBundle\Entity\BenefitItemRepository") * @ORM\Table(name="benefit_items") */ class BenefitItem { /** * @ORM\Column(type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @ORM\Column(type="string", length=400) */ protected $comment; public function __construct() { $this->BenefitGroups = new ArrayCollection(); } public function getBenefitGroups() { return $this->BenefitGroups; } /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set comment * * @param string $comment * @return BenefitItem */ public function setComment($comment) { $this->comment = $comment; return $this; } /** * Get comment * * @return string */ public function getComment() { return $this->comment; } }
BenefitGroup实体:
<?php // src/AppBundle/Entity/BenefitGroup.php namespace AppBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity(repositoryClass="AppBundle\Entity\BenefitGroupRepository") * @ORM\Table(name="benefit_groups") */ class BenefitGroup { /** * @ORM\Column(type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @ORM\Column(type="string", length=100) */ protected $name; /** * @ORM\ManyToOne(targetEntity="BenefitItem", cascade={"persist"}) * @ORM\JoinColumn(name="benefitItem_id") */ protected $benefitItem; /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set name * * @param string $name * @return BenefitGroup */ public function setName($name) { $this->name = $name; return $this; } /** * Get name * * @return string */ public function getName() { return $this->name; } /** * Set benefitItem * * @param \AppBundle\Entity\BenefitItem $benefitItem * @return BenefitGroup */ public function setBenefitItem(\AppBundle\Entity\BenefitItem $benefitItem = null) { $this->benefitItem = $benefitItem; return $this; } /** * Get benefitItem * * @return \AppBundle\Entity\BenefitItem */ public function getBenefitItem() { return $this->benefitItem; } }
BenefitItemFormType:
<?php namespace AppBundle\Form\Type; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; class BenefitItemFormType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('comment'); $builder->add('benefitgroups', 'collection', array('type' => new BenefitGroupFormType())); } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'AppBundle\Entity\BenefitGroup', )); } public function getName() { return 'BenefitItem'; } }
BenefitGroupFormType:
<?php namespace AppBundle\Form\Type; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; class BenefitGroupFormType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('name'); } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'AppBundle\Entity\BenefitGroup', )); } public function getName() { return 'BenefitGroup'; } }
控制器:
<?php // AppBundle\Controller\BenefitController.php namespace AppBundle\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use AppBundle\Entity\BenefitItem; use AppBundle\Entity\BenefitGroup; use AppBundle\Form\Type\BenefitItemFormType; use Symfony\Component\HttpFoundation\Request; use Symfony\Bundle\FrameworkBundle\Controller\Controller; class BenefitController extends Controller { /** * @Route("/benefit/show", name="benefit_show") */ public function showAction(Request $request) { $BI = new BenefitItem(); $BG1 = new BenefitGroup(); $BG1->setName = 'Name 1'; $BI->getBenefitGroups()->add($BG1); $BG2 = new BenefitGroup(); $BG2->setName = 'Name 2'; $BI->getBenefitGroups()->add($BG2); $form = $this->createForm(new BenefitItemFormType(), $BI); $form->handleRequest($request); if ($form->isValid()) { // ... maybe do some form processing, like saving the Task and Tag objects } return $this->render('benefit/show.html.twig', array( 'form' => $form->createView(), )); } }
任何想法?
Trying to embed a collection type, basically following the step by step approach here.
I get the following error:
The form's view data is expected to be an instance of class AppBundle\Entity\BenefitGroup, but is an instance of class AppBundle\Entity\BenefitItem. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms an instance of class AppBundle\Entity\BenefitItem to an instance of AppBundle\Entity\BenefitGroup.
Just to clarify, the BenefitItem is the father while the BenefitGroup is the child.
I'm getting this error, basically.
I've not implemented (yet) the part that allows you to add BenefitGroup elements dynamically, and I'm not even trying to persist the object or remove it (so I did not implement yet the last part of the Doctrine as explained in the example).
Here is my code:
The BenefitItem entity:
<?php // src/AppBundle/Entity/BenefitItem.php namespace AppBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Doctrine\Common\Collections\ArrayCollection; /** * @ORM\Entity(repositoryClass="AppBundle\Entity\BenefitItemRepository") * @ORM\Table(name="benefit_items") */ class BenefitItem { /** * @ORM\Column(type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @ORM\Column(type="string", length=400) */ protected $comment; public function __construct() { $this->BenefitGroups = new ArrayCollection(); } public function getBenefitGroups() { return $this->BenefitGroups; } /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set comment * * @param string $comment * @return BenefitItem */ public function setComment($comment) { $this->comment = $comment; return $this; } /** * Get comment * * @return string */ public function getComment() { return $this->comment; } }
The BenefitGroup entity:
<?php // src/AppBundle/Entity/BenefitGroup.php namespace AppBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity(repositoryClass="AppBundle\Entity\BenefitGroupRepository") * @ORM\Table(name="benefit_groups") */ class BenefitGroup { /** * @ORM\Column(type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @ORM\Column(type="string", length=100) */ protected $name; /** * @ORM\ManyToOne(targetEntity="BenefitItem", cascade={"persist"}) * @ORM\JoinColumn(name="benefitItem_id") */ protected $benefitItem; /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set name * * @param string $name * @return BenefitGroup */ public function setName($name) { $this->name = $name; return $this; } /** * Get name * * @return string */ public function getName() { return $this->name; } /** * Set benefitItem * * @param \AppBundle\Entity\BenefitItem $benefitItem * @return BenefitGroup */ public function setBenefitItem(\AppBundle\Entity\BenefitItem $benefitItem = null) { $this->benefitItem = $benefitItem; return $this; } /** * Get benefitItem * * @return \AppBundle\Entity\BenefitItem */ public function getBenefitItem() { return $this->benefitItem; } }
The BenefitItemFormType:
<?php namespace AppBundle\Form\Type; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; class BenefitItemFormType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('comment'); $builder->add('benefitgroups', 'collection', array('type' => new BenefitGroupFormType())); } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'AppBundle\Entity\BenefitGroup', )); } public function getName() { return 'BenefitItem'; } }
The BenefitGroupFormType:
<?php namespace AppBundle\Form\Type; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; class BenefitGroupFormType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('name'); } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'AppBundle\Entity\BenefitGroup', )); } public function getName() { return 'BenefitGroup'; } }
The controller:
<?php // AppBundle\Controller\BenefitController.php namespace AppBundle\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use AppBundle\Entity\BenefitItem; use AppBundle\Entity\BenefitGroup; use AppBundle\Form\Type\BenefitItemFormType; use Symfony\Component\HttpFoundation\Request; use Symfony\Bundle\FrameworkBundle\Controller\Controller; class BenefitController extends Controller { /** * @Route("/benefit/show", name="benefit_show") */ public function showAction(Request $request) { $BI = new BenefitItem(); $BG1 = new BenefitGroup(); $BG1->setName = 'Name 1'; $BI->getBenefitGroups()->add($BG1); $BG2 = new BenefitGroup(); $BG2->setName = 'Name 2'; $BI->getBenefitGroups()->add($BG2); $form = $this->createForm(new BenefitItemFormType(), $BI); $form->handleRequest($request); if ($form->isValid()) { // ... maybe do some form processing, like saving the Task and Tag objects } return $this->render('benefit/show.html.twig', array( 'form' => $form->createView(), )); } }
Any idea?
原文:https://stackoverflow.com/questions/29260595
满意答案
从图像看起来你想要一个支点,我相信你通过做到这一点来实现这一点
SELECT a.userid, u.name, u.profilePic, SUM(activity_weight) total_points, SUM(CASE WHEN activity_typeid=22 THEN activity_weight ELSE 0 END) activity22, SUM(CASE WHEN activity_typeid=33 THEN activity_weight ELSE 0 END) activity33, SUM(CASE WHEN activity_typeid=55 THEN activity_weight ELSE 0 END) activity55 FROM activity_entries a INNER JOIN users1 u ON u.id = a.userid WHERE competitionId = '$competitionId' GROUP BY a.userid ORDER BY totalPoints
编辑注释:以下可能存在一些语法错误,但想法是动态创建sql然后执行它
-- generate sql for pivoted columns SET @sql = NULL; SELECT GROUP_CONCAT(DISTINCT CONCAT( 'SUM(CASE WHEN activity_typeid=',activity_typeid, ' THEN activity_weight ELSE 0 END) AS activity_', activity_typeid, ) ) INTO @sql FROM activity_entries; -- place above in full select query -- n.b. the `$competitionId` could be a problem my MySQL is not v. good SET @sql = CONCAT('SELECT a.userid, u.name, u.profilePic, SUM(activity_weight) total_points,', @sql, 'FROM activity_entries JOIN users1 u ON u.id = a.userid WHERE competitionId = ''$competitionId'' GROUP BY a.userid, u.name, u.profilePic ORDER BY totalPoints'); -- execute the dynamic sql PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
From the image it looks like you want a pivot, I believe in MySQL you achieve this by doing
SELECT a.userid, u.name, u.profilePic, SUM(activity_weight) total_points, SUM(CASE WHEN activity_typeid=22 THEN activity_weight ELSE 0 END) activity22, SUM(CASE WHEN activity_typeid=33 THEN activity_weight ELSE 0 END) activity33, SUM(CASE WHEN activity_typeid=55 THEN activity_weight ELSE 0 END) activity55 FROM activity_entries a INNER JOIN users1 u ON u.id = a.userid WHERE competitionId = '$competitionId' GROUP BY a.userid ORDER BY totalPoints
Edit for comments: there may be some syntax errors in the following but the idea is to create the sql dynamically and then execute it
-- generate sql for pivoted columns SET @sql = NULL; SELECT GROUP_CONCAT(DISTINCT CONCAT( 'SUM(CASE WHEN activity_typeid=',activity_typeid, ' THEN activity_weight ELSE 0 END) AS activity_', activity_typeid, ) ) INTO @sql FROM activity_entries; -- place above in full select query -- n.b. the `$competitionId` could be a problem my MySQL is not v. good SET @sql = CONCAT('SELECT a.userid, u.name, u.profilePic, SUM(activity_weight) total_points,', @sql, 'FROM activity_entries JOIN users1 u ON u.id = a.userid WHERE competitionId = ''$competitionId'' GROUP BY a.userid, u.name, u.profilePic ORDER BY totalPoints'); -- execute the dynamic sql PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
相关问答
更多SQLite选择哪里空?(SQLite select where empty?)
我的SQL Fiddle查询出了什么问题?(What's wrong with my SQL Fiddle query?)
如何对sum的数据帧进行采样,其中sum与列总和部分匹配(How to sample dataframe where sum matches partially with column sum)
允许参数化SQL查询的哪些部分?(What parts of a SQL query are allowed to be parameterized?)
在MYSQL中使用WHERE时,我可以使用多个参数吗?(Can I use multiple parameters when using WHERE in MYSQL)
在DB2 sql for z / os中使用0填充smallint(pad smallint with 0s in DB2 sql for z/os)
SUM(some_column)WHERE some_column匹配(SUM (some_column) WHERE some_column matches)
dropColumn布尔变量时,liquibase迁移失败(liquibase migration failed while dropColumn boolean variable)
我应该在使用字符串替换进行更新时添加WHERE子句(Should I add a WHERE clause when updating with string replacements)
根据多列匹配(一些部分)总结一列(Sum up a column based on multiple column matches (some partial))
相关文章
更多Symfony2网站开发
ServletOutputStream cannot be resolved to a type
Hibernate 异常之:associate a collection with two ...
用好Collection 对solrj入库进行优化
英语谚语精选(English Proverb Collection)
Solr参数(Analysis Collection Common CoreAdmin)
CLR Via C# 3rd 阅读摘要 -- Chapter 21 – Automatic Memory Management (Garbage Collection)
maven项目使用SOLR时报 previously initiated loading for a different type with name "javax/servlet/http/HttpServletRequest" 错的解决方法
Spring Data: a new perspective of data operations
最新问答
更多获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
如何通过引用返回对象?(How is returning an object by reference possible?)
矩阵如何存储在内存中?(How are matrices stored in memory?)
每个请求的Java新会话?(Java New Session For Each Request?)
css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
xcode语法颜色编码解释?(xcode syntax color coding explained?)
在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!