Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Best way to insert related items into DB
  • 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.
     
  • HarroHarro
    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();
    }


  • 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?

  • HarroHarro
    Accepted Answer
    No, it's the name of the relation between the parent and the child model.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion