简体   繁体   English

cakephp mysql,自动增量id或varchar作为主键

[英]cakephp mysql ,autoincrement id or varchar as primary key

我正在用cakephp 2.4构建一个Web应用程序,并且首先用一个整数自动递增的id设计了我的用户表,而由于我的URL的结构为: http://mywebsite/users/view/username我意识到,与其使用一个整数自动增量,使用用户名(VARCHAR 100)作为我的主键可能对性能更好。现在我很好奇这种方法在数据库增长时将如何影响站点性能。

Don't use varchars as your primary key. 不要使用varchars作为主键。 It's horrible for performance, you need to explicitly check that you don't have duplicate keys, it's not conventional, foreign keys then need to be varchar, the router won't handle it properly... ugh. 这对性能来说太可怕了,您需要显式检查您是否没有重复的密钥,这不是常规的,外键则需要为varchar,路由器将无法正确处理它。

If you're worried about SEO, then you can read up on how the cakePHP router can handle this . 如果您担心SEO,则可以阅读CakePHP路由器如何处理此问题

In my application I dynamically generate a routes file with the seo url that is matched to the ID of the thing that I'm trying to display. 在我的应用程序中,我动态生成一个带有seo url的路由文件,该文件与我要显示的事物的ID相匹配。 In your case this file would have entries like: 在您的情况下,该文件将具有以下条目:

Router::connect('/users/view/johndoe', array(
            'controller' => 'users',
            'action' => 'view',
            13));

This solution works very well for me, and the seo portions are handled by an SEO behavior that is attached to the model. 该解决方案对我来说效果很好,并且seo部分由附加到模型的SEO行为处理。 It also works with reverse routing, so that when you make a link like this: 它也适用于反向路由,因此当您进行如下链接时:

<?php echo $this->Html->link(__('View'), array('action' => 'view', $user['User']['id'])); ?>

it will automatically create a link like /users/view/johndoe. 它将自动创建一个类似于/ users / view / johndoe的链接。 If you decide to change this structure later on, or have a special user with a special path (like http://mywebsite.com/user-john-connor ) then this will automatically change all links to that user's view to the new URL. 如果您决定稍后更改此结构,或拥有一个具有特殊路径的特殊用户(例如http://mywebsite.com/user-john-connor ),则这会将指向该用户视图的所有链接自动更改为新URL。 。

As per other answers, use an integer for your PK. 根据其他答案,请为您的PK使用一个整数。 Pick the correct int size depending on how many records you're expecting. 根据您期望的记录数量来选择正确的int大小。 Not using an integer PK with Cake might cause some problems. 不对Cake使用整数PK可能会导致一些问题。 if you're using a Framework, stick with it's conventions - that's the advantage of a framework! 如果您使用的是框架,请遵循其约定-这就是框架的优势!

DO NOT generate a unique route for every user dynamically in your routes.php. 不要在您的routes.php中为每个用户动态生成唯一的路由。 That goes against the entire point of having generic routing. 这与具有通用路由的整个观点背道而驰。 One route should account for this, and you deal with it in the relevant controller. 一种方法应该解决这个问题,您可以在相关控制器中进行处理。

For example, if you specify all of your real controllers/actions: 例如,如果您指定所有实际控制器/动作:

Router::connect('/:controller', array('controller' => 'user|anotherController|etc'));
Router::connect('/:action', array('controller' => 'something'), array('action' => 'allowed|actions|etc'));
Router::connect('/:action', array('controller' => 'else'), array('action' => 'allowed|actions|etc'));

You can then send everything else, as in, site.com/username, to somewhere specific. 然后,您可以将其他所有内容(例如site.com/username)发送到特定位置。

Router::connect('/:username', array('controller' => 'users'), array('action' => 'view'), array('pass'=>array('username'), 'username' => 'regex'));

And accept the 'username' var in the view function, and use that to find the correct user from your db. 并在视图函数中接受“ username”变量,并使用该变量从数据库中查找正确的用户。 Also, note that if 另外请注意,如果

It's not good to create varchar2 as primary key, I think it's better to be as int with auto-incremental and you can use routeing in cakephp : http://book.cakephp.org/2.0/en/development/routing.html 创建varchar2作为主键不是很好,我认为最好将它与int一起使用,并且可以在cakephp中使用路由: http : //book.cakephp.org/2.0/en/development/routing.html

to be : http://yourwebsite.com/:username 成为:http: //yourwebsite.com/ : username

good luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM