astina / labels-bundle
Symfony 扩展包,为 Doctrine 实体提供分类标签,用于过滤搜索。
0.4.0
2016-05-03 15:15 UTC
Requires
- doctrine/orm: ~2.0
- symfony/symfony: ~2.3
This package is not auto-updated.
Last update: 2024-09-14 16:08:24 UTC
README
为 Doctrine 实体添加分类可翻译标签,并使用它们进行过滤搜索。
配置
将以下内容添加到您的 AppKernel.php 文件中
class AppKernel extends Kernel { public function registerBundles() { $bundles = array( ... new Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle(), new Astina\Bundle\LabelsBundle\AstinaLabelsBundle(), ... ); } }
将以下内容添加到您的 config.yml 文件中
stof_doctrine_extensions: default_locale: de translation_fallback: true orm: default: translatable: true doctrine: orm: mappings: gedmo_translatable: type: annotation prefix: Gedmo\Translatable\Entity dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Translatable/Entity" is_bundle: false
使用方法
将 labels 属性添加到您的实体中
class Foo { // ... /** * @ORM\ManyToMany(targetEntity="Astina\Bundle\LabelsBundle\Entity\Label") */ private $labels; }
像这样在 formType 中使用它
$builder ->add('labels', 'astina_labels', array( 'categories' => array('category1', 'category2'...), 'label' => 'Labels', 'required' => false, )) ;
如果您想在 formType 中看到标签,每个标签都必须属于一个分类,所以请用 fixtures 配置它们。
public function load(ObjectManager $manager) { $labelCategory = new LabelCategory(); $labelCategory->setName('category1'); $manager->persist($labelCategory); foreach ($labelsArray as $labelName) { $label = new Label(); $label->setName($labelName); $label->setCategory($labelCategory); $manager->persist($label); } $manager->flush(); }
搜索
配置一个过滤搜索服务
# .../config/services.yml services: app.filter_search.foo: class: Astina\Bundle\LabelsBundle\Search\LabelSearch #class: Astina\Bundle\LabelsBundle\Search\TranslatedLabelSearch arguments: - @astina_labels.repository.label - @app.repository.foo app.repository.foo: class: Doctrine\ORM\EntityRepository factory_service: doctrine factory_method: getRepository arguments: - AppMyBundle:Foo
使用该服务来查找具有给定标签的实体
$search = $container->get('app.filter_search.foo'); $results = $search->search(new SearchQuery($labels));