Custom model in custom module

edited February 2011 in Modules
Hello guys :)

I'm developing my first application with FuelCMS and i have a question about the models, i have read all the module related tutorials but i couldn't find what I'm looking for.

I have a database table called Categories, it has tree fields

| ID | NAME | PARENT |

The parent field is a reference to the same table and allow to nest the categories. When a category is a base one, (it has no parent) i need to insert in the database the value "-1"

For now i have my models working right but in the form it generates i can only choose between the values that already exists, I was able to hard code a new value into the option list, but i don't know how to tell Fuel that if you select the "nothing" value it have to assign -1 in the DB.

I hope it is clear :)

Thanks in advanced for your help.

Comments

  • edited February 2011
    I have tried to use a hook but when i make the modifications over the value the record is never inserted, i guest it is because parent is a foreing key of the ame table si it can't insert a value that don't exists (in the database the field is NOT a foreign key, it is only at hte model)... Could it be like this?

    this is my hook:

    function on_before_insert($values)
    {
    if($values['parent']=="")
    {
    $values['parent'] = -1;
    }
    }
  • edited 10:39PM
    Try using the on_before_validate hook which should occur before any validation takes place.
  • edited 10:39PM
    Now is working with the on_before_validate hook, thanks so much :)
Sign In or Register to comment.