如果连接实体是新建的,如何保存其他数据以加入表

时间:2016-12-27 13:01:58

标签: cakephp cakephp-3.x

我正在尝试将其他数据保存到belongsToMany连接表。 我按照说明here 但它们只是在实体已经存在的情况下,因为似乎使用了id。但是我的实体应该是新创建的,并且应该保存其他连接数据。 我的保存数据如下所示。除了附加字段' type_keys'

之外,所有内容都保持良好状态
(int) 0 => object(Cloud\Model\Entity\MediaObject) {

        'media_object_type_id' => 'image',
        'title' => '1482842705_1_749145',
        'relative_path' => '/optional_images/1/1/',
        'extension' => 'jpg',
        'size' => (int) 142683,
        'original_title' => 'logo.jpg',
        '_joinData' => [
            'type_key' => 'optional_image_1'
        ],
        '[new]' => true,
        '[accessible]' => [
            '*' => true
        ],
        '[dirty]' => [
            'media_object_type_id' => true,
            'title' => true,
            'relative_path' => true,
            'extension' => true,
            'size' => true,
            'original_title' => true,
            '_joinData' => true
        ],
        '[original]' => [],
        '[virtual]' => [],
        '[errors]' => [],
        '[invalid]' => [],
        '[repository]' => 'Cloud.MediaObjects'

    }

不幸的是,只保存连接表中的连接ID,而不是joinData字段' type_keys'

如果有人能给我一些线索,我会很高兴。

在进一步测试时,我发现保存时连接数据会被覆盖。 有关您的信息:我在beforeSave回调中设置媒体对象连接数据。

object(Cloud\Model\Entity\Touchpoint) {

'title' => 'test',
'user_id' => (int) 1,
'tp_image' => [
    'name' => '',
    'type' => '',
    'tmp_name' => '',
    'error' => (int) 4,
    'size' => (int) 0
],
'optional_image_1' => [
    'name' => 'logo.jpg',
    'type' => 'image/jpeg',
    'tmp_name' => '/tmp/phpMeTwuQ',
    'error' => (int) 0,
    'size' => (int) 142683
],
'created' => object(Cake\I18n\Time) {

    'time' => '2016-12-27T13:19:03+00:00',
    'timezone' => 'UTC',
    'fixedNowTime' => false

},
'modified' => object(Cake\I18n\Time) {

    'time' => '2016-12-27T13:19:03+00:00',
    'timezone' => 'UTC',
    'fixedNowTime' => false

},
'brand_id' => (int) 1,
'media_objects' => [
    (int) 0 => object(Cloud\Model\Entity\MediaObject) {

        'media_object_type_id' => 'image',
        'title' => '1482844743_1_988232',
        'relative_path' => '/optional_images/1/1/',
        'extension' => 'jpg',
        'size' => (int) 142683,
        'original_title' => 'logo.jpg',
        '_joinData' => object(Cake\ORM\Entity) {

            'touchpoint_id' => (int) 8,
            'media_object_id' => (int) 8,
            '[new]' => false,
            '[accessible]' => [
                '*' => true
            ],
            '[dirty]' => [],
            '[original]' => [],
            '[virtual]' => [],
            '[errors]' => [],
            '[invalid]' => [],
            '[repository]' => 'MediaObjectsTouchpoints'

        },
        'created' => object(Cake\I18n\Time) {

            'time' => '2016-12-27T13:19:03+00:00',
            'timezone' => 'UTC',
            'fixedNowTime' => false

        },
        'modified' => object(Cake\I18n\Time) {

            'time' => '2016-12-27T13:19:03+00:00',
            'timezone' => 'UTC',
            'fixedNowTime' => false

        },
        'id' => (int) 8,
        '[new]' => false,
        '[accessible]' => [
            '*' => true
        ],
        '[dirty]' => [],
        '[original]' => [
            '_joinData' => [
                'type_key' => 'optional_image_1'
            ]
        ],
        '[virtual]' => [],
        '[errors]' => [],
        '[invalid]' => [],
        '[repository]' => 'Cloud.MediaObjects'

    }
],
'id' => (int) 8,
'[new]' => false,
'[accessible]' => [
    '*' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Cloud.Touchpoints'

}

所以我知道它会被覆盖,但我不确定如何以正确的方式做到这一点?

1 个答案:

答案 0 :(得分:1)

好的,我现在找到了解决方案。

因为我看到一个实体是作为_joinData创建的,所以我自己创建了一个实体,并且自己在实体中设置了属性,这样_joinData就不会被替换,而只是用id来丰富。

$joinTable = TableRegistry::get('MediaObjectsTouchpoints');
$newMediaObject->_joinData = $joinTable->newEntity();

$newMediaObject->_joinData->type_key = 'something';

$entity->media_objects[] = $newMediaObject;