rapemer/sonata-admin-annotation-bundle

通过注解管理 Sonata 表单、数据、列表和 ShowMapper

安装: 29

依赖: 0

建议者: 0

安全: 0

星标: 1

关注者: 2

分支: 0

类型:symfony-bundle

1.0.0 2019-12-20 16:47 UTC

This package is auto-updated.

Last update: 2024-09-21 03:45:10 UTC


README

通过注解管理 Sonata 表单、数据、列表和 ShowMapper

已知问题

版本 1.1.*

  • 使用 oneToMany/manyToOne 关系会产生一个 "实体未管理" 的 doctrine 错误。原因是 FormMapper-Annotation 默认将 by_reference 设置为 false,使用 @FormMapper(options={"by_reference"=true}) 将解决这个问题。

新功能

版本 1.2

  • 允许使用 @Order/FormReorder, @Order/ShowReorder, @Order/ListReorder 或 @Order/ShowAndFormReorder 注解重新排序 FormMapper/ShowMapper 和 ListMapper

版本 1.1

  • 在 FormMapper 和 ShowMapper 注解中引入了新的 "with" 和 "withOptions" 以进行分组 -> @FormMapper(with="Main")
  • 允许使用 @FormCallback 在实体方法上配置静态回调方法(请参阅 @FormCallback 示例)

如何安装

将 Bundle 添加到您的 composer.json 中

// composer.json

{
    "require": {
        "ibrows/sonata-admin-annotation-bundle": "*"
    }
}

使用 composer.phar 从控制台安装该 Bundle

$ php composer.phar update ibrows/sonata-admin-annotation-bundle

在 AppKernel.php 中启用该 Bundle

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Ibrows\Bundle\SonataAdminAnnotationBundle\IbrowsSonataAdminAnnotationBundle(),
    );
}

注解

  • Ibrows\Bundle\SonataAdminAnnotationBundle\Annotation\Order 用于类上的全局排序,例如 "显示所有属性"
  • Ibrows\Bundle\SonataAdminAnnotationBundle\Annotation 用于特定配置和排除(如果使用排序)的属性

如果发现任何 FormMapperExclude 注解在属性上,则读取器假定类上存在 Order/FormMapperAll(对其他注解 - List/Form/Datagrid 同样适用)

查看注解了解它们接受的选项

示例

<?php

namespace YourApp\Entity;

use Ibrows\Bundle\SonataAdminAnnotationBundle\Annotation as Sonata;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Entity
 * @Sonata\Order\ListMapperAll
 * @Sonata\Order\ShowMapperAll
 * @Sonata\Order\FormMapperAll
 *
 * @Sonata\Order\ShowAndFormreorder(with="General", keys={"name"})
 * @Sonata\Order\ShowAndFormreorder(keys={"taxRate"})
 */
class Country
{
    /**
     * @var integer $id
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @Sonata\ListMapper(identifier=true)
     * @Sonata\Order\FormMapperExclude
     * @Sonata\ShowMapper(with="General")
     */
    protected $id;

    /**
     * @var string $name
     * @ORM\Column(type="string")
     * @Sonata\ShowMapper(with="General")
     */
    protected $name;

    /**
     * @var float
     * @ORM\Column(type="float", name="shipping_free_limit")
     */
    protected $shippingFreeLimit;

    /**
     * @var float
     * @ORM\Column(type="float", name="shipping_fixed_rate")
     */
    protected $shippingFixedRate;

    /**
     * @var float
     * @ORM\Column(type="float", name="tax_rate")
     */
    protected $taxRate;

    /**
     * @ORM\ManyToMany(targetEntity="Article", inversedBy="countries")
     * @ORM\JoinTable(name="article_country")
     * @Sonata\Order\ListMapperExclude
     * @Sonata\FormMapper(options={"required"=false})
     **/
    protected $articles;
}
<?php

namespace YourApp\Entity;

use Ibrows\Bundle\SonataAdminAnnotationBundle\Annotation as Sonata;

use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Show\ShowMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;

use Application\Sonata\MediaBundle\Entity\Gallery;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Entity
 * @Sonata\Order\ShowAndFormreorder(keys={"name"})
 */
