我对树枝很新,我知道可以在模板中添加值并将它们收集到变量中.但我真正需要的是在总结它们之前在模板中显示夏天的值.我需要像旧symfony中的插槽一样的东西.或者在php中我可以通过ob_start()来做到这一点.不知怎的,它可能在树枝上?
我会喜欢这样的东西.
sum is: {{ sum }} {# obviously it is 0 right here, but i want the value from the calculation #}
{# some content.. #}
{% set sum = 0 %}
{% for r in a.numbers}
{% set sum = sum + r.number %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud) 我正在尝试在Symfony2中实现动态表单.表单很简单,首先用户选择代理,然后根据该代理选择一个帐户.因此,在选择代理之前,帐户将被禁用.
在文本输入的情况下,read_only => true可能是一个解决方案,但select没有属性readonly.所以,我禁用了select,然后尝试使用jQuery启用select:
$('#form select').prop('disabled', false);
Run Code Online (Sandbox Code Playgroud)
问题是在服务器端,Symfony无法处理禁用的表单字段.它的值只是设置为null.
我想到了可能的解决方案.首先,我可以在控制器中手动设置字段值,因为我测试了表单字段值实际上在请求参数中,Symfony只是忽略处理它并将其映射到对象.我认为它会起作用,但这不是一个很好的解决方案.
其次,我可以通过一些客户端黑客禁用选择,或者向表单添加隐藏输入并使用Javascript手动设置它的值.
这里有标准解决方案吗?
我的表单实体看起来像这样:
$builder->add('broker', 'entity', array(
'class' => 'MyBundle:Broker',
'property' => 'name',
'query_builder' => function(BrokerRepository $br) {
return $br->getActiveBrokersQuery();
},
'required' => true,
'empty_value' => 'Choose a broker',
));
$builder->add('accountType', 'entity', array(
'class' => 'MyBundle:AccountType',
'empty_value' => 'Choose a broker first',
'disabled' => true,
));
Run Code Online (Sandbox Code Playgroud)
如果我不禁用选择,一切正常,Symfony完美地处理请求.
几个月前我开始使用symfony,有一件事一直困扰着我.当我在Doctrine中有一对多的关系时,我尝试在数据库中插入一些东西.这是一个例子:
Broker.orm.yml
Acme\DemoBundle\Entity\Broker:
type: entity
table: brokers
repositoryClass: BrokerRepository
id:
id:
type: integer
generator: { strategy: AUTO }
fields:
name:
type: string
length: 255
slug:
type: string
length: 64
oneToMany:
accountTypes:
targetEntity: Acme\DemoBundle\Entity\AccountType
mappedBy: broker
cascade: ["persist"]
Run Code Online (Sandbox Code Playgroud)
AccountType.orm.yml
Acme\DemoBundle\Entity\AccountType:
type: entity
table: account_types
repositoryClass: AccountTypeRepository
id:
id:
type: integer
generator: { strategy: AUTO }
fields:
name:
type: string
length: 255
slug:
type: string
length: 64
manyToOne:
broker:
targetEntity: Acme\DemoBundle\Entity\Broker
inversedBy: accountTypes
joinColumn:
name: broker_id
referencedColumn: id
Run Code Online (Sandbox Code Playgroud)
然后尝试将其保存到数据库中.
$accountType = new …Run Code Online (Sandbox Code Playgroud)