Answer the question
In order to leave comments, you need to log in
How to insert id of current record into another column in Mysql?
There is a table of objects in which there are fields id (PRIMARY, AUTO_INCREMENT) and related_to (relationship).
By default, each object must refer to itself (so that when choosing the main object, you can get all the objects related to it, including itself).
I found an example of a BEFORE INSERT trigger on the Internet:
IF NEW.related_to = 0 THEN
SET NEW.related_to = last_insert_id()+1 ;
END IF
Answer the question
In order to leave comments, you need to log in
I am confused by the attitude towards myself - it will surely cause endless cycles. And I don't understand why it would be necessary. There is a suspicion of a fundamentally wrong approach to the organization of the database and relationships.
But in order to be able to add your id to the newly created record, there is an afterSave method in the model www.yiiframework.com/doc-2.0/yii-db-baseactivereco...last_insert_id()+1
- this is the path to failure
. Is it one to one, one to many, or many to many? If many to many, then there should be a separate table where dependencies are stored.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question