0

I'm trying to make a relationship in the FuelPHP, but I'm getting the following error:

Fuel\Core\Database_Exception [ 23000 ]: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (filmestorrentz.filmes_tags, CONSTRAINT filmes_tags_filme_id FOREIGN KEY (filme_id) REFERENCES filmes (id) ON DELETE NO ACTION ON UPDATE CASCADE) with query: "INSERT INTO filmes_tags (filme_id, tag_id) VALUES ('', '1')"

I don't know why but FuelPHP is generating a query with filme_id in blank.

Models: Model_Filme, Model_Tag

In the Model_Filme model I have the function that saves the data from the form:

public static function save_filme()
{
    $data = Input::post('data');
    $val = static::validate_filme();

    if ($val->run()) {
        if ($data['id']) {
            $filme = static::find($data['id'])->set($data);
            unset($filme->tags);
        } else {
            $filme = static::forge($data);
        }

        foreach ($data['tags'] as $tag_id) {
            $filme->tags[$tag_id] = Model_Tag::find($tag_id);
        }

        $filme->save(); return array($filme, true, null, null);
    } else {
        return array(static::forge($data), false, $val->error(), $data['tags']);
    }
}

What am I doing wrong? I'm using FuelPHP 1.8

0

1 Answer 1

0

The problem was that in my view there is a hidden field in which kept the value of the id to edit the registry, just gave a unset(data['id']). solved!

public static function save_filme()
{
    $data = Input::post('data');
    $val = static::validate_filme();

    if ($val->run()) {
        if ($data['id']) {
            $filme = static::find($data['id'])->set($data);
            unset($filme->tags);
        } else {
            unset($data['id']);
            $filme = static::forge($data);
        }

        foreach ($data['tags'] as $tag_id) {
            $filme->tags[$tag_id] = Model_Tag::find($tag_id);
        }

        $filme->save(); return array($filme, true, null, null);
    } else {
        return array(static::forge($data), false, $val->error(), $data['tags']);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.