相关疑难解决方法(0)

Symfony2实体字段类型替代"property"或"__toString()"?

使用Symfony2 实体字段类型应指定property选项:

$builder->add('customers', 'entity', array(
    'multiple' => true,
    'class'    => 'AcmeHelloBundle:Customer',
    'property' => 'first',
));
Run Code Online (Sandbox Code Playgroud)

但有时这还不够:想想两个同名的客户,所以显示电子邮件(唯一)是强制性的.

另一种可能性是__toString()在模型中实现:

class Customer
{
    public $first, $last, $email;

    public function __toString()
    {
        return sprintf('%s %s (%s)', $this->first, $this->last, $this->email);
    }
}
Run Code Online (Sandbox Code Playgroud)

后者的缺点是你被迫在所有形式中以相同的方式显示实体.

有没有其他方法可以使这更灵活?我的意思是像回调函数:

$builder->add('customers', 'entity', array(
    'multiple' => true,
    'class'    => 'AcmeHelloBundle:Customer',
    'property' => function($data) {
         return sprintf('%s %s (%s)', $data->first, $data->last, $data->email);
     },
));
Run Code Online (Sandbox Code Playgroud)

forms symfony-forms symfony

18
推荐指数
1
解决办法
1万
查看次数

Symfony 2创建一个包含2个属性的实体表单字段

我正在使用symfony2并有一个表单来保存一个用户与某些规则的关系.这些规则由公司的管理员用户设置.在这种形式中,在我选择要更新的用户之后,我必须选择该用户具有权限的规则.

问题是我可能有多个具有相同名称的规则(它是另一个实体),但值不同.所以,当我构建selectbox时,我必须显示名称和值,如:

  1. 物品数量 - 10
  2. 物品数量 - 20
  3. 该项目的价值 - 200
  4. 该项目的价值 - 500

但现在我只能使用下面的代码显示" - $ value":

$form = $this->createFormBuilder()->add('myinput', 'entity', array(
                    'class' => 'myBundle:Rule',
                    'property' => 'childEntity.name',
                    'label' => 'Filas Permitidas',
                    'expanded' => false,
                    'multiple' => true,
                    'choices' => $this->getDoctrine()
                            ->getRepository('MyBundle:Rule')
                            ->findAll(),
                    'required' => true,
                ))->getForm();
Run Code Online (Sandbox Code Playgroud)

所以,作为我想要的财产$myEntity->getChildEntity()->getName()$myEntity->getValue().

有办法做到这一点吗?

php symfony-forms symfony

17
推荐指数
1
解决办法
1万
查看次数

Symfony2 - Twig渲染控制器并返回数组响应

注意:我在这里做的是嵌入控制器 <---请参阅类似(官方)示例的链接.

我想从一个twig模板调用一个控制器,并让该控制器返回一个数组,然后我可以在我的模板的其余部分使用该数组.

我可以用个别变量做到这一点:

枝条

{% set testVar = render(controller('AppBundle:Test:index')) %}
Run Code Online (Sandbox Code Playgroud)

调节器

class TestController extends Controller
{
    public function testAction()
    {
        return new Response('OH HAI');
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,以下引发异常:("The Response content must be a string or object implementing __toString(), "array" given.") 使用相同的twig文件.

public function testAction()
{
    return new Response(array('test' => 1, 'foo' => 'bar'));
}
Run Code Online (Sandbox Code Playgroud)

这引发了上述异常.如何在不创建虚拟的,无用的额外模板的情况下完成我所寻求的控制器渲染

php symfony twig

5
推荐指数
1
解决办法
2万
查看次数

标签 统计

symfony ×3

php ×2

symfony-forms ×2

forms ×1

twig ×1