$list = new Model_List(); $list->title = Input::post('title'); $list->content = Input::post('content'); $list->shows = new Model_Shows(); $list->shows->episodes = Input::post('episodes'); $list->shows->airdate = Input::post('airdate'); $list->save();
Fuel\Core\Database_Exception [ 1048 ]: Column 'list_id' cannot be null [ INSERT INTO `list_shows` (`list_id`, `episodes`, `list_airdate`) VALUES (NULL, '15', '1305893843') ]
Jelmer Schreuder wrote on Friday 20th of May 2011:The problem is that the relation needs the primary keys in order to be established, but to get to that the model_to still had to get saved first. This shouldn't be necessary and I fixed it in the develop branch, but for now you can get it to work by allowing list_id to be null - that way it'll work without problems.
I'm having the same issue but this workaround won't work for me because I'm using the InnoDB Engine, which means foreign keys are not allowed to be null. Any other ideas how I can get it to work?Jelmer Schreuder wrote on Friday 20th of May 2011:The problem is that the relation needs the primary keys in order to be established, but to get to that the model_to still had to get saved first. This shouldn't be necessary and I fixed it in the develop branch, but for now you can get it to work by allowing list_id to be null - that way it'll work without problems.
Set them to allow NULL? InnoDB doesn't enforce each FK to have a non-empty value unless you tell it to. Only thing the FK enforces is for it to have a value that is a valid PK value in the table to which it relates OR null.Michael Kreis wrote on Wednesday 28th of September 2011:I'm having the same issue but this workaround won't work for me because I'm using the InnoDB Engine, which means foreign keys are not allowed to be null. Any other ideas how I can get it to work?Jelmer Schreuder wrote on Friday 20th of May 2011:The problem is that the relation needs the primary keys in order to be established, but to get to that the model_to still had to get saved first. This shouldn't be necessary and I fixed it in the develop branch, but for now you can get it to work by allowing list_id to be null - that way it'll work without problems.
Orm\Exception [ Error ]: Invalid Model instance added to relations in this model.
$list = new Model_List(); $list->list_title = Input::post('title'); $list->list_content = Input::post('content'); $list->music->list_tracks = Input::post('tracks'); $list->music->list_album = Input::post('album'); $list->save();Note: It does save to the 'list' table but not to the music table, even with the error.
$list = new Model_List(); $list->list_title = Input::post('title'); $list->list_content = Input::post('content'); $list->music = new Model_Music(); $list->music->list_tracks = Input::post('tracks'); $list->music->list_album = Input::post('album'); $list->save();
Thanks, since they are related they need to have the same list_id, how do i get the list_id from the insert into the Model_List to the Model_Music ?Jelmer Schreuder wrote on Wednesday 18th of May 2011:The way you do it it'll be a stdClass object and not a Model_Music object. You need to create the new Model_Music instance yourself:
$list = new Model_List(); $list->list_title = Input::post('title'); $list->list_content = Input::post('content'); $list->music = new Model_Music(); $list->music->list_tracks = Input::post('tracks'); $list->music->list_album = Input::post('album'); $list->save();
Jelmer Schreuder wrote on Wednesday 18th of May 2011:When saving relations like this, the apropriate foreign keys will be set automaticly.
Fuel\Core\Database_Exception [ 1062 ]: Duplicate entry '0' for key 'PRIMARY' [ INSERT INTO `mini_lists_shows` (`list_episodes`, `list_duration`, `list_aired`, `list_age_rate`) VALUES ('10', '10', '10', 'PG-17') ]
Jelmer Schreuder wrote on Wednesday 18th of May 2011:No, that just means you didn't make it auto-increment.
Jelmer Schreuder wrote on Wednesday 18th of May 2011:Each table is expected to have its own unique primary key(s), if you don't auto-increment them you need to set those IDs manually before saving.
It looks like you're new here. If you want to get involved, click one of these buttons!