CakePHP 3.1 patchEntity在清理时始终将belongsToMany关联标记为脏

时间:2015-12-17 15:16:40

标签: php cakephp cakephp-3.1

所以我注意到如果我修补了一个实体(编辑方法),并且如果它具有belongsToMany关联,我是否对记录进行了任何数据更改,它将它们标记为脏。我希望如果我在视图中没有对BTM多次选择进行任何更改,那么数据就不会变脏,只有在多次选择中添加或删除选项时才会在修补后将其标记为脏。

数据确实保存正确,它只是脏,但我需要知道它是否脏或干净,因为我在地图表中有_join数据。 map表名为users_locations,有id,user_id,location_id和static,其中static是tinyint / bool。

我要做的是仅为新创建的地图表条目标记静态。

我注意到patchEntity正在将_joinData作为编组过程的一部分进行剥离。

因此,查看下面的调试输出,您可以看到在修补位置和user_occupations之后,_joinData被剥离。

这似乎不适合我不知道相关数据是干净还是脏。也许它打算以这种方式工作,我错过了一些东西。想法?

在编辑表格中我有:

<?php echo $this->Form->input('locations._ids', ['options' => $locations]) ?>

在控制器中我有:

<?php
    public function edit($id = null)
    {
        $user = $this->Users->get($id, [
            'contain' => ['Locations', 'UserOccupations']
        ]);
        if ($this->request->is(['patch', 'post', 'put'])) {
            $user = $this->Users->patchEntity($user, $this->request->data);
            if ($this->Users->save($user)) {
                $this->Flash->success(__('The user has been saved.'));
                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error(__('The user could not be saved. Please, try again.'));
            }
        }
        $securityGroups = $this->Users->SecurityGroups->find('list');
        $locations = $this->Users->Locations->find('list', [
            'order' => ['Locations.name' => 'ASC'],
            'keyField' => 'id',
            'valueField' => 'name',
            'limit' => 200
        ]);
        $userOccupations = $this->Users->UserOccupations->find('list');
        $this->set(compact('user', 'securityGroups', 'locations', 'userOccupations'));
        $this->set('_serialize', ['user']);
    }
?>

在模型中,我在User的初始化函数中有这个:

$this->belongsToMany('Locations', [
    'through' => 'Users.UsersLocations',
    'foreignKey' => 'user_id',
    'targetForeignKey' => 'location_id',
    'className' => 'Locations.Locations'
]);

这是请求数据调试输出:

[
    'Referer' => [
        'url' => '/login'
    ],
    'security_group_id' => '',
    'username' => 'test',
    'email' => 'test@test.com',
    'prefix' => '',
    'first_name' => 'test',
    'middle_name' => '',
    'last_name' => 'test',
    'suffix' => '',
    'credentials' => '',
    'birthdate' => '',
    'timezone' => 'America/New_York',
    'theme' => '',
    'locations' => [
        '_ids' => [
            (int) 0 => '7',
            (int) 1 => '33'
        ]
    ],
    'user_occupations' => [
        '_ids' => [
            (int) 0 => '1'
        ]
    ]
]

这是在使用请求数据进行修补之前的用户实体:

object(Users\Model\Entity\User) {

