2017 © Pedro Peláez
 

symfony-bundle sonata-admin-annotation-bundle

Manage Sonata Form, Data, List and ShowMapper over annotations

image

ibrows/sonata-admin-annotation-bundle

Manage Sonata Form, Data, List and ShowMapper over annotations

  • Wednesday, October 5, 2016
  • by ibrows
  • Repository
  • 14 Watchers
  • 10 Stars
  • 16,395 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 15 Versions
  • 2 % Grown

The README.md

IbrowsSonataAdminAnnotationBundle

Manage Sonata Form, Data, List and ShowMapper over annotations, (*1)

Latest Stable Version Total Downloads, (*2)

knpbundles.com, (*3)

How to install

Add Bundle to your composer.json

// composer.json

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

Install the bundle from console with composer.phar

``` bash $ php composer.phar update ibrows/sonata-admin-annotation-bundle, (*4)


### Enable the bundle in AppKernel.php - Dont forget to give AppKernel to the Bundle and register the Bundle *BEFORE* SonataAdminBundle ``` php <?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new Ibrows\Bundle\SonataAdminAnnotationBundle\IbrowsSonataAdminAnnotationBundle($this), ); }

Configuration (Only needed if @AutoService is used)

``` yaml ibrows_sonata_admin_annotation: autoservice: service_id_prefix: companyname.admin default_entity: admin: CompanyName\ProjectNameBundle\Admin\DefaultAdmin controller: CompanyNameProjectNameBundle:Admin/DefaultAdmin entities: - {directory: %kernel.root_dir%/../src/CompanyName/ProjectNameBundle/Entity, prefix: CompanyName\ProjectNameBundle\Entity}, (*5)


### The Annotations - Ibrows\Bundle\SonataAdminAnnotationBundle\Annotation\Order on classes for global orders like "show me all properties" - Ibrows\Bundle\SonataAdminAnnotationBundle\Annotation on properties for specific configurations and excludes if orders are used > If any FormMapperExclude Annotation is found on a property the reader assumes that there es an Order/FormMapperAll on the class (same goes for the other annotations - List/Form/Datagrid) Have a look on the Annotations to see what options they accept ### Example ``` php <?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"}) * * @Sonata\AutoService() */ 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 <?php, (*6)

namespace YourApp\Entity;, (*7)

use Ibrows\Bundle\SonataAdminAnnotationBundle\Annotation as Sonata;, (*8)

use Sonata\AdminBundle\Form\FormMapper; use Sonata\AdminBundle\Show\ShowMapper; use Sonata\AdminBundle\Datagrid\DatagridMapper; use Sonata\AdminBundle\Datagrid\ListMapper;, (*9)

use Application\Sonata\MediaBundle\Entity\Gallery;, (*10)

use Doctrine\ORM\Mapping as ORM; use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\ArrayCollection;, (*11)

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

