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

Utility
Utility

I am extending Rainlab\Blog\Models\Post.

I used post_id as primary key in the table, and did not use an extra id column.

Schema::create('ut_ut_extendposts', function($table)
    {
        $table->engine = 'InnoDB';
        $table->integer('post_id')->unsigned();
        $table->boolean('closed')->default(false);
        $table->primary('post_id');
        $table->timestamps();
    });

In the extended Post model:

public $belongsTo = [
    'post' => ['RainLab\Blog\Models\Post','post_id','id']
];

In Plugin.php

Post::extend(function($model) {
        $model->hasOne['extendpost'] = ['Ut\Ut\Models\Extendpost', 'id', 'post_id'];
    });

When I use the extend Post model (in backend list), the following error occurs:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'extendposts.id' in 'group statement' (SQL: select extendposts.*, rainlab_blog_posts.title as title from extendposts left join rainlab_blog_posts on rainlab_blog_posts.id = extendposts.post_id group by extendposts.id order by title desc)

The problem seems to be the 'group by' clause. How could I solve it without adding 'id' column to the table?

Last updated

daftspunky
daftspunky

I think the syntax should be:

Post::extend(function($model) {
    $model->hasOne['extendpost'] = ['Ut\Ut\Models\Extendpost', 'key' => 'post_id'];
});

It doesn't matter, simply tell the Model about your custom primary key:

class Extendpost {
    protected $primaryKey = 'post_id';
}

Last updated

Utility
Utility

Solved!

The syntax is a bit different from Laravel.

Thanks!

daftspunky
daftspunky

You're welcome. Yes the terminology is different to make it easier to understand. Laravel's terminology appears slightly confused.

Last updated

Utility
Utility

A followed problem:

Could not show properly the post title in the models\extendpost\fields.yaml

fields:
    post:
        label: Extended Post
        type: relation
        options:
            nameColumn: title

It shows a dropdown widget. Is the hasOne relation for form behavior still a TODO as http://octobercms.com/docs/backend/relations#has-one ?

Last updated

Utility
Utility

Ok, now I understand that, for hasOne relation, it renders a dropdown list, enabling you to reassign the extend model to another base model.

In my case, I just want to show title column from the base Post table, and does not allow modification/reassignment.

I ended up writting a formWidget to do this.

Last updated

Trymops
Trymops

Hey, I'm stuck with the hasOne relation, too. I get

Call to undefined method Backend\FormWidgets\Relation::makeFormWidget()

if i try to render a dropdown with

slider:
  tab: Edit
  label: Slider
  type: relation
  options:
    nameColumn: title
    emptyOption: No sliders available

Any idea why this error occurs?

Last updated

JimRed
JimRed

I have the same problem.
Could anyone help us?

Thanks

maxDubovsky
maxDubovsky

Same problem with: options: nameColumn: title emptyOption: No sliders available

daftspunky
daftspunky

You may wish to post a new topic.

1-10 of 10

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