【问题标题】:How to validate mandatory associated model in cakephp 3如何在 cakephp 3 中验证强制关联模型
【发布时间】:2017-02-07 03:52:55
【问题描述】:

我对确保在创建新用户时通过 POST 发送关联模型的正确(或最佳)方法感到困惑。下面列出的两种方法都有效。

User hasOne UserDetails

选项 1

发布数据:

{
    "username": "loremipsum",
    "password": "123456",
    "UserDetails": {
        "first_name": "Lorem",
        "last_name": "Ipsum"
    }
}

PatchEntity UserDetail 并将其添加到 User Entity 中,如下所示:

public function add () {

    $user = $this->Users->newEntity();

    if ($this->request->is('post')) {
        $user = $this->Users->patchEntity($user, $this->request->data(), [
            'associated' => [], 
            'validate' => true
        ]);
        $userDetail = $this->Users->UserDetails->newEntity($this->request->data());
        $user->user_detail = $userDetail;

    if ($this->Users->save($user, ['associated' =>['UserDetails']]))
    { 
...

编辑 1: 如果 $this->request->data() 中不存在 UserDetails,则 UserDetails 实体 将出现验证错误。

选项 2

发布数据:

{
    "username": "loremipsum",
    "password": "123456",
    "user_detail": {
        "first_name": "Lorem",
        "last_name": "Ipsum"
    }
}

在 UserTable.php 中添加 user_detail 验证:

编辑 2: 如果我不添加 user_detail 验证,则可以在没有关联模型的情况下发送请求并将其保存。添加它可以确保 $this->request->data() 上有一个 user_detail 字段,并且 Users 实体 是将获得验证的那个。

$validator
     ->requirePresence('user_detail', 'create')
     ->notEmpty('user_detail'); 

在 UsersController.php 上像这样的 patchEntity:

public function add () {

    $user = $this->Users->newEntity();

    if ($this->request->is('post')) {
        $user = $this->Users->patchEntity($user, $this->request->data(), [
            'associated' => ['UserDetails'], 
            'validate' => true
        ]);

    if ($this->Users->save($user, ['associated' =>['UserDetails']]))
    { 
...

这些方法是遵循 Cake 的约定还是有最好/干净的方法?

【问题讨论】:

  • 选项 2 是更好的选择。它会为您提供Users 和关联模型UserDetails 的验证错误。
  • @ObjectManipulator 感谢您的回复!实际上在这两种情况下验证都有效,因为验证在创建实体时运行。这里的主要区别是编写代码和发送数据的方式:)
  • 选项 1 是正确的 ..选项 2 不起作用它只是验证用户模型而不是 userdetails ..谢谢你真的很有帮助

标签: php cakephp cakephp-3.x


【解决方案1】:

第二个选项是正确的选项,原因有几个 IMO。

  1. 验证应该在它所属的模型上。
  2. 验证应该是表达性和明确的。如果我是另一个阅读此代码的人,我可以说保存用户需要验证。
  3. 更清晰,更不容易出错。
  4. 符合蛋糕惯例。我没有在他们的任何示例中看到您会保存一个然后保存另一个。 Saving Associations

【讨论】:

  • 我已经编辑了我的问题以使其清楚。在这两种情况下,数据都会一次保存;第一个只是单独添加 UserDetails 实体。问题是:要强制 UserDetails 出现在请求中,我应该使用我给出的第一个还是第二个选项?谢谢!
【解决方案2】:

如果你想验证多个模型,那么你需要根据表创建多个 newEntity。这里是 hasOne 关联的工作示例。

 public function add() {
        $user = $this->Users->newEntity();
        if ($this->request->is('post')) {
            $user = $this->Users->patchEntity($user, $this->request->getData());
            $userDetail = $this->UserDetails->newEntity($this->request->getData()['user_detail']);
            $user->user_detail = $userDetail;
            if ($this->Users->save($user)) {
                $this->Flash->success(__('The user has been saved.'));
                return $this->redirect($this->referer());
            } else {
                $this->Flash->error(__('The user could not be saved. Please, try again.'));
            }
        }
        $this->set(compact('user'));
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    • 1970-01-01
    • 1970-01-01
    • 2017-08-14
    • 1970-01-01
    相关资源
    最近更新 更多