Reputation: 5133
I have two classes, A and B, each one mapping a table from my the database. A and B has a relation between a primary key column and another column in the second table. I want to insert/update both tables in one function and I am not sure what is the best way to do it.
I found some solutions on Google but I'm not sure that is the best way to follow.
What should I do? My idea is to have a method like save()
which calls the save methods for A and B. If this is the best choice, what should I extend, CFormModel or CActiveRecord? If I try to extend CFormModel, by following documentation it wouldn't be right to have a method such as save()
or insert()
or update()
. Documentation says that CFormModel is only used to store data that will not be saved. If I extend CActiveRecord, I will have access the save() method but I won't map anything. Documentation says that a CActiveRecord must map a table from a database.
So, what is the best way to do this?
Upvotes: 1
Views: 253
Reputation: 5133
I think this is the best answer. Having more control of the data flow is always better.
@DCoder I think that in a large scale application this will be critical. If you will use aftersave() or override save() and forget the workflow, then you will have a lot of problems. You can find rows in the database and you may not know where they are coming from because, by instinct, you call save(). The alternative is better, having a custom function that calls both save(). But you have to know that this custom method exists.
Upvotes: 0
Reputation: 160
If you have the default folder structure for yii generated from the gii generation tool, in you components folder you may use the controller file and add your save method there and access any CActiveRecords you'd like. Then, you can call your save method from within any controller you'd like.
Upvotes: 1