Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Inser Cascade with orm
  • How i can inser one record in my POST table and multiple records in the PHOTOS table.

    MODEL PHOTOS

    protected static $_has_many = array(
            'photos' => array(
                'key_from' => 'id',
                'model_to' => 'Model_Photos',
                'key_to' => 'post_id',
                'cascade_save' => false,
                'cascade_delete' => true,
            )
        );

    I think the problem is my controller

    CONTROLLER

    $post = new Model_Post;
    $post->photos[] = new Model_Photos();

    It only inser in the ralated field, how i can inser in the photo field?
  • HarroHarro
    Accepted Answer
    What is "the photo field"?  This is the correct sequence for creating a related object:

    $post = Model_Post::forge();
    $post->photos[] = Model_Photos::forge(array('field' => 'value'));
    $post->save();

    But as you have disabled cascade_save on the relation, your Model_Photos object will not be saved, so that might be your problem.

    Try to avoid using "new", not in all cases it does the same as forge().
  • am havig problem do the delete

    public function action_delete($id = null)
    {
        if (Input::post('chk_delete')) {
            foreach (Input::post('chk_delete') as $id) {
                if (Model_Mailbox::find($id))
                {   
                    $mailbox->users_to[] = $mailbox->sent_to[] = Model_Mailboxes_Users::forge(array('user_id' => $id));
                    $mailbox->delete();
                    Session::set_flash('success', e('Deleted mailbox #'.$id));
                    Response::redirect('admin/mailbox');
                }
                else
                {
                    Session::set_flash('error', e('Could not delete mailbox #'.$id));
                }
            }
        }
        Response::redirect('admin/mailbox');
    }
  • You're not deleting anything, you're adding here?

    What exactly do you want to delete? The parent object? The related objects? Only the relation between the two?

    I see more issues here.

    You pass a variable $id in the action method, but this value is overwritten by the $id used in your foreach. Is that intended?

    And what is that exactly? It looks like an array with id's of some sort, but on the first find you do a redirect, which means you no longer process all others?

    Perhaps explain exactly what you want to do, and what the input is (both from the URI and from the post).

Howdy, Stranger!

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

In this Discussion