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

Rob Ballantyne
Rob Ballantyne

Hi, I hope someone can help. I am struggling to understand how to configure my forms correctly.

I have several models: product, category, link, linkgroup. A PRODUCT belongs to many CATEGORIES.
A PRODUCT has many LINKS. A LINKGROUP has many LINKS (not sure if this is correct or if a link should have one linkgroup but I can figure this out later.) I want to have product as the master, so when I click create I am presented with my product fields (I've got this, it's easy) but I also want to be able to click a button to add a new link (ie one which does not exist yet). This is where I am stuck. I have defined my relationship through the product controller (is this right?) and I have attempted to add a field to my product fields.yaml in the following way:

links:
    tab: Detail
    label: Add link
    type: relation

This does not work at all and throws an exception - Call to undefined method Backend\FormWidgets\Relation::makeFormWidget() I assume I am doing something wrong, I just don't know what. I can manage the product/category relationship just fine because the categories already exist, I just don't know how to make the form work for creating relationships as I go.

Sorry if this is straightforward and I have failed to read some documentation properly, but I have been progressing very well with my first October plugin until this point and it'll drive me mad if I don't get my head around it soon.

Cheers, Rob

d.negativa
d.negativa

Relationships must be defined in the model
Documentatiion
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'],
    ];
}

Last updated

Rob Ballantyne
Rob Ballantyne

Thanks, but I have defined the relationship per the documentation. What I am looking for is further information regarding how to use these relationship in backend forms. I know how to use the form to, for example, create a drop down to match a product to an existing category using a pivot table but what I don't understand is how to create a brand new link(model) to bind to the product from within the product form.

Last updated

d.negativa
d.negativa

Oh, I'm just now trying to deal with this also!
Here that at the moment I got:

relations-result

and code:

relations-code

Last updated

Rob Ballantyne
Rob Ballantyne

Thank you this is really helpful. I'll update later with any progress I make

Rob Ballantyne
Rob Ballantyne

I have this working now it was the relationRender that I was missing. What I have done differently, however, is place that in a partial which is then called from the fields.yaml so my links can have their own tab. Currently this does not work on create and only on update. This, presumably, is because I have not yet dealt with deferred binding.

planetadeleste
planetadeleste

@rob2068 I have the same problem. Thanks to this post I could solve it partially. I try to write the relationRender in a partial inside of a tab. But I receive an Exception

Maximum function nesting level of '100' reached, aborting!

I don't understand what is wrong. This is the partial content.

<?php
$isCreate = $this->formGetContext() == 'create';
if(!$isCreate):
    $this->relationRender('address');
else: ?>
    <?= $this->makeHintPartial(null, 'hint_tab_address', ['icon' => 'fa fa-info']) ?>
<?php endif; ?>

If I write <?= $this->relationRender('address') ?> in update.htm it works perfect.

Thanks.

Last updated

deroccha
deroccha

I try the same but I get error Controllers\Shops does not have a method definition for relationRender

BMCouto
BMCouto

Have you considered to share that plugin with the community? Looks nice!

deroccha
Cpt.Meatball
Cpt.Meatball

rob@dynamedia.co.uk said:

I have this working now it was the relationRender that I was missing. What I have done differently, however, is place that in a partial which is then called from the fields.yaml so my links can have their own tab. Currently this does not work on create and only on update. This, presumably, is because I have not yet dealt with deferred binding.

Were your presumptions right? Currently dealing with this myself. Update works fine, but onCreate it fails on a SQL#1452. The constraint fails for some reason.

GreenImp
GreenImp

Cpt.Meatball said:

rob@dynamedia.co.uk said:

I have this working now it was the relationRender that I was missing. What I have done differently, however, is place that in a partial which is then called from the fields.yaml so my links can have their own tab. Currently this does not work on create and only on update. This, presumably, is because I have not yet dealt with deferred binding.

Were your presumptions right? Currently dealing with this myself. Update works fine, but onCreate it fails on a SQL#1452. The constraint fails for some reason.

I've hit the same problem. Did either of you find a solution? I tried setting deferredBinding: true in the relation config, so I have:

contact:
  label: Contacts
  emptyMessage: No contacts
  deferredBinding: true
  view:
    ...

But I still get the error.

keonovic
keonovic

@GreenImp I had the same problem! Finally found the solution - make sure your controller has the 'Backend.Behaviors.RelationController' added to the $implement array, also make sure you define a public $relationConfig pointing to your config_relation.yaml file. I found this out from the start of the relations section in the docs: https://octobercms.com/docs/backend/relations

GreenImp
GreenImp

Thanks keonovic, I've already got that in. Basically, I have a list of offices, and contacts for the office. It works fine if I try adding a related contact to an existing office, however, if I try and add contacts to a new office, which hasn't been saved yet, it throws the error because the office doesn't exist.

I was hoping the deferredBinding: true flag would tell it not to try and create the contacts until the office has been saved.

Renatio
Renatio

Do you have the relation field in database nullable?

In migration file for office contacts:

$this->integer('office_id')->unsigned()->index()->nullable();
GreenImp
GreenImp

No, it's a non-nullable foreign key. I suppose making it nullable would probably work around the issue, but the field really shouldn't be nullable. In the project a contact cannot exist without a corresponding office (It's basically just an email or phone number). If it's nullable then I could end up with random contacts that don't relate to anything, and no way of managing them; there is no separate contacts controller.

Do you know if the deferredBinding: true should do the trick? Am I using it wrong?

Renatio
Renatio

I think that how October works. It will create contact record with office_id as null. After you save office it will update this office_id field.

I know that if you create some contacts and don't save office you will end up with records not assign to any office.

I don't know any other way though.

GreenImp
GreenImp

Hmm.. that could lead to a lot of orphaned data, which isn't ideal. I'll have a play with doing it like that, alongside deferred binding and see what happens.

vince_o
vince_o

@GreenImp I'm curious if you got this sorted out. Did the approach suggested by @Renatio work? Thanks.

Surahman
Surahman

I have same problem with defered binding.

1-20 of 22

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