Love Fuel?
Donate
About
Forums
Discussions
Login
FuelPHP Forums
Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
General
Best way to insert related items into DB
JeanAlesi
January 2016
I have the following entity: MEETUP with the following fields
- id,
- owner_id,
- descriptions
I have another entity, MEETUP_MEMBERS, with:
- meetup_id (foreign key references id in meetup)
- user_id //(this represents an user that has been invited to a meetup)
What is the best way to insert these items into a mysql database?
The data I get from the client provides a list of invited users as well as the information to create a meetup entity..
The only way I know how to do this is to:
- create a new MEETUP model, and save,
- next, insert all the user entries provided (do I have to use raw sql here? bc using model->save() opens and closes connections, so takes longer)
Also, is there such a thing as transactions when using models to save to DB?
Thanks.
Harro
January 2016
Accepted Answer
You can do that in one go:
// create a parent
$parent = Model_Parent::forge(array('column' => 'value'));
// add some children
$parent->child[] = Model_Child::forge(array('column' => 'child1'));
$parent->child[] = Model_Child::forge(array('column' => 'child2'));
$parent->child[] = Model_Child::forge(array('column' => 'child3'));
// and save
DB::start_transaction();
if ($parent->save())
{
DB::commit_transaction();
}
else
{
DB::rollback_transaction();
}
JeanAlesi
January 2016
Thank you.
When we do:
parent->child[] = .........
.. the "child" we are referring to, doesn't need to be declared as a property in the parent model?
Harro
January 2016
Accepted Answer
No, it's the name of the relation between the parent and the child model.
Add a Comment
Howdy, Stranger!
It looks like you're new here. If you want to get involved, click one of these buttons!
Sign In
Apply for Membership
Categories
All Discussions
5,088
General
↳ General
3,364
↳ Job Board
13
↳ Installation & Setup
214
Packages
↳ Oil
213
↳ Orm
700
↳ Auth
260
Development
↳ Tips and Tutorials
126
↳ Code share
145
↳ Applications
52
In this Discussion
Harro
January 2016
JeanAlesi
January 2016