HasOne 协会 table 未保存

HasOne associations table is not saving

您好,我正在尝试同时保存 2 tables(条目和条件)的数据。条目有一个名为 foreign_key 的列,它与条件主要 ID 相关。

我正在测试保存部分,我的一个 table(条件)保存了,但我的另一个 table(条目)没有保存。

Entry.php

protected $_accessible = [
        'metadata' => true,
        'type' => true,
        'foreign_key' => true,
        'created' => true,
        'modified' => true,
        'condition' => true
    ];

Condition.php

protected $_accessible = [
        'user_id' => true,
        'data' => true,
        'created' => true,
        'modified' => true,
        'user' => true,
        'entry' => true
    ];

ConditionsTable.php(我声明关联的地方)

public function initialize(array $config)
    {
        parent::initialize($config);

        $this->setTable('conditions');
        $this->setDisplayField('id');
        $this->setPrimaryKey('id');

        $this->addBehavior('Timestamp');

        $this->belongsTo('Users', [
            'foreignKey' => 'user_id',
            'joinType' => 'INNER'
        ]);

        $this->hasOne('Entries', [
            'foreignKey' => 'foreign_key',
            'bindingKey' => 'id',
            'propertyName' => 'entries',
            'joinType' => 'INNER'
        ]);
    }

DataControlellr.php(我正在测试节省的地方)


        $this->loadModel('Conditions');

        $data = [
            'user_id' => 'b26ee991-a27c-441b-a78b-dd2a1dbf5164',

            'data' => json_encode(['test'=>1,'test2' => 2]),
            'entry' =>[
                'meta' => json_encode(['test'=>1,'test2' => 2]),
                'type' => 'conditions'
            ]
        ];

        $entity = $this->Conditions->newEntity($data,['associated' => 'Entries']);
        //dd($entity);
        dd($this->Conditions->save($entity));
        exit;   
    }

所以再次条目 table 没有保存一行,条件是,我相信我使用了正确的关联(有一个)但也许这不是正确的逻辑?非常感谢您的帮助。

您数据中的键和您的关联名称需要匹配。您可能想将关联更改为:

$this->hasOne('Entries', [
    'foreignKey' => 'foreign_key',
    'bindingKey' => 'id',
    'joinType' => 'INNER'
]);

假设 fhir_entries table 的 class 被称为 EntriesTable,而不是 FhirEntriesTable

或者,保持关联名称不变,但将 propertyName 更改为 entryhasOne 属性应为单数)。或者,将其更改为 fhir_entry 并将数据数组中的键从 entry 更改为 fhir_entry 以匹配。