V
V
Viktor Yanyshev2017-10-01 12:10:50
MySQL
Viktor Yanyshev, 2017-10-01 12:10:50

How to get a record from the database by its ID without using the id in the url, in yii?

There is a page where there is a list of specialists /specialists, it contains links to the pages of specialists /specialists/last_name-first_name.html
To create a URL for the specialist page, I do this:

Url::toRoute(['specialists/show', 'url' => Translit::t($specialist->first_name.'-'.$specialist->last_name)])

In order not to translate back into Russian and not to search for a record in the database by the name and surname when going to the specialist's page, you need to pass the id of the record. But! id should not be in url, how can i do that?
How else can you pass the id of the specialist record on the specialist directory, so that when you go to the specialist page, you could use the id to retrieve the record from the database, but,

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2017-10-01
@villiwalla

You want the url to not have an id, but you want to search by it - a vicious circle. Miracles don't happen.
The easiest way is to make another url column in the specialist table and write the url there and find it. So you solve 1 more problem that you don’t see, the uniqueness of this url, since even with a unique pair of first_name and last_name, after transliteration, a non-unique value may result.
You can write data to this column when creating a specialist. But there is another problem, this is the persistence of the url. If the user changes first_name or last_name, then whether to change this url? After all, the link mass that has accumulated will lead nowhere, and if you do not change this url, then the question arises of the appropriateness of the approach as a whole. Look at the greats of this world who have passed this way - social networks. They don't automate this process, they generate a unique url for the profile and then offer a random change. It's justified. It is right.
Alternatively, sometimes they do this:
domain.com/id_firstname_lastnamewhile in the action you can extract the id and find the user, everything after id_ is purely decorative. But I am not a supporter of this method, since it’s not ice to give records in the url id. And then it is better to give a hash, and since a hash, then again an additional column is needed and we come to the first option.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question