/**
 * @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');
}

}, (*13)


### Using in the Admin (maybe in an AbstractAdmin.php) #### With the AbstractSonataAdminAnnotationAdmin ```php <?php namespace YourApp\Admin; use Ibrows\Bundle\SonataAdminAnnotationBundle\Admin\AbstractSonataAdminAnnotationAdmin; abstract class AbstractAdmin extends AbstractSonataAdminAnnotationAdmin { }

With 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;
}

Own implementation

``` php <?php, (*14)

namespace YourApp\Admin;, (*15)

use Ibrows\Bundle\SonataAdminAnnotationBundle\Reader\SonataAdminAnnotationReaderInterface;, (*16)

use Symfony\Component\DependencyInjection\ContainerInterface;, (*17)

use Sonata\AdminBundle\Admin\Admin;, (*18)

use Sonata\AdminBundle\Datagrid\ListMapper; use Sonata\AdminBundle\Datagrid\DatagridMapper; use Sonata\AdminBundle\Form\FormMapper; use Sonata\AdminBundle\Show\ShowMapper;, (*19)

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

/**
 * @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');
}

} ```, (*21)

Known issues

Version 2.2.*

  • When using the @AutoService Annotation you have to clear the cache everytime you make changes on this Annotation (remove/add/edit). Reason is that the appDevDebugContainer is fully cached and the CompilerPass cannot register the new changes.

Version 1.1.*

  • Using oneToMany/manyToOne relations produces an "entity not managed" doctrine error. Reason is that FormMapper-Annotation per default sets by_reference to false, using @FormMapper(options={"by_reference"=true}) will fix that.

New features

Version 2.4

  • New "tab" and "tabOptions" in FormMapper and ShowMapper Annotation for grouping -> @FormMapper(tab="Main")

Version 2.2

  • Allow to register SonataAdminServices over Annotations with @AutoService @see Known issues for caching problems

Version 1.2

  • Allow reorder of FormMapper/ShowMapper and ListMapper with @Order/FormReorder, @Order/ShowReorder, @Order/ListReorder or @Order/ShowAndFormReorder annotations

Version 1.1

  • New "with" and "withOptions" in FormMapper and ShowMapper Annotation for grouping -> @FormMapper(with="Main")
  • Allow configuration of static callback methods in entity with @FormCallback on Method (see @FormCallback example)

The Versions

05/10 2016

dev-master

9999999-dev

Manage Sonata Form, Data, List and ShowMapper over annotations

  Sources   Download

MIT

The Requires

 

sonata annotation ibrows

28/04 2015

2.4.1

2.4.1.0

Manage Sonata Form, Data, List and ShowMapper over annotations

  Sources   Download

MIT

The Requires

 

sonata annotation ibrows

22/04 2015

2.4.0

2.4.0.0

Manage Sonata Form, Data, List and ShowMapper over annotations

  Sources   Download

MIT

The Requires

 

sonata annotation ibrows

05/11 2014

2.3.0

2.3.0.0

Manage Sonata Form, Data, List and ShowMapper over annotations

  Sources   Download

MIT

The Requires

 

sonata annotation ibrows

30/10 2014

2.2.2

2.2.2.0

Manage Sonata Form, Data, List and ShowMapper over annotations

  Sources   Download

MIT

The Requires

 

sonata annotation ibrows

30/10 2014

2.2.1

2.2.1.0

Manage Sonata Form, Data, List and ShowMapper over annotations

  Sources   Download

MIT

The Requires

 

sonata annotation ibrows

21/10 2014

2.2.0

2.2.0.0

Manage Sonata Form, Data, List and ShowMapper over annotations

  Sources   Download

MIT

The Requires

 

sonata annotation ibrows

19/09 2014

2.1.1

2.1.1.0

Manage Sonata Form, Data, List and ShowMapper over annotations

  Sources   Download

MIT

The Requires

 

sonata annotation ibrows

19/09 2014

2.1.0

2.1.0.0

Manage Sonata Form, Data, List and ShowMapper over annotations

  Sources   Download

MIT

The Requires

 

sonata annotation ibrows

09/08 2013

2.0.0

2.0.0.0

Manage Sonata Form, Data, List and ShowMapper over annotations

  Sources   Download

MIT

The Requires

 

sonata annotation ibrows

20/02 2013

1.2.0

1.2.0.0

Manage Sonata Form, Data, List and ShowMapper over annotations

  Sources   Download

MIT

The Requires

 

sonata annotation ibrows

20/02 2013

1.1.3

1.1.3.0

Manage Sonata Form, Data, List and ShowMapper over annotations

  Sources   Download

MIT

The Requires

 

sonata annotation ibrows

20/02 2013

1.1.2

1.1.2.0

Manage Sonata Form, Data, List and ShowMapper over annotations

  Sources   Download

MIT

The Requires

 

sonata annotation ibrows

20/02 2013

1.1.1

1.1.1.0

Manage Sonata Form, Data, List and ShowMapper over annotations

  Sources   Download

MIT

The Requires

 

sonata annotation ibrows

19/02 2013

1.1.0

1.1.0.0

Manage Sonata Form, Data, List and ShowMapper over annotations

  Sources   Download

MIT

The Requires

 

sonata annotation ibrows