在Sonata Admin中动态过滤“ CollectionType”表单中的表单列表

时间:2019-03-11 19:55:40

标签: php symfony sonata-admin sonata symfony-sonata

我已经在Sonata Admin中构建了一个ProductAdmin类,该类包含一个引用ProductHasAccessoryAdmin类(包含子产品的产品)的“ CollectionType”表单。看起来像这样:

->add(
            'accessories',
            CollectionType::class,
            array(
                'label' => 'Accessories',
                'type' => AdminType::class,
                'error_bubbling' => true,
                'required' => false
            ),
            array(
                'edit' => 'inline',
                'inline' => 'table',
                'admin_code' => 'admin.product_has_accessory',
                'sortable' => 'sort'
            )
        )

Entity / Product.php:

class Product {
/**
 * @ORM\Id()
 * @ORM\GeneratedValue()
 * @ORM\Column(type="integer")
 */
private $id;

/**
 * @ORM\Column(type="string", length=255)
 */
private $name;

    /**
 * @ORM\OneToMany(targetEntity="App\Entity\ProductHasAccessory", mappedBy="accessory", cascade={"ALL"}, orphanRemoval=true)
 * @Orm\OrderBy({"sort" = "ASC"})
 */
private $accessories;

public function getId(): ?int
{
    return $this->id;
}

public function getName(): ?string
{
    return $this->name;
}

public function setName(string $name): self
{
    $this->name = $name;

    return $this;
}
public function addAccessory(\App\Entity\ProductHasAccessory $accessory)
{
    $accessory->setProduct($this);
    $this->accessories[] = $accessory;

    return $this;
}

public function removeAccessory(\App\Entity\ProductHasAccessory $accessory)
{
    $accessory->setProduct(null);
    $this->accessories->removeElement($accessory);
}

public function getAccessories()
{
    return $this->accessories;
} }

Entity / ProductHasAccessory.php:

class ProductHasAccessory {
/**
 * @ORM\Id()
 * @ORM\GeneratedValue()
 * @ORM\Column(type="integer")
 */ private $id;

/**
 * @var int
 * @ORM\Column(name="sort", type="integer")
 */
private $sort = 0;

/**
 * @ORM\ManyToOne(targetEntity="Product")
 */
private $accessory;

/**
 * @ORM\ManyToOne(targetEntity="Product", inversedBy="accessories")
 * @ORM\JoinColumn(name="product_id", referencedColumnName="id")
 */
private $product;

public function __toString()
{
    return (string)$this->getAccessory();
}

public function getId(): ?int
{
    return $this->id;
}

public function setSort($sort)
{
    $this->sort = $sort;

    return $this;
}

public function getSort()
{
    return $this->sort;
}

public function getAccessory()
{
    return $this->accessory;
}

public function setAccessory(\App\Entity\Product $accessory = null)
{
    $this->accessory = $accessory;

    return $this;
}

public function getProduct()
{
    return $this->product;
}

public function setProduct(\App\Entity\Product $product = null)
{
    $this->product = $product;

    return $this;
}}

问题是,在Sonata面板中,当我添加一些子产品并想要添加另一个子产品时,先前添加的产品再次出现在可用列表中,因此将重复输入。

我想动态过滤CollectionType表单,以使可用的子产品不包含我当前正在编辑的产品。

如何获得这种效果?我尝试在ProductAdmin.php中结合使用以下功能:

public function createQuery($context = 'list')
{
    $query = parent::createQuery($context);

    $id = 1;
    if(in_array($this->getRequest()->query->get('pcode'), $codearray)) {
        $query->andWhere(
            $query->expr()->not($query->expr()->eq($query->getRootAliases()[0] . '.id', ':param'))
        );
        $query->setParameter('param', $id);
    }
    return $query;
}

它可以工作,但是我无法将以前添加的对象的ID和父对象的ID发送给此函数。有人知道怎么做吗?谢谢

0 个答案:

没有答案