This is the case: I need to insert the basic information of the article into the Post table first, and then return post_id. Then we need to perform some data processing, and then insert the post_id and the corresponding data into the Postmeta table. However, data processing sometimes fails. The number of posts is as follows:
I need to insert the basic information of the article into the Post table first, and then return post_id. Then we need to perform some data processing, and then insert the post_id and the corresponding data into the Postmeta table. However, data processing sometimes fails. As a result, the Post data is successfully inserted, and the data in the Postmeta table fails to be inserted. This is a failed insert. This will generate a lot of redundant Post data.
What I want is that postmeta data insertion fails, and the previous post data should also be erased, just like the transaction rollback.
Transactions are run in the CI model, but I have learned that the model is mainly used to add, delete, modify, and query data. Data Processing (logical processing) is mainly performed in the controller.
So I don't know. How should I deal with this situation?
Thank you.
Reply content:
The situation is as follows:
I need to insert the basic information of the article into the Post table first, and then return post_id. Then we need to perform some data processing, and then insert the post_id and the corresponding data into the Postmeta table. However, data processing sometimes fails. As a result, the Post data is successfully inserted, and the data in the Postmeta table fails to be inserted. This is a failed insert. This will generate a lot of redundant Post data.
What I want is that postmeta data insertion fails, and the previous post data should also be erased, just like the transaction rollback.
Transactions are run in the CI model, but I have learned that the model is mainly used to add, delete, modify, and query data. Data Processing (logical processing) is mainly performed in the controller.
So I don't know. How should I deal with this situation?
Thank you.
public function postWithOther()
The Controller does not need to worry about whether there is a transaction in it.
In my understanding, the model is actually a business logic layer. You can write a method (including transaction processing) in the model in this scenario ).
Reference for CI transactions: http://codeigniter.org.cn/user_guide/database/transactions.html