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.
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().
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.
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
}
}
else
{
$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
$user->from_array($val->validated());
// and save it
$user->save();
}
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()?
It looks like you're new here. If you want to get involved, click one of these buttons!