CakePHP 3-只允许在实体上保存特定字段的最简单方法

CakePHP 3-只允许在实体上保存特定字段的最简单方法,php,cakephp,cakephp-3.0,Php,Cakephp,Cakephp 3.0,在Cake 2中,您可以保存模型并指定要限制保存的字段 在蛋糕3中是否有一种内置的方法来实现这一点?例如,如果请求数据被直接放入一个新的实体中,然后被保存,我如何告诉该方法只保存我允许的字段 简化示例 // User makes a request; their POST data goes directly into the entity $customer = $this->Customers->newEntity($this->request->data); $th

在Cake 2中,您可以保存模型并指定要限制保存的字段

在蛋糕3中是否有一种内置的方法来实现这一点?例如,如果请求数据被直接放入一个新的实体中,然后被保存,我如何告诉该方法只保存我允许的字段

简化示例

// User makes a request; their POST data goes directly into the entity
$customer = $this->Customers->newEntity($this->request->data);
$this->Customers->save($customer);

这里明显的危险是,我可以通过请求在该客户实体上设置我喜欢的任何属性。实际上,我只想允许保存几个特定字段。

这就是批量分配保护的目的,它以
$\u可访问的
实体属性的形式存在

class Customer extends Entity
{
    // allow only `first_name` and `last_name` to be mass assigned
    protected $_accessible = [
        'first_name' => true,
        'last_name' => true
    ];
}
以及
表::newEntity/newEntities/patchEntity/patchEntities()的
字段列表
可访问字段
选项

accessibleFields
选项将仅更改指定字段的可访问性。此外,它还将实际修改实体,即与封送员仅将其用作白名单而不是实体默认值的
字段列表不同,
accessibleFields
将更改实体的值
$\u accessible
属性


这就是质量分配保护的目的,它以
$\u可访问的
实体属性的形式存在

class Customer extends Entity
{
    // allow only `first_name` and `last_name` to be mass assigned
    protected $_accessible = [
        'first_name' => true,
        'last_name' => true
    ];
}
以及
表::newEntity/newEntities/patchEntity/patchEntities()的
字段列表
可访问字段
选项

accessibleFields
选项将仅更改指定字段的可访问性。此外,它还将实际修改实体,即与封送员仅将其用作白名单而不是实体默认值的
字段列表不同,
accessibleFields
将更改实体的值
$\u accessible
属性


    • 有两种方法可以防止您遇到此问题。第一种方法是通过使用实体中的功能设置默认列,这些默认列可以从请求中安全设置

      _accessible属性允许您提供属性映射,以及它们是否可以批量指定。值true和false表示是否可以为场指定质量:

      第二种方法是在创建数据或将数据合并到实体时使用fieldList选项:

              // Contains ['user_id' => 100, 'title' => 'Hacked!'];
                $data = $this->request->data;
      
              // Only allow title to be changed
                $entity = $this->patchEntity($entity, $data, [
                        'fieldList' => ['title']
                       ]);
                 $this->save($entity);
      
      您还可以控制可以为关联指定哪些属性:

                  // Only allow changing the title and tags
                 // and the tag name is the only column that can be set
                    $entity = $this->patchEntity($entity, $data, [
                             'fieldList' => ['title', 'tags'],
                             'associated' => ['Tags' => ['fieldList' => ['name']]]
                             ]);
                    $this->save($entity);
      
      当您的用户可以访问许多不同的功能,并且希望让用户根据其权限编辑不同的数据时,使用此功能非常方便

      newEntity()、newEntities()和patchEntities()方法也接受fieldList选项

      更多信息:


      有两种方法可以防止您遇到此问题。第一种方法是通过使用实体中的功能设置默认列,这些默认列可以从请求中安全设置

      _accessible属性允许您提供属性映射,以及它们是否可以批量指定。值true和false表示是否可以为场指定质量:

      第二种方法是在创建数据或将数据合并到实体时使用fieldList选项:

              // Contains ['user_id' => 100, 'title' => 'Hacked!'];
                $data = $this->request->data;
      
              // Only allow title to be changed
                $entity = $this->patchEntity($entity, $data, [
                        'fieldList' => ['title']
                       ]);
                 $this->save($entity);
      
      您还可以控制可以为关联指定哪些属性:

                  // Only allow changing the title and tags
                 // and the tag name is the only column that can be set
                    $entity = $this->patchEntity($entity, $data, [
                             'fieldList' => ['title', 'tags'],
                             'associated' => ['Tags' => ['fieldList' => ['name']]]
                             ]);
                    $this->save($entity);
      
      当您的用户可以访问许多不同的功能,并且希望让用户根据其权限编辑不同的数据时,使用此功能非常方便

      newEntity()、newEntities()和patchEntities()方法也接受fieldList选项

      更多信息:


      为了阐明你的问题,你介意给我们一个你已经尝试过的例子吗?为什么只需要保存特定字段而不保存整个实体。“我习惯了条令,我不确定保存方法是否不同。”安德烈·雷纳更新了这个问题。由于来自用户的请求数据将直接用于保存实体,因此我希望通过此方法保护允许用户保存的字段。当然,我可以编写一些代码来检查并只保存这些字段,但在Cake 2中,保存时有一种简单的方法。在这种情况下,最好的方法是在将结果传递给控制器操作之前验证用户发布的结果。为了阐明您的问题,您介意给我们一个您已经尝试过的示例吗?为什么只需要保存特定字段而不保存整个实体。“我习惯了条令,我不确定保存方法是否不同。”安德烈·雷纳更新了这个问题。由于来自用户的请求数据将直接用于保存实体,因此我希望通过此方法保护允许用户保存的字段。当然,我可以编写一些代码来检查并只保存这些字段,但在Cake 2中,保存时有一种简单的方法。在这种情况下,最好的方法是在将结果传递给控制器操作之前验证用户发布的结果。