1

I am building a relation System and I have 3 Tables in my database:

  • Relations: A list of companies
  • Persons: Staff members in a company
  • Opportunities: A list of opportunities

An Opportunity has a relation id as foreignKey, and a person has a relation_id too.

I linked my Tables on this way:

Opportunities:

$this->belongsTo('Relations', [
        'className' => 'Relations',
        'foreignKey' => 'relation_id',
        'joinType' => 'LEFT',
    ]);

Persons:

$this->belongsTo('Relations', [
        'className' => 'Relations',
        'foreignKey' => 'relation_id',
        'joinType' => 'LEFT',
    ]);

Relations:

$this->hasMany('Opportunities');
$this->hasMany('Persons');

When I add a new Opportunity, Cakephp make a new relation at the same time, but the person will not be created.

When I debug the patchEntity, I get this as result:

object(Cake\ORM\Entity) {

'statusname' => 'Openstaand',
'title' => 'XX',
'budget' => (float) 123,
'type' => 'nieuw',
'relation_id' => (int) 56,
'relation' => object(App\Model\Entity\Relation) {

    'name' => 'XX',
    'person' => [
        'salutation' => 'Dhr.'
    ],
    '[new]' => true,
    '[accessible]' => [
        '*' => true
    ],
    '[dirty]' => [
        'name' => true,
        'person' => true
    ],
    '[original]' => [],
    '[virtual]' => [],
    '[errors]' => [],
    '[invalid]' => [],
    '[repository]' => 'Relations'

},
'person' => [
    'firstname' => 'XX',
    'insertion' => 'XXX',
    'surname' => 'XXXX',
    'phone' => 'XXXX',
    'email' => 'XXXX@a.be'
],
'status_id' => (int) 13,
'chance' => (int) 0,
'source' => '',
'[new]' => true,
'[accessible]' => [
    '*' => true
],
'[dirty]' => [
    'statusname' => true,
    'title' => true,
    'budget' => true,
    'type' => true,
    'relation_id' => true,
    'relation' => true,
    'person' => true,
    'status_id' => true,
    'chance' => true,
    'source' => true
],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Opportunities'

}

The Persons part of my form is set up like this:

<section class="form-group--left">
                <span class="global-label"><?= __("Bedrijf"); ?></span>
                <section class="control-group">
                    <?= $this->Form->input('relation.name', ['class' => 'text-field', 'label' => false]); ?>
                </section>
</section>

            <section class="form-group--left">
                <span class="global-label"><?= __('Lead'); ?></span>
                <section class="control-group">
                    <?= $this->Form->input('person.salutation', [
                        'label' => __('Aanhef'),
                        'empty' => false,
                        'options' => [['value' => "Dhr.", 'text' => __("Dhr.")], ['value' => "Mevr.", 'text' => __("Mevr.")]],
                        'class' => 'text-field'
                    ]); ?>
                </section>
                <section class="control-group">
                    <?= $this->Form->input('person.firstname', ['class' => 'text-field', 'label' => __("Voornaam")]); ?>
                </section>
                <section class="control-group">
                    <?= $this->Form->input('person.insertion', ['class' => 'text-field', 'label' => __("Tussenvoegsel")]); ?>
                </section>
                <section class="control-group">
                    <?= $this->Form->input('person.surname', ['class' => 'text-field', 'label' => __("Achternaam")]); ?>
                </section>
            </section>
            <section class="form-group--left">
                <span class="global-label"><?= __("Contact"); ?></span>
                <section class="control-group">
                    <?= $this->Form->input('person.phone', ['class' => 'text-field', 'label' => __("Telefoon")]); ?>
                </section>
                <section class="control-group">
                    <?= $this->Form->input('person.email', ['class' => 'text-field', 'label' => __("E-mailadres")]); ?>
                </section>
            </section>

I have also tried to solve my problem with this information, but in the example all the tables has linked with a product number, and I don't have a link in persons with opportunities.

Can anyone see what I do wrong?

0 Answers0