gbenitez/attribute-bundle

此包最新版本(v1.0)没有提供许可证信息。

Symfony Attribute Bundle para Form

安装次数: 3,198

依赖项: 0

建议者: 0

安全: 0

星标: 1

关注者: 4

分叉: 3

公开问题: 0

类型:symfony-bundle

v1.0 2019-03-20 18:34 UTC

This package is auto-updated.

Last update: 2024-09-25 03:09:35 UTC


README

Attributes 文档

添加到 composer.json

"require" : {
   "gbenitez/attribute-bundle": "dev-master"
}

AppKernel.php 中注册 bundles

public function registerBundles()
{
    $bundles = array(
            new gbenitez\Bundle\AttributeBundle\AttributeBundle(),
            new Knp\Bundle\PaginatorBundle\KnpPaginatorBundle(),
        );
    ...
}

app/config/routing.yml 中添加

gbenitez_attribute:
    resource: "@AttributeBundle/Controller/"
    type:     annotation
    prefix:   /admin/attributes

将 bundle 的表添加到数据库中

php app/console doctrine:database:create
php app/console doctrine:schema:update --force

实体属性

实体 AttributeValueTargetEntity

负责与属性和我们的实体之间的关系,并获取选定的或输入的值

Entity/AttributeValueTargetEntity.php

####Entity AttributeValueTargetEntity 示例

<?php

namespace AppBundle\Entity;

use gbenitez\Bundle\AttributeBundle\Entity\AbstractAttributeValue;
use gbenitez\Bundle\AttributeBundle\Entity\Attribute;
use gbenitez\Bundle\AttributeBundle\Model\AttributeTypes;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * AttributeValues
 *
 * @ORM\Table(name="attribute_value_target_entity")
 * @ORM\Entity
 */
class AttributeValueTargetEntity extends AbstractAttributeValue
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\ManyToOne(targetEntity="gbenitez\Bundle\AttributeBundle\Entity\Attribute")
     * @Assert\NotBlank()
     */
    protected $attribute;

    /**
     * @var array
     *
     * @ORM\Column(name="value", type="array", length=255, nullable=true)
     */
    protected $value;

    /**
     * @var string
     *
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\TargetEntity")
     * @Assert\NotBlank()
     */
    private $targetEntityAttribute;

    /**
     * AttributeValueCompany constructor.
     *
     * @param string $attribute
     * @param string $targetEntityAttribute
     */
    public function __construct(Attribute $attribute = null, TargetEntity $targetEntityAttribute = null)
    {
        $this->attribute = $attribute;
        $this->targetEntityAttribute = $targetEntityAttribute;
    }


    public function __toString()
    {
        return  json_encode($this->value);
    }
    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        $this->assertAttributeIsSet();
        return $this->attribute->getName();
    }
    /**
     * {@inheritdoc}
     */
    public function getPresentation()
    {
        $this->assertAttributeIsSet();
        return $this->attribute->getPresentation();
    }
    /**
     * {@inheritdoc}
     */
    public function getType()
    {
        $this->assertAttributeIsSet();
        return $this->attribute->getType();
    }
    /**
     * {@inheritdoc}
     */
    public function getConfiguration()
    {
        $this->assertAttributeIsSet();
        return $this->attribute->getConfiguration();
    }
    /**
     * @throws \BadMethodCallException When attribute is not set
     */
    protected function assertAttributeIsSet()
    {
        if (null === $this->attribute) {
            throw new \BadMethodCallException('The attribute is undefined, so you cannot access proxy methods.');
        }
    }

    /**
     * @param Attribute $attribute
     */
    public function setAttribute(Attribute $attribute)
    {
        $this->attribute = $attribute;
    }
    /**
     * @return string
     * @return \AppBundle\Entity\TargetEntity
     */
    public function getTargetEntityAttribute()
    {
        return $this->targetEntityAttribute;
    }

    /**
     * Get targetEntityAttribute
     *
     * @return \AppBundle\Entity\TargetEntity
     */
    public function setTargetEntityAttribute(\AppBundle\Entity\TargetEntity $targetEntityAttribute)
    {
        $this->targetEntityAttribute = $targetEntityAttribute;
        return $this;
    }

}

TargetEntity 示例

/**
     * @var Assert\Collection
     *
     * @ORM\OneToMany(
     *  targetEntity="AppBundle\Entity\AttributeValueTargetEntity",
     *  mappedBy="targetEntityAttribute",
     *  cascade={"all"},
     *  orphanRemoval=true
     * )
     * @Assert\Valid
     */
    private $attributes;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->attributes = new \Doctrine\Common\Collections\ArrayCollection();
    }
    /**
     * Add attributes
     *
     * @param \AppBundle\Entity\AttributeValueTargetEntity $attributes
     *
     * @return TargetEntity
     */
    public function addAttributes(\AppBundle\Entity\AttributeValueTargetEntity $attributes)
    {
        $this->attributes[] = $attributes;

        $attributes->setTargetEntityAttribute($this);

        return $this;
    }

    /**
     * Remove attributes
     *
     * @param \AppBundle\Entity\AttributeValueTargetEntity $attributes
     */
    public function removeAttributes(\AppBundle\Entity\AttributeValueTargetEntity $attributes)
    {
        $this->attributes->removeElement($attributes);

        $attributes->setTargetEntityAttribute(null);
    }

    /**
     * Get attributes
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getAttributes()
    {
        return $this->attributes;
    }

加载属性的控制器示例

/**
     * @Route("/admin/edit/{id}", name="admin_edit")
     *
     */
    public function editAction(Request $request, TargetEntity $targetEntity)
    {
        if (count($targetEntity->getAttributes()) == 0) {
        //repository del attribute entity
            $attrRepoCompany = $this->get('attribute.repository')->findBy(
                array(
                    'active' => 1
                ),
                array('position' => 'ASC')
            );
            foreach ($attrRepoCompany as $attributeTargetEntity) {
                $targetEntity->addAttributes(new AttributeValueTargetEntity($attributeTargetEntity, $targetEntity));
            }
        }

        $form = $this->createForm(new TargetEntityType(), $targetEntity, array(
            'action' => $request->getRequestUri(),
        ))->handleRequest($request);

        if ($form->isSubmitted() and $form->isValid()) {

            $this->getDoctrine()->getEntityManager()->persist($targetEntity);
            $this->getDoctrine()->getEntityManager()->flush();

            return $this->redirectToRoute('homepage');
        }

        return $this->render('default/index.html.twig' , array('form' => $form->createView()) );
    }

在我们的表单 TargetEntityType()

添加一个属性类型的字段

->add('attributes', 'attributes')

属性加载的 CRUD

admin/attributes/list

配置类型为 choice 的字段示例

choices:
    1: label.yes
    0: label.no
multiple: false
expanded: true
required: true

配置类型为 entity 的字段示例

class: AppBundle\Entity\Country

为了定义与属性所有者之间的关系

  • 在实体关系实现接口

gbenitez\Bundle\AttributeBundle\Entity\AttributeOwnerInterface

  • 添加到 app/config/config.yml 文件中
# app/config/config.yml
doctrine:
    # ...
    orm:
        # ...
        resolve_target_entities:
            gbenitez\Bundle\AttributeBundle\Entity\AttributeOwnerInterface: Acme\AppBundle\Entity\EntidadRelacion

为了定义 targetEntity

# app/config/config.yml
attribute:
    target_entities:
        - 'Entity'
        - 'AnotherEntity'