在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?)[2023-10-26]
有几种方法,如: where some_column is null or some_column = '' 要么 where ifnull(some_column, '') = '' 要么 where coalesce(some_column, '') = '' 的 where ifnull(length(some_column), 0) = 0 There are several ways, like: where some_column is null or some_column = '' ... -
对于那些对SQL Fiddle我一直在处理的一些神秘问题感兴趣的人的一些背景知识: 禁用JDBC中的显式提交,在SQL中检测它们,或将数据库置于只读状态 (dba.se) 从本质上讲,我试图确保小提琴始终保持一致状态,即使人们玩它们也是如此。 我担心的一件事是人们故意弄乱数据库,打破了可能与他们一起工作的其他人(这种情况以前发生过,但通常不幸见)。 我已经找到了为每个数据库平台保持清洁的方法,但有趣的是每个平台的每个方法都完全不同。 不幸的是,对于MySQL,我不得不求助于最糟糕的选择 - 只允许在右侧使用 ...
-
如何对sum的数据帧进行采样,其中sum与列总和部分匹配(How to sample dataframe where sum matches partially with column sum)[2022-07-18]
像这样的东西? id = c(123,234,345,124,111,116) sum = c(12,12,50,23,20,20) df = data.frame("id" = id, "sum"=sum) df[cumsum(df$sum) <110,] # id sum #1 123 12 #2 234 12 #3 345 50 #4 124 23 Something like this? id = c(123,234,345,124,111,116) sum = c(12,12,50, ... -
标识符和关键字不可参数化。 这包括: 列名称 表名 架构名称 数据库名称 运营商 功能名称 关键词 基本思想是编译查询,编译版本有参数。 缺少任何上述元素时,无法编译查询。 Identifiers and keywords are not parameterizable. This includes: Columns names Table names Schema names Database names Operators Function names Keywords The basic idea i ...
-
是的,只需将它们与AND分开: WHERE some_column=some_value AND other_column=other_value Yes, just separate them with ANDs: WHERE some_column=some_value AND other_column=other_value
-
在所有当前支持的DB2 for z / OS版本中,都有一个功能DIGITS()可以完全满足您的需求。 In all currently supported versions of DB2 for z/OS there's a function DIGITS() that does exactly what you need.
-
从图像看起来你想要一个支点,我相信你通过做到这一点来实现这一点 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 ...
-
dropColumn布尔变量时,liquibase迁移失败(liquibase migration failed while dropColumn boolean variable)[2023-07-04]
SOME_TABLE通过从其中删除some_column来修改SOME_TABLE 然后在同一个changeSet中,我修改了VIEW表,它具有旧的some_column值,因为它不再需要 我得到异常的原因是因为当从表中删除列时,查看表仍然持有它并且不让它掉落 我如何解决它: 首先通过删除some_column变量来修改视图表 然后从SOME_TABLE删除some_column The changeSet consisted of modifying the SOME_TABLE by dropping ... -
我应该在使用字符串替换进行更新时添加WHERE子句(Should I add a WHERE clause when updating with string replacements)[2023-04-03]
如果在表上定义了BEFORE / AFTER UPDATE触发器,则查询的不同之处在于是否为表中的所有行触发了触发器,或者仅触发了WHERE子句中满足谓词的行。 否则,在MySQL中,这两个查询是等价的。 如果分配给列的值与列中已有的值相同,则MySQL不会将行计数(或报告)为受UPDATE“影响”。 (其他关系数据库会对“受影响”计数中的行进行计数。 由于LIKE比较中的前导百分号,需要对表中的每一行评估该条件,因此性能不会有任何差异。 如果some_records(some_column)上有索引,My ... -
拯救! $ awk '{a[$2 FS substr($5,1,4)]+=$1} END {for(k in a) print a[k],k}' file | sort -k3n -k2,2M 16180725217 May 2016 20543202693 May 2017 awk to the rescue! $ awk '{a[$2 FS substr($5,1,4)]+=$1} END {for(k in a) print a[k],k}' file | sort ...