CakePHP 通过 slug 而不是 ID 过滤列表

发布于 2024-12-08 04:25:55 字数 467 浏览 2 评论 0原文

我正在使用 CakePHP,这是我的数据库的结构:

CarMakes
----------------------------------
ID     Slug     Name
16     ford     Ford

CarModels
----------------------------------
ID     Name     CarMake_ID
10     Escort   16

Cars
----------------------------------
ID     Name     CarModel_ID
1      My car   10

我想通过 CarMakes.Slug 查看汽车列表,

因此网址为: http://localhost/cars/ford

有任何想法或一般信息方向吗?

I'm using CakePHP, this is the structure of my DB:

CarMakes
----------------------------------
ID     Slug     Name
16     ford     Ford

CarModels
----------------------------------
ID     Name     CarMake_ID
10     Escort   16

Cars
----------------------------------
ID     Name     CarModel_ID
1      My car   10

I want to view a list of cars by CarMakes.Slug

so the url would be:
http://localhost/cars/ford

Any ideas or general directions of information?

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

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

发布评论

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

评论(1

小女人ら 2024-12-15 04:25:55

您可以使用 findBy()findAllBy() 根据 ID 以外的内容检索记录。如果您需要为查询提供条件,请使用常规 find()< /a>:

$this->Car->find(
   'all',
   array(
      'conditions' => array(
         'CarMake.Slug' => $slug,
         'Car.Name LIKE' => $name
      ),
   )
);

另外,对于您尝试设置的 URL,您需要创建一个 路线/cars

Router::connect(
   '/cars/:make',
   array('controller' => 'cars', 'action' => 'bymake'),
   array(
      'pass' => array('make'),
      'make' => '[A-Za-z]+'
   )
);

编辑:

如果您的条件基于与模型的直接关联,则上述内容有效。如果您的条件是递归关联(即 Car->CarModel->CarMake),则需要使用显式联接:

$result = $this->Car->find('all', array(
    'joins' => array(
        array(
            'table' => 'car_models',
            'type' => 'inner',
            'foreignKey' => false,
            'conditions' => array('car_models.id = Car.car_model_id')
        ),
        array(
            'table' => 'car_makes',
            'type' => 'inner',
            'foreignKey' => false,
            'conditions' => array(
                'car_makes.id = car_models.car_make_id',
                'car_makes.slug' => $slug
            )
        )
    ),
    'conditions' => array(
        'Car.name LIKE' => $name
    )
));

You can use findBy() or findAllBy() to retrieve records based on something other than ID. If you need to supply conditions to the query, use regular find():

$this->Car->find(
   'all',
   array(
      'conditions' => array(
         'CarMake.Slug' => $slug,
         'Car.Name LIKE' => $name
      ),
   )
);

Also, for the URL you're trying to set up, you will need to create a route for /cars:

Router::connect(
   '/cars/:make',
   array('controller' => 'cars', 'action' => 'bymake'),
   array(
      'pass' => array('make'),
      'make' => '[A-Za-z]+'
   )
);

Edit:

The above works if your conditions are based on a direct association on your model. If your conditions are on a recursive association (i.e. Car->CarModel->CarMake), you need to use explicit joins:

$result = $this->Car->find('all', array(
    'joins' => array(
        array(
            'table' => 'car_models',
            'type' => 'inner',
            'foreignKey' => false,
            'conditions' => array('car_models.id = Car.car_model_id')
        ),
        array(
            'table' => 'car_makes',
            'type' => 'inner',
            'foreignKey' => false,
            'conditions' => array(
                'car_makes.id = car_models.car_make_id',
                'car_makes.slug' => $slug
            )
        )
    ),
    'conditions' => array(
        'Car.name LIKE' => $name
    )
));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文