This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
Hi folks,
i'm playing around with the builder plugin and all in all i like it very much. It speeds up the development a lot. Thanks for that great plugin. Anyways I would like to know how to set up a relation within a plugin build with builder. I can't figure it out. Have I missed something? I only found a relationfield option in the models field.yaml. But i did not find any documentation on this. Did someone use the builderplugin already in this way?
The relations I would like to implement: a category belongs to many Questions and a Question has one category. Simple stuff for the normal way but I can't find a solution for the builder plugin.
Thanks in advance
i think you need to set up the relationship manually in your models, after that you can use the relation widget in builder
Okay I got it.... sometimes OctoberCMS is so easy to handle that I can't believe it :D For everybody: just use the relationfield and type in the column-name you want to relate too. I did not find a solution to write the relation into the modelclass so I had to do it manually in the file but it is not overwritten by Builder when changing other things of the model via GUI so it doesn't matter i think.
Cheers
Hello everybody!
Did not get to connect categories using the relation widget in Builder: http://prntscr.com/afeczi
It seems all right , but there is an error: http://prntscr.com/afebfc
In the model that's created such a code: http://prntscr.com/afecma
Boris said:
Okay I got it.... sometimes OctoberCMS is so easy to handle that I can't believe it :D For everybody: just use the relationfield and type in the column-name you want to relate too. I did not find a solution to write the relation into the modelclass so I had to do it manually in the file but it is not overwritten by Builder when changing other things of the model via GUI so it doesn't matter i think.
Cheers
hello! may i ask what you mean with just use the relationfield and type in the column-name you want to relate too.?
i am in the same situation and can't get how to solve it :\
TIA
i'm also stuck on this. a small explanation and/or working example would be very helpful.
I'm also stuck on this and get the same error as JTweb when trying to use the relation field in my fields.yaml
I have defined the relationships manually on the models for album and images, one album has many images.
// Album model
<?php namespace Acme\Library\Models;
use Model;
/**
* Model
*/
class Album extends Model
{
use \October\Rain\Database\Traits\Validation;
/*
* Validation
*/
public $rules = [
];
/*
* Disable timestamps by default.
* Remove this line if timestamps are defined in the database table.
*/
public $timestamps = false;
/**
* @var string The database table used by the model.
*/
public $table = 'acme_library_albums';
public $hasMany = [
'images' => 'Acme\Library\Models\Image'
];
}
// Image model
public $table = 'acme_library_images';
public $belongsTo = [
'albums' => 'Acme\Library\Models\Album'
];
}
}
Last updated
bump The builder is a great tool but without an easy way to implement relationships it's not very useful.
As the relationships have to be put in manually how does builder then query the relationships?
And what is the twig code to query the relationships?
If someone could give a quick example of how to do this then I would be happy to switch over to octobercms
Thanks!
There is a built-in media manager, you would be much better off leveraging that than to try to create your own.
Otherwise, you're probably going to have a very hard time creating a form with an image selection modal. At best you'll get to choose from a list of filenames, and I'm pretty sure you don't want that.
There appears to be no documentation on this feature yet, but I have seen people define $attachMany in models a few times, I think this is where you would start.
here is an example:
php namespace Pavlov\Market\Models;
use Model;
use October\Rain\Support\Markdown;
use October\Rain\Support\ValidationException;
use October\Rain\Database\Attach\File;
class Product extends Model
{
/*
* Relations
*/
public $belongsTo = [
'user' => ['Backend\Models\User']
];
public $belongsToMany = [
'categories' => ['Pavlov\Market\Models\Category', 'table' => 'pavlov_market_pr_cat', 'order' => 'name']
];
public $attachMany = [
'blend' => ['System\Models\File', 'order' => 'sort_order'],
'featured_images' => ['System\Models\File', 'order' => 'sort_order'],
];
}
The twig code for rendering relationships is simply
<?= $this->relationRender('image') ?>
This is defined all the way at the bottom of https://octobercms.com/docs/backend/relations which can be easy to miss.
Don't forget to add 'Backend\Behaviors\RelationController' to your controller's $implement and create a config_relations.yaml and define it on $model->relationConfig
Last updated
Thanks for the reply, I'm trying to create a relationship between 2 plugins made using the builder, albums and images. Albums has many images. I have defined the relationships in the models and I have create a config_relations.yaml file under my albums controller. I have the field album_id in the image table and want to use this to select which album the image goes into. I have created the relation field on the image plugin but keep getting an error as in my previous post.
looking at another example I presume that the below goes in update.htm file?
<?= $this->relationRender('image') ?>
I then get the error -
Call to undefined method Acme\Library\Controllers\Albums::relationRender()
C:\xampp\htdocs\octobercms\vendor\october\rain\src\Extension\ExtendableTrait.php line 371
It would appear you forgot to add 'Backend\Behaviors\RelationController' to your controller's $implement
public $implement = ['Backend\Behaviors\ListController', 'Backend\Behaviors\FormController', 'Backend\Behaviors\RelationController'];
Also, you need to create a file in your controller's view directory (next to update.htm) name it what you would like (config_relations.yaml is good) and include it in your controller with
public $relationConfig = 'config_relations.yaml';
Last updated
ok great so now my controller is :
<?php namespace Acme\Library\Controllers;
use Backend\Classes\Controller;
use BackendMenu;
use Acme\Library\Models\Album;
use Acme\Library\Models\Image;
class Albums extends Controller
{
public $implement = ['Backend\Behaviors\ListController', 'Backend\Behaviors\FormController', 'Backend\Behaviors\RelationController'];
public $listConfig = 'config_list.yaml';
public $formConfig = 'config_form.yaml';
public $relationConfig = 'config_relation.yaml';
public function __construct()
{
parent::__construct();
BackendMenu::setContext('Acme.Library','Library', 'Gallery');
}
}
and I have create config_relations.yaml
images:
label: image
view:
form: $/acme/library/models/image/fields.yaml
list: $/acme/library/models/image/columns.yaml
toolbarButtons: create|delete
manage:
form: $/acme/library/models/image/fields.yaml
recordsPerPage: 10
However i still get the error
Model 'Acme\Library\Models\Image' does not contain a definition for 'album_id'.
I have album_id as a relation field in albums, is there something else I'm missing?
Hmm... do you also try to define the relation in your model's form (fields.yaml)? I got a similar error when trying to define it there.
it seems that you have somewhere
relation: album_id
You do not have to define it there since you are calling a custom relationship render outside of your form.
Last updated
I have yes, i've used the relation field and added album_id here. So your saying I dont need to do this?
I'll try removing this from my images model and see if that works.
Only thing I'm not quite clear on is how I could then have a list field of album ids display on the form without using the relation field?
Last updated
calling $this->renderRelation('image') will generate a table of 'images'. Above this table will be the buttons you define in relationship_config.yaml.
toolbarButtons: create|delete
Will show a table with create and delete buttons above it. You may want to add the 'link' and 'remove' buttons to that as well. Link will allow you to select an existing 'image', 'remove' will remove the relation without deleting the image.
As far as how to get this table INSIDE the form, I.E. in a new tab, I'm still trying to figure out.
right now I call the relation first, so that it goes above the rest of the form instead of below the save buttons
= $this->relationRender('users') ?>
= $this->formRender() ?>
Last updated
I've removed the relation and added
<?=$this->renderRelation('image') ?>
to update.htm and to index.htm and both times i get the error below:
Call to undefined method Acme\Library\Controllers\Albums::relationRender() C:\xampp\htdocs\octobercms\vendor\october\rain\src\Extension\ExtendableTrait.php line 371
thanks for all your help, I've semi got it working. I now have the option to create an album when clicking on images so hopefully paying around with it a bit I can get it to list the albums by id and then put the image into the corresponding album
Thanks!