activelamp / taxonomy-bundle
使用此扩展包创建词汇表和术语。
dev-master
2015-05-02 08:47 UTC
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-09-24 06:46:45 UTC
README
#使用方法
步骤
- 将
activelamp/taxonomy-bundle
作为依赖项添加到您的项目中。 - 注册扩展包
$bundles = array( ... new ActiveLAMP\TaxonomyBundle\ALTaxonomyBundle(), );
- 更新您的数据库模式(例如,运行
php app/console doctrine:schema:update --force
) - 将以下行添加到您的
app/config/routing.yml
文件中,以公开术语和分类的 后端管理
al_taxonomy: resource: "@ALTaxonomyBundle/Resources/routing.yml" prefix : /manage-taxonomies #This can be whatever you want.
#实体设置
在您开始使用术语之前,必须设置您的实体。这是通过向实体类和您想要使用术语的属性添加一些注解来完成的。您将使用到的注解位于\ActiveLAMP\TaxonomyBundle\Annotations
命名空间。
- 通过使用
@Entity
注解标记您的实体为术语实体。 - 使用
@Vocabulary
注解标记将使用词汇术语的属性。 - 在必要时对词汇字段进行占位(更多示例见下文。)
<?php namespace Foo\BarBundle\Entity; use Doctrine\ORM\Mapping as ORM; use ActiveLAMP\TaxonomyBundle\Annotations as Taxn; /** * The identifier defaults to "id". However, since this entity uses the userId field * as its primary key, then we should set identifier to "userId", like below: * * @Taxn\Entity(identifier="userId") */ class User { /** * @ORM\Id */ protected $userId; /** * This will contain an array of ActiveLAMP\TaxonomyBundle\Entity\Term objects * from the "languages" vocabulary that are linked to the entity. * * @Taxn\Vocabulary(name="languages") */ protected $languages; /** * This is a field that will contain a singular taxonomy term value instead * instead of an array of them. * See the singular=true setting in the annotation below. * * This will contain a singular ActiveLAMP\TaxonomyBundle\Entity\Term object * from the "organizations" vocabulary that is linked to the entity. * * @Taxn\Vocabulary(name="organizations", singular=true) */ protected $organization; public function __construct() { /* * You might want to stub non-singular vocabulary fields with * an instance of ArrayCollection so that you can add, remove terms * on a non-managed/detached instance of this entity class. * * i.e. * $user = new User(); * $user->getLanguages()->add($term); * */ $this->languages = new \Doctrine\Common\Collections\ArrayCollection(); } public function getLanguages() { return $this->languages; } public function setOrganization(\ActiveLAMP\TaxonomyBundle\Entity\Term $organization) { /* * The taxonomy system would inject an instance of * \ActiveLAMP\TaxonomyBundle\Entity\SingularVocabularyField * into your entities during certain points in the entity's lifecycle. * * You might also want to check for this field as well and deal with terms * appropriately. * * Although this is NOT necessary at all, it saves the taxonomy system * some trips to the database in certain cases. * */ if ($this->organization instanceof \ActiveLAMP\TaxonomyBundle\Entity\SingularVocabularyField) { $this->organization->setTerm($organization); } else { $this->organization = $organization; } } public function getOrganization() { /* * Nothing extra here, as SingularVocabularyField will just * act like a Term object. So as far as you are concerned, treat * this as a Term object. */ return $this->organization; } }
#分类服务
可以从服务容器中检索分类服务,地址为al_taxonomy.taxonomy_service
。
#常见操作
###检索词汇表
<?php //Via the service: $languages = $service->findVocabularyByName("languages") //Via the vocabulary field of a managed entity: $user = $em->find('Foo\BarBundle\Entity\User', 1); $languages = $user->getLanguages()->getVocabulary(); //From detached entities: $user = new User(); $service->loadVocabularyFields($user); $languages = $user->getLanguages()->getVocabulary();
###检索术语
<?php //From a vocabulary object: $languages = $service->findVocabularyByName("languages"); $french = $language->getTermByName('french'); $filipino = $language->getTermByName('filipino'); /* Will throw a \DomainException for non-existing terms. */ $klingon = $language->getTermByName('klingon');
###遍历实体分类术语
//With a managed entity: $user = $em->find('Foo\BarBundle\Entity\User', 1); foreach ($user->getLanguages() as $languageTerm) { echo $languageTerm->getLabelName(); } //With a detached entity: $user = new User(); //This will yield nothing. foreach ($user->getLanguages() as $languageTerm) { exit("Something you won't see."); } /* * You would have to call TaxonomyService#loadVocabularyFields before * you can loop through attached terms. */ $service->loadVocabularyFields($user); foreach ($user->getLanguages() as $languageTerm) { echo $languageTerm->getLabelName(); }
###持久化分类
//Managed entities: $user = $em->find('Foo\BarBundle\Entity\User', 1); $languages = $user->getLanguages()->getVocabulary(); $user->getLanguages()->add($languages->getTermByName('french')); $user->getLanguages()->removeElement($languages->getTermByName('english')); $service->saveTaxonomies($user); //How about detached entities? $user = new User(); $user->setName("Albert Einstein"); $user->getLanguages() ->replace(array( $languages->getTermByName('english'), $languages->getTermByName('german') )); $em->persist($user); $em->flush(); $service->saveTaxonomies($user); //No problem!