向 sonata_type_collection 添加查询/过滤器 [英] Add a query/filter to sonata_type_collection

查看:25
本文介绍了向 sonata_type_collection 添加查询/过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体Product",它与ProductVariant"有 OTM 关系.

I have an entity "Product" which has a OTM relation with "ProductVariant".

我希望能够生成一个 Sonata_type_collection,其中只显示带有isMaster = false"的 ProductVariants.

I would like to be able to generate a sonata_type_collection wherein only the ProductVariants with "isMaster = false" are shown.

我无法在 Sonata_type_collection 中使用 query_builder.是否有另一种方法可以操作生成的列表并仍然能够将新的 ProductVariants 插入到 Product 实体中?

I can't use the query_builder in the sonata_type_collection. Is there another way of manipulating the generated list and still be able to insert new ProductVariants into the Product entity?

我的实体:

/**
 * Product
 *
 * @ORM\Table(name="product")
 * @Gedmo\SoftDeleteable(fieldName="deletedAt")
 * @ORM\Entity
 */
class Product
{

    /**
     * @var ArrayCollection $variants
     * @ORM\OneToMany(targetEntity="My\Bundle\ProductVariant", mappedBy="product", cascade={"all"}, orphanRemoval=true)
     */
    private $variants;
}



/**
 * ProductVariant
 *
 * @ORM\Table(name="productVariant")
 * @Gedmo\SoftDeleteable(fieldName="deletedAt")
 * @ORM\Entity
 */
class ProductVariant
{

    /**
     * @var Product
     * @ORM\ManyToOne(targetEntity="My\Bundle\Product", inversedBy="variants")
     */
    private $product;
}

产品管理员:

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->add('variants', 'sonata_type_collection',
            array(
                'label' => 'A nice label',
                'btn_add' => false, // Because adding won't work with the softdeletable variants
                'type_options' => array(
                    'delete' => true,
                )
            ),
            array(
                'edit' => 'inline',
                'inline' => 'table',
                'delete' => 'inline'
            )
        )
    ;
}

推荐答案

您可以为不同类型的 ProductVariant 创建不同的 getter 和 setter.

You could create different getters and setters for the different types of ProductVariants.

我的\捆绑包\产品

public function getNonMasterVariants() {
    return $this->variants->filter(function(My\Bundle\ProductVariant $item) {
        return !$item->isMaster();
    });
}

public function getMasterVariants() {
    return $this->variants->filter(function(My\Bundle\ProductVariant $item) {
        return $item->isMaster();
    });
}

产品管理员

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->add('nonMasterVariants', 'sonata_type_collection',
            array(
                ...
            ),
            array(
                ...
            )
        )
    ;
}

这篇关于向 sonata_type_collection 添加查询/过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