外交关系中的可打行为原则

发布于 2024-08-19 05:30:04 字数 1573 浏览 4 评论 0原文

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


产品
  
    id: {类型:整数,主值:true,自动增量:true }
    type_id: { 类型:整数,notnull: true }
    brand_id: { 类型:整数,notnull: true }
  关系
    产品类型:
      类:产品类型
      本地:type_id
     国外:id
    品牌:
      类别:品牌
      本地:brand_id
     国外:id

产品类型
  充当
    I18n:
    字段:{ 名称 }
  
    id: {类型:整数,主值:true,自动增量:true }
    名称: { 类型: string(255), notnull: true }

品牌:
  充当
    I18n:
      字段:{ name }
  
    id: {类型:整数,主值:true,自动增量:true }
    name: { type: string(255), notnull: true }


我想对 Products 表进行 slugify,即。产品将通过它们的段头到达。然而,正如您所看到的,品牌和产品类型表都有国际化行为。而且,产品没有名称。产品的别名将为:“Brand.name - ProductType.name”,并随所提供的语言而变化。

对于这种情况,我是否可以使用 Doctrine 的 Sluggable 行为来自动对我的产品进行 Sluggify。或者我必须手动管理它?

顺便说一下我的环境配置是:
教义版本:1.2
Symfony:1.4.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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

栖竹 2024-08-26 05:30:04

我的理解是,您需要在产品类型和品牌模型中都有别名。您可以保留产品定义不变。不管怎样,我从你的问题中假设每个品牌+类型只有一种产品(即使它没有多大意义)。因此 ProductType 和 Brand 将定义如下:

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

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

然后您必须配置 Product 路由以使用 slugs。之后,您将需要配置操作来检查从路线中获得的内容。

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

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);
}

My understanding is that you need to have the slug in both Product Type and Brand models. You can leave the Product definition as it is. Anyway, I'm assuming from your question that there is only one product for every brand+type (even if it doesn't have to much sense). So ProductType and Brand will be defined like this:

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

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

Then you have to configure your Product route to use the slugs. And after that you will need to configure the action to check what you are getting from the route.

For example, this could be your route for 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

Then in the action you will call to your own findOneBySlugs($brand_slug, $type_slug) method:

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);
}
↙温凉少女 2024-08-26 05:30:04

该解决方案的问题是查询。与:

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

如果我没记错的话,您正在执行 5 连接查询。您可以改进为只执行三个(产品、品牌翻译和产品类型翻译)

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

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

我正在考虑两个选项:

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

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

the problem with that solution are the queries. With:

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

you are doing a 5-join query if I'm not mistaken. You can improve to do only three (product, brand_translation and producttype_translation)

I'm in a similar situation, and the best option is to create a slug for each product using the brand or product type name in this case. So you would only need:

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

I'm thinking between two options:

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

or using the getUniqueSlug() function on the record class. I think the first option is best so you don't have to worry about uniqueness.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文