We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

What is best approach?

Hi, i have a user table (id - int, primary key, name - varchar, thumb - varchar).

When i create a user, i want to save thumb name prefixed with id. ex: 1, 'test user', '1_thumb.png'

I know that after inserting a record only i'll get its id. and updating thumb prefixing id (means TWO operations on each create user). I would like to the best practices in this regard... when there are many users trying to create their profile.

thanks in advance.

Some trigger in datbase after adding row ?



85.5k

there are many ways to make this happen. Many people are ussing timestamp and stuff to predict which upload belongs to whom after that. I personally do it, stupid but easy.

Every time i click ( for ex. ) add new category i create a new record in the database ( I have a "flag" column that sais that this record is deleted, so its not displayed on the page ). Now once i have the ID i can use it in my form. I have a cron job running every monday that cleans all those "blank records" that are created by just clicking add category.

edited Aug '16

Just do it in two atomic operations (INSERT, then UPDATE). InnoDB engine locks table per row/record only, you'll hardly notice any difference while using indexed primary key for UPDATE. UPDATE something WHERE PrimaryKey = 9283;



2.5k

Hi, Thanks for sharing your thoughts. Here, my approach here is to prefix the thumb file name with the auto generated ID and at the same time i want to store the file in file system. eg: in DB, 1, 'test user', '/assets/profile_images/1_thumb.png' in file system, ~/app_base/assets/profile_images/1_thumb.png.