Problems with multiple hasOne and DropDown-Field

Hello,
I am very very new to AgileToolkit and before asking the community I try to read and understand other postings. But, - since I am really new to the framework - , I am stuck:

Problem:
I have three entities

  • mandant
  • kontakt
  • adresse

Both kontakt and adresse have a HASONE relationship to mandant.
An adresse has a HASONE relationship to kontakt as well.
So when I use CRUD to edit a adresse it shows a dropdown to choose a kontakt but not only those in the same mandant.

My classes:

class Mandant extends \atk4\data\Model {
    public $table = 'mandant';

    function init()
    {
       $this->hasMany('Kontakt', new Kontakt());
        $this->hasMany('Adresse', new Adresse());
.....

class Kontakt extends \atk4\data\Model {
    public $table = 'kontakt';
    public $title_field = 'Name_lang';
    function init()
    {
        $this->hasOne('mandant_id', new Mandant());        
.....
class Adresse extends \atk4\data\Model {
    public $table = 'adresse';

    function init()
    {
        $this->hasOne('mandant_id', new Mandant());
        $this->hasOne('kontakt_id', new Kontakt());

My very basic page looks like this

<?php
require'libapp.php';
$app = new MyApp();
$crud = $app->add(['CRUD','ipp'=>10]);
$crud->setModel($app->mandant->ref('Adresse'));

Question: How can a dropdown field show only those kontakt-values from the same mandant as the adress belong to.
I would expect something like
$crud->*field->dropdown->kontakt_id->*setmodel($app->mandant->ref(‘Kontakt’));

Anyone can help?

I’ll make my first attempt to return some of the help I’ve been given. I’m far from an expert in ATK, but I can see that the issue is that you haven’t set up the Adresse model to restrict the kontacts to those that match the mandant – your two hasOnes are completely independent.

You should replace your $this->hasOne(‘kontact_id’, new Kontakt()) line in your Adresse init function with something like:

$this->hasOne(‘kontakt_id’, [function ($m)
{
return ($m->ref(‘mandant_id’)->ref(‘Kontakt’));
}]);

That’s how I accomplish similar things in my applications.

Thank you very much, it works.