Last active
February 3, 2016 22:42
-
-
Save webdevilopers/2e67b886558b42360afc to your computer and use it in GitHub Desktop.
Override One To Many Template for Collection using Doctrine `indexBy` in Sonata Admin Forms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| LOC_ID = {{ form.children.4.value.vars['value'] }} {# index exists #} | |
| LOC_ID = {{ form.children.LOC_ID.value.vars['value'] }} {# index does not exist #} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| services: | |
| sonata.user.admin.user: | |
| class: Application\Sonata\UserBundle\Admin\UserAdmin | |
| tags: | |
| - { name: sonata.admin, manager_type: orm, group: "%sonata.user.admin.groupname%", label_catalogue: "messages", label: "Benutzer"} | |
| arguments: | |
| - ~ | |
| - Application\Sonata\UserBundle\Entity\User | |
| - ~ | |
| calls: | |
| - [ addChild, [ @sonata.user.admin.user_setting ] ] | |
| sonata.user.admin.user_setting: | |
| class: Application\Sonata\UserBundle\Admin\UserSettingAdmin | |
| tags: | |
| - { name: sonata.admin, manager_type: orm, group: "%sonata.user.admin.groupname%", label_catalogue: "messages", label: "Konfiguration"} | |
| arguments: | |
| - ~ | |
| - Application\Sonata\UserBundle\Entity\UserSetting | |
| - ~ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {#{% use 'SonataAdminBundle:Form:form_admin_fields.html.twig' %}#} | |
| {% block sonata_admin_orm_one_to_many_widget %} | |
| {% if sonata_admin.name == 'settings' %} | |
| {% include 'ApplicationSonataUserBundle:UserAdmin:_settings.html.twig' %} | |
| {% else %} | |
| {% include 'SonataDoctrineORMAdminBundle:CRUD:edit_orm_one_to_many.html.twig' %} | |
| {% endif %} | |
| {% endblock %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| SELECT | |
| t0.id AS id1, | |
| t0.config_key AS config_key2, | |
| t0.config_value AS config_value3, | |
| t0.datetime AS datetime4, | |
| t0.users_id AS users_id5 | |
| FROM | |
| users_config t0 | |
| WHERE | |
| t0.users_id = ? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Application\Sonata\UserBundle\Entity; | |
| use Sonata\UserBundle\Entity\BaseUser as BaseUser; | |
| //use FOS\UserBundle\Model\User as BaseUser; | |
| //use Doctrine\Common\Collections\Collection; | |
| use Gedmo\Mapping\Annotation as Gedmo; | |
| use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; | |
| use Doctrine\ORM\Mapping as ORM; | |
| /** | |
| * User | |
| * | |
| * @ORM\Entity(repositoryClass="UserRepository") | |
| * @ORM\Table(name="users", indexes={ | |
| * @ORM\Index(columns={"username"}), | |
| * @ORM\Index(columns={"users_key"}), | |
| * @ORM\Index(columns={"block_user"}), | |
| * @ORM\Index(columns={"deleted"}) | |
| * }) | |
| * UniqueEntity(fields="email", message="Ce nom d'utilisateur est déjà pris.", groups={"registration"}) | |
| * UniqueEntity(fields="email_canonical", message="Ce nom d'utilisateur est déjà pris.", groups={"registration"}) | |
| * @UniqueEntity(fields="email", message="Ce nom d'utilisateur est déjà pris.") | |
| * @UniqueEntity(fields="email_canonical", message="Ce nom d'utilisateur est déjà pris2.") | |
| * ORM\AttributeOverrides({ | |
| * ORM\AttributeOverride(name="email", column=@ORM\Column(type="string", name="email", length=255, unique=false, nullable=true)), | |
| * ORM\AttributeOverride(name="emailCanonical", column=@ORM\Column(type="string", name="email_canonical", length=255, unique=false, nullable=true)) | |
| * }) | |
| */ | |
| class User extends BaseUser | |
| { | |
| /** | |
| * @var integer $id | |
| * | |
| * @ORM\Column(name="users_id", type="integer", precision=0, scale=0, nullable=false, unique=false) | |
| * @ORM\Id | |
| * @ORM\GeneratedValue(strategy="IDENTITY") | |
| */ | |
| protected $id; | |
| /** | |
| * @var Collection $settings | |
| * | |
| * @ORM\OneToMany(targetEntity="Application\Sonata\UserBundle\Entity\UserSetting", indexBy="config_key", mappedBy="user", fetch="EAGER") | |
| * ORM\OneToMany(targetEntity="Application\Sonata\UserBundle\Entity\UserSetting", indexBy="config_key", mappedBy="user") | |
| */ | |
| protected $settings; | |
| public function __construct() { | |
| parent::__construct(); | |
| $this->settings = new \Doctrine\Common\Collections\ArrayCollection(); | |
| } | |
| public function getSettings() | |
| { | |
| return $this->settings; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| SELECT | |
| t0.username AS username1, | |
| t0.username_canonical AS username_canonical2, | |
| t0.email AS email3, | |
| t0.email_canonical AS email_canonical4, | |
| t0.enabled AS enabled5, | |
| t0.salt AS salt6, | |
| t0.password AS password7, | |
| t0.last_login AS last_login8, | |
| t0.locked AS locked9, | |
| t0.expired AS expired10, | |
| t0.expires_at AS expires_at11, | |
| t0.confirmation_token AS confirmation_token12, | |
| t0.password_requested_at AS password_requested_at13, | |
| t0.roles AS roles14, | |
| t0.credentials_expired AS credentials_expired15, | |
| t0.credentials_expire_at AS credentials_expire_at16, | |
| t0.created_at AS created_at17, | |
| t0.updated_at AS updated_at18, | |
| t0.date_of_birth AS date_of_birth19, | |
| t0.firstname AS firstname20, | |
| t0.lastname AS lastname21, | |
| t0.website AS website22, | |
| t0.biography AS biography23, | |
| t0.gender AS gender24, | |
| t0.locale AS locale25, | |
| t0.timezone AS timezone26, | |
| t0.phone AS phone27, | |
| t0.facebook_uid AS facebook_uid28, | |
| t0.facebook_name AS facebook_name29, | |
| t0.facebook_data AS facebook_data30, | |
| t0.twitter_uid AS twitter_uid31, | |
| t0.twitter_name AS twitter_name32, | |
| t0.twitter_data AS twitter_data33, | |
| t0.gplus_uid AS gplus_uid34, | |
| t0.gplus_name AS gplus_name35, | |
| t0.gplus_data AS gplus_data36, | |
| t0.token AS token37, | |
| t0.two_step_code AS two_step_code38, | |
| t0.users_id AS users_id39, | |
| t0.users_name AS users_name40, | |
| t0.users_email_address AS users_email_address41, | |
| t0.users_website AS users_website42, | |
| t0.users_level AS users_level43, | |
| t0.users_key AS users_key44, | |
| t0.block_user AS block_user45, | |
| t0.session_id AS session_id46, | |
| t0.note AS note47, | |
| t0.role AS role48, | |
| t0.deleted AS deleted49 | |
| FROM | |
| users t0 | |
| WHERE | |
| t0.users_id = ? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Application\Sonata\UserBundle\Admin; | |
| use Sonata\UserBundle\Admin\Model\UserAdmin as SonataUserAdmin; | |
| use Sonata\AdminBundle\Route\RouteCollection; | |
| use Sonata\AdminBundle\Admin\AdminInterface; | |
| use Sonata\AdminBundle\Datagrid\ListMapper; | |
| use Sonata\AdminBundle\Datagrid\DatagridMapper; | |
| use Sonata\AdminBundle\Form\FormMapper; | |
| use Knp\Menu\ItemInterface as MenuItemInterface; | |
| use Application\Sonata\UserBundle\Entity\UserSetting; | |
| class UserAdmin extends SonataUserAdmin | |
| { | |
| protected $maxPerPage = 10; | |
| protected $userManager; | |
| public function getUserManager() | |
| { | |
| if (!$this->userManager) { | |
| $this->userManager = $this->configurationPool->getContainer()->get('fos_user.user_manager'); | |
| } | |
| return $this->userManager; | |
| } | |
| protected function configureRoutes(RouteCollection $collection) | |
| { | |
| $collection | |
| ->remove('delete') | |
| ->remove('export') | |
| ; | |
| } | |
| public function getFormTheme() | |
| { | |
| return array_merge( | |
| parent::getFormTheme(), | |
| array('ApplicationSonataUserBundle:UserAdmin:form_admin_fields.html.twig') | |
| ); | |
| } | |
| protected function configureFormFields(FormMapper $formMapper) | |
| { | |
| $subject = $this->getSubject(); | |
| $formMapper | |
| ->with('General') | |
| ->add('gender', 'sonata_user_gender', array( | |
| 'required' => true, | |
| 'translation_domain' => $this->getTranslationDomain() | |
| )) | |
| ->add('firstname', null, array('required' => false)) | |
| ->add('lastname', null, array('required' => false)) | |
| ->add('email', null, array('required' => false)) | |
| ->add('website', null, array('required' => false)) | |
| ->end() | |
| ; | |
| // if ($this->getSubject() && !$this->getSubject()->hasRole('ROLE_SUPER_ADMIN')) { | |
| if ($this->getSubject() && !$this->getSubject()->hasRole('ROLE_ADMIN')) { | |
| $formMapper | |
| ->with('Zugang') | |
| ->add('username') | |
| ->add('plainPassword', 'text', array( | |
| 'required' => (!$this->getSubject() || is_null($this->getSubject()->getId())) | |
| )) | |
| ->add('realRoles', 'sonata_security_roles', array( | |
| // ->add('roles', 'sonata_security_roles', array( | |
| 'label' => 'form.label_roles', | |
| 'expanded' => true, // render checkboxes instead of select | |
| 'multiple' => true, | |
| 'required' => false, | |
| 'choices' => [ | |
| 'ROLE_USER' => 'Partner', | |
| 'ROLE_ADMIN' => 'Admin', | |
| // 'ROLE_SUPER_ADMIN' => 'Superadmin', | |
| ] | |
| )) | |
| // ->add('roles', 'choice', [ | |
| // 'multiple' => true, | |
| // 'choices' => ['ROLE_USER' => 'user', 'ROLE_ADMIN' => 'admin'] | |
| // ]) | |
| ->add('enabled', null, array('required' => false)) | |
| ->add('locked', null, array('required' => false)) | |
| ->add('expired', null, array('required' => false)) | |
| ->add('credentialsExpired', null, array('required' => false)) | |
| ->end(); | |
| } | |
| $userSetting = new UserSetting(); | |
| // @todo how to index by? | |
| $formMapper | |
| ->with('Konfiguration') | |
| ->add('settings', 'sonata_type_collection', array( | |
| 'label' => false, | |
| // 'btn_add' => false, | |
| 'type_options' => array('delete' => false) | |
| ), array( | |
| 'edit' => 'inline', | |
| 'inline' => 'table', | |
| // 'template' => 'ApplicationSonataUserBundle:UserAdmin:form_admin_fields.html', | |
| 'allow_delete' => false, // no effect | |
| 'link_parameters' => array( | |
| 'keys' => $userSetting->getKeys() | |
| ) | |
| )) | |
| ->end() | |
| ; | |
| } | |
| public function createQuery($context = 'list') | |
| { | |
| $query = parent::createQuery($context); | |
| $query->addSelect( | |
| 'PARTIAL ' . $query->getRootAlias() . '.{id, username, email, enabled, locked, createdAt}', | |
| 'PARTIAL g.{id, name}', | |
| 'PARTIAL s.{id, key, value}' | |
| ) | |
| ->leftJoin($query->getRootAlias() . '.groups', 'g') | |
| ->leftJoin($query->getRootAlias() . '.settings', 's') | |
| ; | |
| return $query; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment