it-blaster / checkbox-list-bundle
类型:Checkbox-list for symfony-form.
v1.0.0
2015-03-10 13:39 UTC
Requires
- propel/propel: ~2.0@dev
- propel/propel-bundle: >=1.4
- sonata-project/propel-admin-bundle: >=1.0
- symfony/symfony: >=2.2
This package is not auto-updated.
Last update: 2024-09-25 14:08:08 UTC
README
Symfony2 bundle. 使用 "checkbox-list" 类型于 symfony-form。
安装
在 composer.json
中添加 ItBlasterCheckboxListBundle
{ "require": { "it-blaster/checkbox-list-bundle": "dev-master" }, }
现在运行 composer 命令下载捆绑包
$ php composer.phar update it-blaster/checkbox-list-bundle
composer 将捆绑包安装到项目文件夹 vendor/it-blaster/checkbox-list-bundle
中。
接下来在 AppKernel.php
核心中连接捆绑包
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new ItBlaster\CheckboxListBundle\ItBlasterCheckboxListBundle(), ); }
在 app/config/config.yml
中需要连接模板视图 checkbox_list
twig: form: resources: - 'ItBlasterCheckboxListBundle:Form:checkbox_list_widget.html.twig' assetic: bundles: - 'ItBlasterAttachFileBundle'
使用
以“Contact”和“ContactGroup”两个实体为例,展示如何使用 checkbox_list
小部件。在编辑联系人组表单中添加小部件和辅助方法
class ContactGroupAdmin extends Admin { protected $contact_choices = array(); /** * @param FormMapper $formMapper */ protected function configureFormFields(FormMapper $formMapper) { $formMapper ->tab('Атрибуты') ->with('Атрибуты', ['class'=>'col-md-6']) ->add('Slug') ->add('formContacts', 'checkbox_list', array( 'label' => 'Контакты:', 'foreign_objects' => $this->getContactForeignObjects(), 'choices' => $this->contact_choices, 'filter_add_foreign_object' => $this->getFilterAddContact(), 'foreign_object_model' => 'contact', 'bundle_alias' => 'app_main', )) ... ->end() ->end() ; } /** * Привязанные контакты * * @return array */ protected function getContactForeignObjects() { $object = $this->getSubject(); if($object) { $foreign_objects = ContactQuery::create() ->filterByGroupId($object->getId()) ->_or() ->filterByGroupId(NULL) ->find(); } else { $foreign_objects = ContactQuery::create() ->filterByGroupId(NULL) ->find(); } $choices = array(); foreach ($foreign_objects as $foreign_object) { $email = $foreign_object->getEmail() ? $foreign_object->getEmail() : 'не указан'; $phone = $foreign_object->getPhone() ? $foreign_object->getPhone() : 'не указан'; $label = 'Email: '.$email.', Телефон: '.$phone; $choices[] = array( 'id' => $foreign_object->getId(), 'label' => $label, 'checked' => $foreign_object->getGroupId() ); $this->contact_choices[$foreign_object->getId()] = $label; } return $choices; } /** * Парметры фильтра для добавления контакта с выставленным текущей группой * * @return array */ protected function getFilterAddContact() { $filter = array(); $object = $this->getSubject(); if ($object->getId()) { $filter = array( 'filter[Group][type]' => '', 'filter[Group][value]' => $object->getId() ); } return $filter; } /** * После создания объекта * * @param mixed $object * @return mixed|void */ public function postPersist($object) { $object->updateContacts(); } /** * После создания объекта * * @param mixed $object * @return mixed|void */ public function postUpdate($object) { $object->updateContacts(); } ... }
并在 ContactGroup
模型中添加以下方法
class ContactGroup extends BaseContactGroup { protected $form_contacts = NULL; /** * Привязанные контакты * Используется только в форме CMS * * @return null */ public function getFormContacts() { return $this->form_contacts; } /** * Привязанные контакты * Используется только в форме CMS * * @param mixed $form_contacts */ public function setFormContacts($form_contacts) { $this->form_contacts = $form_contacts; } /** * Обновляем привязанные контакты * Используется только в форме CMS */ public function updateContacts() { $object_list = $this->getFormContacts(); if ($object_list !== NULL) { //затираем старые значения ContactQuery::create() ->filterByGroupId($this->getId()) ->update(array('GroupId' => NULL)); //выставляем новые if (is_array($object_list) && count($object_list)) { ContactQuery::create() ->filterById($object_list) ->update(array('GroupId' => $this->getId())); } } } }
致谢
It-Blaster it-blaster@yandex.ru