我在处理我的表单时遇到了一些问题。
我的表单布局如下:
用户(FOSUserBundle)
成员(自定义类)
地区(自定义类)
成员(member)表单是用户表单的子表单,区域表单是成员(member)表单的子表单。
首先,我得到的错误是:
Neither the property "region" nor one of the methods "setRegion()", "_set()" or "_call()" exist and have public access in class "Netsite\Bundle\AdminBundle\Entity\Member".
当我提交表单时会发生这种情况:
$form->handleRequest($request);
这里是所有相关代码:
成员.orm.yml
Netsite\Bundle\AdminBundle\Entity\Member:
type: entity
table: member
id:
id:
type: integer
generator:
strategy: AUTO
fields:
security_question:
type: string
length: 255
security_answer:
type: string
length: 255
----- SNIP -----
manyToMany:
region:
targetEntity: Region
cascade: ["persist", "remove"]
实体\成员.php
<?php
namespace Netsite\Bundle\AdminBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Member
*/
class Member
{
/**
* @var \Doctrine\Common\Collections\Collection
*/
private $region;
/**
* Constructor
*/
public function __construct()
{
$this->region = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add region
*
* @param \Netsite\Bundle\AdminBundle\Entity\Region $region
* @return Member
*/
public function addRegion(\Netsite\Bundle\AdminBundle\Entity\Region $region)
{
$this->region[] = $region;
return $this;
}
/**
* Remove region
*
* @param \Netsite\Bundle\AdminBundle\Entity\Region $region
*/
public function removeRegion(\Netsite\Bundle\AdminBundle\Entity\Region $region)
{
$this->region->removeElement($region);
}
/**
* Get region
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getRegion()
{
return $this->region;
}
Form\MemberType.php
<?php
namespace Netsite\Bundle\AdminBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Validator\Constraints\NotBlank;
class MemberType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('region', new RegionType())
区域.orm.yml
Netsite\Bundle\AdminBundle\Entity\Region:
type: entity
table: region
id:
id:
type: integer
generator:
strategy: AUTO
fields:
region:
type: string
length: 255
实体\区域.php
<?php
namespace Netsite\Bundle\AdminBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Region
*/
class Region
{
/**
* @var integer
*/
private $id;
/**
* @var string
*/
private $region;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set region
*
* @param string $region
* @return Region
*/
public function setRegion($region)
{
$this->region = $region;
return $this;
}
/**
* Get region
*
* @return string
*/
public function getRegion()
{
return $this->region;
}
}
Form\RegionType.php
<?php
namespace Netsite\Bundle\AdminBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class RegionType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('region', 'entity', array(
'class' => 'Netsite\Bundle\AdminBundle\Entity\Region',
'property' => 'region',
'empty_data' => null,
'multiple' => true,
'expanded' => true,
'required' => false,
'label' => false
))
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array(
'data_class' => 'Netsite\Bundle\AdminBundle\Entity\Region',
'cascade_validation' => true
));
}
public function getName() {
return 'RegionType';
}
}
我不知道我做错了什么。
最佳答案
我想你想在 MemberType.php 中替换:
$builder->add('region', new RegionType())
与
$builder->add('region', 'entity',
array(
'class' => 'YourBundle:Region',
'multiple' => true,
'expanded' => true,
'required' => true|false,
'property' => 'region',
'label' => 'your.label',
'error_bubbling' => true,
)
)
这将渲染一个带有标记为 Region::region 的复选框的区域(顺便说一句,令人困惑的命名方案。应该只是“名称”)。多个复选标记将反射(reflect)在多个 M2M 条目中。
关于php - Symfony2.3 多对多形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18720922/
我们有一个目前在Rails2.3.12版和Ruby1.8.7版上运行的应用程序。我们想将我们的应用程序更新到Rails4.0和Ruby2.1.0。我们有大约200个模型和150个Controller。我想知道升级过程需要多大的努力。您还可以提供升级可以遵循的步骤。我们应该先升级Ruby然后再升级Rails还是相反? 最佳答案 您想要实现的目标将是史诗般的努力。我无法为您提供分步说明,因为不可能在一个答案中涵盖所有情况。我建议不要同时升级Ruby和Rails,而是分步升级。升级本身的复杂性是巨大的,但只要您的应用程序具有合理的测试覆盖
我正在尝试让Rails在Windows10上运行。我正在使用Ruby2.3.0和Rails4.2.6,并且暂时使用Nokogiri1.6.3。当我尝试运行railsnewdemo时,它返回错误:Anerroroccurredwhileinstallingnokogiri(1.6.7.2),andBundlercannotcontinue.Makesurethat`geminstallnokogiri-v'1.6.7.2'`succeedsbeforebundling.当我运行geminstallnokogiri-v'1.6.7.2时,我得到:ERROR:Errorinstallingn
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭9年前。我来自C、php和bash背景,很容易学习,因为它们都有相同的C结构,我可以将其与我已经知道的联系起来。然后2年前我学了Python并且学得很好,Python对我来说比Ruby更容易学。然后从去年开始,我一直在尝试学习Ruby,然后是Rails,我承认,直到现在我还是学不会,讽刺的是那些打着简单易学的烙印,但是对于我这样一个老练的程序员来说,我只是无法将它
我在新的Rails应用程序(3.2.3)中运行迁移时遇到了问题。我们正在使用postrgres9.1.3和-pg(0.13.2)-当我运行rakedb:create,然后运行rakedb:migrate,我得到->1.9.3-p194(master)rakedb:migrate--trace**Invokedb:migrate(first_time)**Invokeenvironment(first_time)**Executeenvironmentrakeaborted!PG::Error:ERROR:relation"roles"doesnotexistLINE4:WHEREa
我有以下模型用户has_many:users_contactshas_many:contacts,through::users_contactsaccepts_nested_attributes_for:contacts,allow_destroy:true联系方式has_many:users_contactshas_many:users,through::users_contactsaccepts_nested_attributes_for:users_contacts,allow_destroy:true用户联系belongs_to:usersbelongs_to:contacts
通过rvm升级到Ruby2.3的最佳方法是什么,同时保持所有gem安装在以前的版本上(例如json、nokogiri、等等)? 最佳答案 编辑这个问题在这里有答案:RVM:Howtousegemsfromadifferentruby?$rvmgemsetcopy$oldversion2.3.0##Assignorreplace$oldversionwitholdversionname原创在安装Ruby2.3之前,使用gemlist获取已安装的gem及其版本的列表。然后,在安装Ruby2.3之后,使用rvm将2.3设置为新的默认值:$
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭10年前。我使用PHP的时间太长了,对它感到厌倦了。我也想学习一门新语言。我一直在使用Ruby并且喜欢它。我必须在Rails和Sinatra之间做出选择,那么您会推荐哪一个?Sinatra真的不能用来构建复杂的应用程序,它只能用于简单的应用程序吗?
我正在使用DataMapper,一个用于ruby的开源ORM,我很想抓挠。目前,DataMapper可以对一对多关系使用StrategicEagerLoading(SEL),但不能对发生N+1查询的多对多关系使用。我想设法让这项工作正常进行,但我找不到在哪里做。所以两部分问题:如何运行测试套件,以便它显示失败(注意,现在所有应该失败的规范都标记为待定)?在何处以及如何为一对多关系实现SEL? 最佳答案 对于第二个问题,您可以尝试深入研究代码:/lib/dm-core/associations/relationship.rb#Ea
我在让asseticsass过滤器与node-sass而不是ruby替代品一起工作时遇到了一些困难。我的config.yml文件中有以下配置:assetic:debug:"%kernel.debug%"use_controller:falsebundles:[]write-to:"%kernel.root_dir%/../web/assets"read_from:"%kernel.root_dir%/../web/assets"node:"%%PROGRAMFILES%%\nodejs\\node.exe"node_paths:["%%USERPROFILE%%\\AppData\
我很确定Ruby有这些(等同于__call、__get和__set),否则find_by将如何在Rails中工作?也许有人可以举一个简单的例子来说明如何定义与find_by相同的方法?谢谢 最佳答案 简而言之你可以映射__调用带有参数的method_missing调用__设置为方法名称以'='结尾的method_missing调用__获取不带任何参数的method_missing调用__调用PHPclassMethodTest{publicfunction__call($name,$arguments){echo"Callingob