S
S
shitprog2019-01-22 11:49:47
Yii
shitprog, 2019-01-22 11:49:47

Inserting data using just added data?

How to organize the initial data for the user?
For example, there is a table tags (id, user_id, tag) into which we insert data (1,1, "food"). There is a second table post(id, user_id, tag_id, text). How to get the id of the inserted tag in order to add a record to the post table, provided that many records are inserted into the table with tags at once?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
morricone85, 2019-01-22
@morricone85

you want to insert id tags from the tags table into the Post table?

M
Maxim Timofeev, 2019-01-22
@webinar

1. Are you sure that there should be tags (id, user_id, tag), and not two tables:
tags (id, tag)
user_tag (user_id, tag_id)
Since users and tags are many-to-many and therefore an intermediate user_tag table is needed .
2.

How to get the id of the inserted tag in order to add a record to the post table, provided that many records are inserted into the table with tags at once?

How exactly is it inserted? Will there be a code? On idea before sending insert to a DB you have an array with the necessary data.
3. It is not clear what relationship the post table has with other tables. Describe where hasOne is, where hasMany is. There is a suspicion that your structure is, to put it mildly, strange. As far as I understand, there should be a table post(id,text,author_id), and post_tag(post_id,tag_id)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question