This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

inIT
inIT

Hi guys,

I am trying to extend Rainlab.blog plugin so it can handle subcategories.

In new category form I have added a dropdown containing categories names that can be selected as parent category of the one being creating. In database table that contains categories I have added a column named parent_id which is actually an id of some other category. And of course it should be nullable in case of root category (the one which does not have any parent). And here comes the problem. I want to have an option in my dropdown (for example "-") which will send null value to Postgres database.

Below you can see the function that returns array of options in dropdown menu:

    public function getParentIdOptions($keyValue=null) {
        $entries = [];
        $entries[null] = "-";
        $categories = self::orderBy('name')->get();
        foreach ($categories as $category) {
            $entries[$category->id] = $category->name;
        }
        return $entries;
    }

In fields.yaml I put:

parent_id:
    label: rainlab.blog::lang.category.parent_category
    type: dropdown

Until now all seems to work great and here comes the problem. October returns me error:

"SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for integer: "" (SQL: insert into "rainlab_blog_categories" ("name", "slug", "parent_id", "updated_at", "created_at") values (Root category, root-category, , 2015-10-07 22:03:31, 2015-10-07 22:03:31) returning "id")" on line 624 of /var/www/projekty/mala-psychologia/vendor/laravel/framework/src/Illuminate/Database/Connection.php

As you can see there are two colons in values. I guess null works perfectly fine.

But I have no idea where is the place where October/Laravel/PDO/SQL converts null to empty string and how can I force it not to do so. I have tried with a lot of combinations like NULL, "NULL" and so on and they all seem not to work as I want to.

Hope I described it clearly. Thanks for help in advance.

Last updated

daftspunky
daftspunky

Is parent_id nullable in the database?

inIT
inIT

Yes it is. In migrating script I have added:

$table->integer('parent_id')->nullable();
$table->foreign('parent_id')->references('id')->on('rainlab_blog_categories');

And I can insert such row in phpPgAdmin without any problems.

Last updated

inIT
inIT

OK, as a workaround I have overloaded create_onSave method, but I do not consider this as a solution. Check this out:

public function create_onSave($context = null) {
    $arr = Input();
    $category = new Category();
    $category->name = $arr["Category"]["name"];
    $category->slug = $arr["Category"]["slug"];
    if(empty($arr["Category"]["parent_id"])) {
        $category->parent_id = NULL;
    } else {
        $category->parent_id = (int)$arr["Category"]["parent_id"];
    }
    $category->save();
    return $this->listRefresh();
}

1-4 of 4

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.