Hello, I am trying to create a one-to-many relation between a Customer and a custom entity (Company).
One company can have multiple customers.
I created a new entity in MyPlugin/scr/Core/Content/Company
:
name;
}
/**
* @param string $name
*/
public function setName(string $name): void
{
$this->name = $name;
}
/**
* @return CustomerCollection
*/
public function getCustomers(): CustomerCollection
{
return $this->customers;
}
/**
* @param CustomerCollection $customers
*/
public function setCustomers(CustomerCollection $customers): void
{
$this->customers = $customers;
}
}
I also created a CompanyCollection.php
in the same folder:
I created a CompanyDefinition.php
in the same folder:
*/
protected function defineFields(): FieldCollection
{
return new FieldCollection(
[
(new IdField('id', 'id'))->addFlags(new PrimaryKey(), new Required()),
(new StringField('name', 'name'))->addFlags(new Required()),
new OneToManyAssociationField('customers', CustomerDefinition::class, 'company_id'),
]
);
}
}
In MyPlugin\MyPlugin\Core\Checkout\Customer
I created a CustomerExtension.php
:
add(
(new ManyToOneAssociationField(
'company',
'company_id',
CompanyDefinition::class,
'id'
))
);
}
public function getDefinitionClass(): string
{
return CustomerDefinition::class;
}
}
I created two migrations, the first one MyPlugin/scr/Migration
:
executeQuery($query);
}
public function updateDestructive(Connection $connection): void
{
}
}
And the customer extension migration:
updateInheritance($connection, 'customer', 'company_id');
}
public function updateDestructive(Connection $connection): void
{
}
}
This creates the neccesary database tables, and as far as I can tell this works fine.
Now I want to create a relation in the admin panel. I created a new section in the admin panel by creating a file in MyPlugin/src/Resources/admin/src/main.js
:
import './page/company-list';
import './page/company-detail';
Shopware.Module.register('my-plugin-company', {
type: 'plugin',
name: 'Custom',
title: 'Debtors',
description: 'Module to configure Companies in Shopware',
color: '#f12091',
icon: 'default-building-home',
routes: {
list: {
component: 'company-list',
path: 'list'
},
detail: {
component: 'company-detail',
path: 'detail/:id',
meta: {
parentPath: 'my.plugin.company.list'
}
}
},
navigation: [{
label: 'Companies',
color: '#f12091',
path: 'my.plugin.company.list',
icon: 'default-building-home',
position: -4
}]
});
I want to have a multisearch box on the company detail page, but nothing I tried seems to work. Here is my index.js
for the company detail page:
import template from './company-detail.html.twig';
const {Criteria} = Shopware.Data;
Shopware.Component.register('company-detail', {
template,
inject: [
'repositoryFactory'
],
metaInfo() {
return {
title: this.$createTitle()
};
},
data() {
return {
company: null,
repository: null,
};
},
created() {
this.repository = this.repositoryFactory.create('my_plugin_company');
this.getCompany();
},
computed: {
customerColumns() {
return [
{
property: 'lastName',
label: this.$tc('sw-customer.detailAddresses.columnLastName')
}, {
property: 'firstName',
label: this.$tc('sw-customer.detailAddresses.columnFirstName')
}
]
}
},
methods: {
getCompany() {
this.repository
.get(this.$route.params.id, Shopware.Context.api)
.then((entity) => {
this.company = entity;
});
}
}
});
And my template:
{% block my_plugin_company_detail %}
Customers
Now this doesnt work. Extra info i didnt include to prevent making this post any longer:
-
I added my custom definitions in my services.xml
-
I created the /public/administration/js/my-plugin.js file and the corresponding view, and rebuild all assets
Please tell me what I have to do to create a one to many relation in the admin panel. I want to be able to select a customer from the company detail page, and create a relation between the entities. Thanks!