Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Auto Increment in MySQL and creating new Records
  • I have a controller with 2 actions, ie. action_save($id) and action_add().

    I have a MySQL table User with primary key ID which is of type auto_increment.

    My action_add() calls $user = new Model_User();

    My action_save($id) calls $user = Model_User::find($id); $user->save();

    How do I pass the ID to action_save($id) without first saving the newly created object to the database?

    The reason I want this setup is that it would enable me to use the same action_save($id) both for saving an edited record as a new record to the database.

  • This model, is that:
    - an ORM model
    - a Model_Crud model
    - a custom model?

  • Model_User is an ORM model mapping to the table User.


    $_table_name = 'user';
    $_primary_key = 'id';

    no properties defined.
  • I have trouble understanding what it is exactly you want to do.

    Normally, an action is something you request from the URI, so what is action_save() exactly supposed to do?

    Normally, in an add() action, you create a new object, add data to if (from a form usually), and save it. An edit() action does exactly the same, but then with an existing object. save() doesn't really fit in?

  • I am trying this setup:

    Adding a User to the database:

    action_add(): creates new empty User object, display empty User form

    In the form there are 2 buttons (Save, Cancel) with associated actions:

    action_cancel(): obvious

    action_save($id): $user = find($id); $user->save();

    So I when the user clicks on the Save button I need to pass the User->id of the object just created in action_add() to action_save($id).

    This will not work because the User->id is auto_increment and is only generated when the object is saved to the database.

    The reason I want to do this (I could also create the new User object in action_save() is that I hope to reuse action_save($id) when editing an existing User.

    Editing a User in the database

    action_edit($id): find existing User, display  User form with User data

    action_cancel(): obvious

    action_save($id): same as when Adding a user.

    If this is not possible, I would need 2 separate save actions:

    Adding a User to the database:

    action_add(): display empty User form

    action_save_add(): create new User object; fill with data from form, save()

    Editing a User in the database:

    action_edit($id): find existing User; display User form with user data

    action_save_edit($id); find existing User; fill with data from form; save().

  • Let me try to understand what (imho complicated) setup you have:

    You have an action add() and edit(), both displaying a form. In case of add, an empty one, in case of edit, a populated one. Right?

    Then the form has buttons, each in their own form (otherwise you can't call different URI's on submit)? Doesn't that make you lose the form data? If the buttons are in the same form (which would be a lot more logical), how do you intend to call these other methods (cancel, save, save_edit) as the form will be submitted to the form action?

     Normally an action would have a flow similar to this one: (just replace the create_user call by populating the object and saving it).
  • There is only one User form with no action attached. The form has 2 buttons, ie. Cancel and Save.

    When I click on one of the buttons, an action is dynamically attached to the form with JavaScript.

    When the Cancel button is clicked, action_cancel() is called; when the Save button is clicked action_save() is called.

    action_add() only displays an empty User form and creates an empty User object.

    action_edit() only finds an existing User and displays a populated User form.

    For me this makes sense because it maps to the state transition diagram of my application.

  • Ok.

    So you always submit the form to the save action. No problem. Make sure you include the users PK in the form action in case of an edit, like "/users/save/12".

    Then in your save method you can use that id to fetch a user object:
    public function action_save($id = null)
        // fetch or create a user object
        if ($id)
            if ( ! $user = Model_User::find($id))
                // user with id not found, display an error message terminate the action call
             $user = Model_User::forge();

    // create your validation object here (manually or using ORM)

        // validate the post
    if ( ! $val->run())
        // display errors if not validated and terminate the method

        // populate the user object from the validation object

        // and save it
  • Thx. This is what I was looking for. I was creating a new User object in action_add(), this should be done in action_save().

    Btw. Is it inefficient to do 2 finds after each other when editing an existing record? In this setup action_edit() contains a find() and action_save() contains a second find() of the same record.

    Is it possible to create persistent objects in FuelPHP so action_edit() can pass the User object to action_save()?

  • Those are two different requests, so without persistence you can't pass it along.

    Given the fact that it's a SELECT of a single record on primary key, the impact on performance will be minimal, it will be a hit on index.

    If you want to, you can save the object in the session. I would suggest using a flash variable because you only need it on the next page request. If the model class is in a module, make sure the module is loaded before you try to retrieve it from the session.

    Also, this will not work with cookie based sessions, due to the size of the serialized ORM object.

Howdy, Stranger!

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

In this Discussion