class Article
{
    /**
     * @var string $number
     * @ORM\Column(name="number", type="string", unique=true)
     */
    protected $number;

    /**
     * @var string $name
     * @ORM\Column(type="string")
     * @Sonata\DatagridMapper
     */
    protected $name;

    /**
     * @var string $description
     * @ORM\Column(type="text")
     * @Sonata\Order\ListMapperExclude
     */
    protected $description;

    /**
     * @var string $matchCode
     * @ORM\Column(name="match_code", type="string")
     * @Sonata\Order\ListMapperExclude
     */
    protected $matchCode;

    /**
     * @var string $articleGroup
     * @ORM\Column(name="article_group", type="string")
     * @Sonata\Order\ListMapperExclude
     * @Sonata\Order\ShowMapperExclude
     */
    protected $articleGroup;

    /**
     * @var ArrayCollection
     * @ORM\OneToMany(targetEntity="ArticlePrice", mappedBy="articleEntity")
     * @Sonata\Order\ListMapperExclude
     */
    protected $prices;

    /**
     * @ORM\ManyToMany(targetEntity="Country", mappedBy="articles")
     **/
    protected $countries;

    /**
     * @var Gallery $pictures
     * @ORM\ManyToOne(
     *      targetEntity="Application\Sonata\MediaBundle\Entity\Gallery",
     *      cascade={"persist"}
     * )
     * @Sonata\FormMapper(
     *      type="sonata_type_model_list",
     *      options={"required"=false},
     *      fieldDescriptionOptions={
     *          "link_parameters"={
     *              "context":"article",
     *              "provider":"sonata.media.provider.image"
     *          }
     *      }
     * )
     */
    protected $pictures;

    /**
     * @param \Sonata\AdminBundle\Form\FormMapper $formMapper
     * @Sonata\FormCallback
     */
    public static function configureFormFields(FormMapper $formMapper)
    {
        $formMapper->add('countries');
    }
}

在管理中(可能在 AbstractAdmin.php 中)使用

使用 AbstractSonataAdminAnnotationAdmin

<?php

namespace YourApp\Admin;

use Ibrows\Bundle\SonataAdminAnnotationBundle\Admin\AbstractSonataAdminAnnotationAdmin;

abstract class AbstractAdmin extends AbstractSonataAdminAnnotationAdmin
{

}

使用 Traits(PHP >=5.4)

<?php

namespace YourApp\Admin;

use Ibrows\Bundle\SonataAdminAnnotationBundle\Admin\SonataAdminAnnotationAllTrait;

use Sonata\AdminBundle\Admin\Admin;

abstract class AbstractAdmin extends Admin
{
    use SonataAdminAnnotationAllTrait;
}

自己的实现

<?php

namespace YourApp\Admin;

use Ibrows\Bundle\SonataAdminAnnotationBundle\Reader\SonataAdminAnnotationReaderInterface;

use Symfony\Component\DependencyInjection\ContainerInterface;

use Sonata\AdminBundle\Admin\Admin;

use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Show\ShowMapper;

abstract class AbstractAdmin extends Admin
{
    /**
     * @param ListMapper $listMapper
     */
    protected function configureListFields(ListMapper $listMapper)
    {
        $this->getSonataAnnotationReader()->configureListFields($this->getClass(), $listMapper);
    }

    /**
     * @param FormMapper $formMapper
     */
    protected function configureFormFields(FormMapper $formMapper)
    {
        $this->getSonataAnnotationReader()->configureFormFields($this->getClass(), $formMapper);
    }

    /**
     * @param ShowMapper $showMapper
     */
    protected function configureShowFields(ShowMapper $showMapper)
    {
        $this->getSonataAnnotationReader()->configureShowFields($this->getClass(), $showMapper);
    }

    /**
     * @param DatagridMapper $datagridMapper
     */
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
        $this->getSonataAnnotationReader()->configureDatagridFilters($this->getClass(), $datagridMapper);
    }

    /**
     * @return ContainerInterface
     */
    protected function getContainer()
    {
        return $this->getConfigurationPool()->getContainer();
    }

    /**
     * @return SonataAdminAnnotationReaderInterface
     */
    protected function getSonataAnnotationReader()
    {
        return $this->getContainer()->get('ibrows_sonataannotation.reader');
    }
}