    'id' => '8b7197a4-5633-4bda-a6c7-a6e16f7cad64',
    'identifier' => (int) 5,
    'security_group_id' => null,
    'sex_id' => null,
    'username' => 'test',
    'email' => 'test@test.com',
    'prefix' => '',
    'first_name' => 'test',
    'middle_name' => '',
    'last_name' => 'test',
    'suffix' => '',
    'credentials' => '',
    'birthdate' => null,
    'timezone' => 'America/New_York',
    'theme' => '',
    'ip' => '0.0.0.0',
    'last_login' => null,
    'created' => object(Cake\I18n\Time) {

        'time' => '2015-09-16T16:17:57+0000',
        'timezone' => 'UTC',
        'fixedNowTime' => false

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

        'time' => '2015-12-16T22:22:49+0000',
        'timezone' => 'UTC',
        'fixedNowTime' => false

    },
    'user_occupations' => [
        (int) 0 => object(Users\Model\Entity\UserOccupation) {

            'id' => (int) 1,
            'name' => 'Test',
            '_joinData' => object(Cake\ORM\Entity) {

                'user_occupation_id' => (int) 1,
                'user_id' => '8b7197a4-5633-4bda-a6c7-a6e16f7cad64',
                '[new]' => false,
                '[accessible]' => [
                    '*' => true
                ],
                '[dirty]' => [],
                '[original]' => [],
                '[virtual]' => [],
                '[errors]' => [],
                '[repository]' => 'UsersUserOccupations'

            },
            '[new]' => false,
            '[accessible]' => [
                '*' => true
            ],
            '[dirty]' => [],
            '[original]' => [],
            '[virtual]' => [],
            '[errors]' => [],
            '[repository]' => 'Users.UserOccupations'

        }
    ],
    'locations' => [
        (int) 0 => object(Locations\Model\Entity\Location) {

            'id' => (int) 7,
            'ldap_name' => 'Test',
            'name' => 'Test',
            'address' => null,
            'address_2' => null,
            'city' => 'Test',
            'state' => 'MD',
            'zip' => null,
            'phone' => null,
            'fax' => null,
            'active' => true,
            'created' => object(Cake\I18n\Time) {

                'time' => '2015-09-11T19:35:34+0000',
                'timezone' => 'UTC',
                'fixedNowTime' => false

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

                'time' => '2015-12-16T21:47:29+0000',
                'timezone' => 'UTC',
                'fixedNowTime' => false

            },
            '_joinData' => object(Users\Model\Entity\UsersLocation) {

                'location_id' => (int) 7,
                'id' => (int) 304,
                'user_id' => '8b7197a4-5633-4bda-a6c7-a6e16f7cad64',
                'static' => false,
                '[new]' => false,
                '[accessible]' => [
                    '*' => true
                ],
                '[dirty]' => [],
                '[original]' => [],
                '[virtual]' => [],
                '[errors]' => [],
                '[repository]' => 'Users.UsersLocations'

            },
            '[new]' => false,
            '[accessible]' => [
                '*' => true
            ],
            '[dirty]' => [],
            '[original]' => [],
            '[virtual]' => [],
            '[errors]' => [],
            '[repository]' => 'Locations.Locations'

        },
        (int) 1 => object(Locations\Model\Entity\Location) {

            'id' => (int) 33,
            'ldap_name' => 'Test2',
            'name' => 'Test2',
            'address' => null,
            'address_2' => null,
            'city' => 'Test',
            'state' => 'MD',
            'zip' => null,
            'phone' => null,
            'fax' => null,
            'active' => true,
            'created' => object(Cake\I18n\Time) {

                'time' => '2015-09-15T21:03:46+0000',
                'timezone' => 'UTC',
                'fixedNowTime' => false

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

                'time' => '2015-12-16T21:47:29+0000',
                'timezone' => 'UTC',
                'fixedNowTime' => false

            },
            '_joinData' => object(Users\Model\Entity\UsersLocation) {

                'location_id' => (int) 33,
                'id' => (int) 305,
                'user_id' => '8b7197a4-5633-4bda-a6c7-a6e16f7cad64',
                'static' => false,
                '[new]' => false,
                '[accessible]' => [
                    '*' => true
                ],
                '[dirty]' => [],
                '[original]' => [],
                '[virtual]' => [],
                '[errors]' => [],
                '[repository]' => 'Users.UsersLocations'

            },
            '[new]' => false,
            '[accessible]' => [
                '*' => true
            ],
            '[dirty]' => [],
            '[original]' => [],
            '[virtual]' => [],
            '[errors]' => [],
            '[repository]' => 'Locations.Locations'

        },
    ],
    '[new]' => false,
    '[accessible]' => [
        '*' => true
    ],
    '[dirty]' => [],
    '[original]' => [],
    '[virtual]' => [
        (int) 0 => 'full_name',
        (int) 1 => 'name_last_first'
    ],
    '[errors]' => [],
    '[repository]' => 'Users.Users'

}

这是使用请求数据修补后调试输出的样子:

object(Users\Model\Entity\User) {

