CONTRANCE6: MULTIPLE AUTOCOMPLETE BEETWEEN TWO MODELS

created: 14.01.2019 12:21

edited: 18.01.2019 14:20

back

Inside the model that has many, belongs to many:
contrance/Plugin/<Pluginname>/Model/<Modelname>

php
App::uses('TariffCalculatorsAppModel', 'TariffCalculators.Model');

class TariffCalculatorZipCode extends TariffCalculatorsAppModel
{


    public $hasAndBelongsToMany = [
        // Define an alias
        'TariffCalculatorElectricityTypeTariffCalculatorZipCode' => [
            // Declare the class name
            'className' => 'TariffCalculatorElectricityType',
        ],
    ];

    public $containments = [
        // Contain the alias to gain the data from your relationship
        'TariffCalculatorElectricityTypeTariffCalculatorZipCode',
    ];

    public $validate = [
        'zip_code' => [
            'rule'    => 'notBlank',
            'message' => 'Dieses Feld ist ein Pflichtfeld',
        ],
        'city'     => [
            'rule'    => 'notBlank',
            'message' => 'Dieses Feld ist ein Pflichtfeld',
        ],
    ];
}

contrance/Plugin/<Pluginname>/View/<Pluginname>/edit.php

php
$this->extend('BaseData.BaseData/edit');

$this->append('form');

echo $this->AceForm->create(null, ['class' => 'form-horizontal']);

// Tabs
$this->AceTab->startTab(__d('app', 'Allgemein'));
echo $this->AceForm->hidden('id');
echo $this->AceForm->input('zip_code', ['type' => 'text', 'label' => ['text' => __d('app', 'Postleitzahl')]]);
echo $this->AceForm->input('city', ['type' => 'text', 'label' => ['text' => __d('app', 'Ort')]]);
echo $this->AceForm->input(
    'TariffCalculatorElectricityTypeTariffCalculatorZipCode',
    [
        'type'         => 'autocomplete',
        'multiple'     => true,
        'autocomplete' => [
            'url' => [
                'controller' => 'tariff_calculator_electricity_types',
                'plugin'     => 'tariff_calculators',
                'action'     => 'autocomplete',
            ],
        ],
        'label'        => ['text' => __d('app', 'G├╝ltige Tarife')],
    ]
);
$this->AceTab->endTab();

echo $this->AceTab->renderTabs();
echo $this->element('BaseData.formButtons');
echo $this->AceForm->end();

$this->end();