Use of CI framework transactions

Source: Internet
Author: User
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.

  • Write a new method directly in the Model.

public function postWithOther()

The Controller does not need to worry about whether there is a transaction in it.

  • For the two-layer division of MC, the landlord also needs to study hard.

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.