    'id' => '8b7197a4-5633-4bda-a6c7-a6e16f7cad64',
    'identifier' => (int) 5,
    'security_group_id' => null,
    'sex_id' => null,
    'username' => 'test',
    'email' => 'test@test.com',
    'prefix' => '',
    'first_name' => 'test',
    'middle_name' => '',
    'last_name' => 'test',
    'suffix' => '',
    'credentials' => '',
    'birthdate' => null,
    'timezone' => 'America/New_York',
    'theme' => '',
    'ip' => '0.0.0.0',
    'last_login' => null,
    'created' => object(Cake\I18n\Time) {

        'time' => '2015-09-16T16:17:57+0000',
        'timezone' => 'UTC',
        'fixedNowTime' => false

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

        'time' => '2015-12-16T22:22:49+0000',
        'timezone' => 'UTC',
        'fixedNowTime' => false

    },
    'user_occupations' => [
        (int) 0 => object(Users\Model\Entity\UserOccupation) {

            'id' => (int) 1,
            'name' => 'Test ',
            '[new]' => false,
            '[accessible]' => [
                '*' => true
            ],
            '[dirty]' => [],
            '[original]' => [],
            '[virtual]' => [],
            '[errors]' => [],
            '[repository]' => 'Users.UserOccupations'

        }
    ],
    'locations' => [
        (int) 0 => object(Locations\Model\Entity\Location) {

            'id' => (int) 7,
            'ldap_name' => 'Test',
            'name' => 'Test',
            'address' => null,
            'address_2' => null,
            'city' => 'Test',
            'state' => 'MD',
            'zip' => null,
            'phone' => null,
            'fax' => null,
            'active' => true,
            'created' => object(Cake\I18n\Time) {

                'time' => '2015-09-11T19:35:34+0000',
                'timezone' => 'UTC',
                'fixedNowTime' => false

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

                'time' => '2015-12-16T21:47:29+0000',
                'timezone' => 'UTC',
                'fixedNowTime' => false

            },
            '[new]' => false,
            '[accessible]' => [
                '*' => true
            ],
            '[dirty]' => [],
            '[original]' => [],
            '[virtual]' => [],
            '[errors]' => [],
            '[repository]' => 'Locations.Locations'

        },
        (int) 1 => object(Locations\Model\Entity\Location) {

            'id' => (int) 33,
            'ldap_name' => 'Test2',
            'name' => 'Test2',
            'address' => null,
            'address_2' => null,
            'city' => 'Test',
            'state' => 'MD',
            'zip' => null,
            'phone' => null,
            'fax' => null,
            'active' => true,
            'created' => object(Cake\I18n\Time) {

                'time' => '2015-09-15T21:03:46+0000',
                'timezone' => 'UTC',
                'fixedNowTime' => false

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

                'time' => '2015-12-16T21:47:29+0000',
                'timezone' => 'UTC',
                'fixedNowTime' => false

            },
            '[new]' => false,
            '[accessible]' => [
                '*' => true
            ],
            '[dirty]' => [],
            '[original]' => [],
            '[virtual]' => [],
            '[errors]' => [],
            '[repository]' => 'Locations.Locations'

        }
    ],
    '[new]' => false,
    '[accessible]' => [
        '*' => true
    ],
    '[dirty]' => [
        'locations' => true,
        'user_occupations' => true
    ],
    '[original]' => [
        'locations' => [
            (int) 0 => object(Locations\Model\Entity\Location) {

                'id' => (int) 7,
                'ldap_name' => 'Test',
                'name' => 'Test',
                'address' => null,
                'address_2' => null,
                'city' => 'Test',
                'state' => 'MD',
                'zip' => null,
                'phone' => null,
                'fax' => null,
                'active' => true,
                'created' => object(Cake\I18n\Time) {

                    'time' => '2015-09-11T19:35:34+0000',
                    'timezone' => 'UTC',
                    'fixedNowTime' => false

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

                    'time' => '2015-12-16T21:47:29+0000',
                    'timezone' => 'UTC',
                    'fixedNowTime' => false

                },
                '_joinData' => object(Users\Model\Entity\UsersLocation) {

                    'location_id' => (int) 7,
                    'id' => (int) 304,
                    'user_id' => '8b7197a4-5633-4bda-a6c7-a6e16f7cad64',
                    'static' => false,
                    '[new]' => false,
                    '[accessible]' => [
                        '*' => true
                    ],
                    '[dirty]' => [],
                    '[original]' => [],
                    '[virtual]' => [],
                    '[errors]' => [],
                    '[repository]' => 'Users.UsersLocations'

                },
                '[new]' => false,
                '[accessible]' => [
                    '*' => true
                ],
                '[dirty]' => [],
                '[original]' => [],
                '[virtual]' => [],
                '[errors]' => [],
                '[repository]' => 'Locations.Locations'

            },
            (int) 1 => object(Locations\Model\Entity\Location) {

                'id' => (int) 33,
                'ldap_name' => 'Test2',
                'name' => 'Test2',
                'address' => null,
                'address_2' => null,
                'city' => 'Test',
                'state' => 'MD',
                'zip' => null,
                'phone' => null,
                'fax' => null,
                'active' => true,
                'created' => object(Cake\I18n\Time) {

                    'time' => '2015-09-15T21:03:46+0000',
                    'timezone' => 'UTC',
                    'fixedNowTime' => false

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

                    'time' => '2015-12-16T21:47:29+0000',
                    'timezone' => 'UTC',
                    'fixedNowTime' => false

                },
                '_joinData' => object(Users\Model\Entity\UsersLocation) {

                    'location_id' => (int) 33,
                    'id' => (int) 305,
                    'user_id' => '8b7197a4-5633-4bda-a6c7-a6e16f7cad64',
                    'static' => false,
                    '[new]' => false,
                    '[accessible]' => [
                        '*' => true
                    ],
                    '[dirty]' => [],
                    '[original]' => [],
                    '[virtual]' => [],
                    '[errors]' => [],
                    '[repository]' => 'Users.UsersLocations'

                },
                '[new]' => false,
                '[accessible]' => [
                    '*' => true
                ],
                '[dirty]' => [],
                '[original]' => [],
                '[virtual]' => [],
                '[errors]' => [],
                '[repository]' => 'Locations.Locations'

            },
        ]
    ],
    '[virtual]' => [
        (int) 0 => 'full_name',
        (int) 1 => 'name_last_first'
    ],
    '[errors]' => [],
    '[repository]' => 'Users.Users'

}

1 个答案:

答案 0 :(得分:1)

请在https://github.com/cakephp/cakephp/issues中打开一个问题。

谢谢!