1

I have a database design case which I am curios whether Doctrine ORM supports it out-of-box.


Product:
  columns:
    id: {type: integer, primary: true, autoincrement: true }
    type_id: { type: integer, notnull: true }
    brand_id: { type: integer, notnull: true }
  relations:
    ProductType:
      class: ProductType
      local: type_id
      foreign: id
    Brand:
      class: Brand
      local: brand_id
      foreign: id

ProductType:
  actAs:
    I18n:
    fields: { name }
  columns:
    id: {type: integer, primary: true, autoincrement: true }
    name: { type: string(255), notnull: true }

Brand:
  actAs:
    I18n:
      fields: { name }
  columns:
    id: {type: integer, primary: true, autoincrement: true }
    name: { type: string(255), notnull: true }


I want to slugify Products table, ie. products will be reached via their slugs. However, as you see both brand and productype tables has i18n behaviour. And moreover, product doesnt have a name. A product's slug will be: "Brand.name - ProductType.name", and vary with the language served.

For this scenario, is there anyway I can use Sluggable behaviour of Doctrine to sluggify my products automatically. Or do I have to manage it manually?

By the way my environment configuration is:
Doctrine Version: 1.2
Symfony: 1.4.1

Thanks

4

2 に答える 2

2

私の理解では、製品タイプとブランド モデルの両方にスラッグが必要です。製品定義はそのままにしておくことができます。とにかく、私はあなたの質問から、すべてのブランド+タイプに対して1つの製品しかないと仮定しています(それほど意味がなくても). したがって、ProductType と Brand は次のように定義されます。

schema.yml
----------

ProductType:
  actAs:
    I18n:
    fields: { name }
    actAs:
      Sluggable: { fields: [name], uniqueBy: [lang], canUpdate: true }
  columns:
    ...

次に、スラッグを使用するように Product ルートを構成する必要があります。その後、ルートから何を取得しているかを確認するアクションを構成する必要があります。

たとえば、これは Products のルートになる可能性があります。

routing.yml
-----------

product:
  url:   /:sf_culture/product/:brand_slug/:type_slug
  param: { module: product, action: view }
  requirements:
    sf_culture: (?:en|fr)
    sf_method:  get

次に、アクションで独自の findOneBySlugs($brand_slug, $type_slug) メソッドを呼び出します。

product/actions/actions.class.php
---------------------------------

public function executeView(sfWebRequest $request)
{
  $product = Doctrine::getTable('Product')
    ->findOneBySlugs(
                     $request->getParameter('brand_slug'),
                     $request->getParameter('type_slug')
                    );

  $this->forward404Unless($product);
}
于 2010-01-29T12:33:15.740 に答える
1

そのソリューションの問題はクエリです。と:

$product = Doctrine::getTable('Product')
->findOneBySlugs(
                 $request->getParameter('brand_slug'),
                 $request->getParameter('type_slug')
                );

私が間違っていなければ、5結合クエリを実行しています。3つ(product、brand_translation、producttype_translation)だけで改善できます

私も同様の状況にあり、この場合、ブランド名または製品タイプ名を使用して各製品のスラッグを作成するのが最善の方法です。したがって、必要なのは次のとおりです。

$product = Doctrine::getTable('Product')
  ->findOneBySlug($request->getParameter('slug'));

私は2つのオプションの間で考えています:

Product:
  actAs:
    Sluggable:
      unique: true
      fields: [title]
      builder: [Slug, slugify] 

または、レコード クラスで getUniqueSlug() 関数を使用します。一意性について心配する必要がないので、最初のオプションが最適だと思います。

于 2010-02-17T12:13:44.043 に答える