1

我有一个数据库设计案例,我很好奇 Doctrine ORM 是否支持开箱即用。


产品
  
    id:{type:integer,primary:true,autoincrement:true}
    type_id:{type:integer,notnull:true}
    brand_id:{type:integer,notnull:true}
  关系
    ProductType:
      class:ProductType
      local: type_id
      国外:id
    品牌:
      类:品牌
      本地:brand_id
      国外:id

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

品牌
  actAs
    I18n:
      字段:{名称}
  
    id:{类型:整数,主要:真,自动增量:真}
    名称:{类型:字符串(255),notnull:真}


我想 slugify Products 表,即。产品将通过它们的蛞蝓到达。但是,正如您所见,brand 表和 productype 表都有 i18n 行为。而且,产品没有名称。产品的 slug 将是:“Brand.name - ProductType.name”,并随服务的语言而变化。

对于这种情况,无论如何我可以使用 Doctrine 的 Sluggable 行为来自动对我的产品进行 sluggify。还是我必须手动管理它?

顺便说一下,我的环境配置是:
Doctrine Version : 1.2
Symfony : 1.4.1

谢谢

4

2 回答 2

2

我的理解是,您需要在产品类型和品牌模型中都有 slug。您可以保持产品定义不变。无论如何,我从您的问题中假设每个品牌+类型只有一种产品(即使它没有太大意义)。所以 ProductType 和 Brand 将被定义如下:

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

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

然后,您必须配置您的 Product 路线以使用 slug。之后,您将需要配置操作以检查您从路线中获得了什么。

例如,这可能是您的产品路线:

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-join 查询。您可以改进为只做三个(产品、品牌翻译和产品类型翻译)

我处于类似情况,在这种情况下,最好的选择是使用品牌或产品类型名称为每个产品创建一个 slug。所以你只需要:

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

我在两个选项之间考虑:

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

或在记录类上使用 getUniqueSlug() 函数。我认为第一个选项是最好的,所以你不必担心唯一性。

于 2010-02-17T12:13:44.043 回答