微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

用于 underscore_named 模型的烘焙实体策略

如何解决用于 underscore_named 模型的烘焙实体策略

我有一个关于 Cake 对以 underscore_names 命名的模型/实体的策略的问题。

我有一个应用程序使用身份验证和授权中间件,但是在具有 two_word 名称的模型上烘焙实体和表的授权策略不会生成正确的代码(在我看来)。

我将尝试使用我在应用程序中创建的基本示例来简化问题以重现问题:

  1. 首先,我使用: CAKE_VERSION: 4.2.5,cakePHP/身份验证:2.0, cakePHP/授权:2.0, PHP:>=7.2,(另外,我在 Windows 10 上运行 XAMPP)

  2. 我在我的应用中创建了这两个简单的模型来演示这个问题:

CREATE TABLE `dogs` (
  `id` int(11) NOT NULL,`name` varchar(16) NOT NULL,`created` datetime NOT NULL,`modified` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=tis620;

CREATE TABLE `dog_traits` (
  `id` int(11) NOT NULL,`dog_id` int(11) NOT NULL,`trait` varchar(128) NOT NULL,`modified` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=tis620;
  1. 我为这两个模型烘焙了所有东西:
bin\cake bake all dogs
bin\cake bake policy --type entity dog
bin\cake bake policy --type table dogs

bin\cake bake all dog_traits
bin\cake bake policy --type entity dog_trait
bin\cake bake policy --type table dog_traits
  1. 对于 Dogs 模型,一切都按预期进行。 Dog 政策如下所示:
declare(strict_types=1);

namespace App\Policy;

use App\Model\Entity\dog;
use Authorization\IdentityInterface;

class dogPolicy
{
    /**
     * Check if $user can create dog
     *
     * @param Authorization\IdentityInterface $user The user.
     * @param App\Model\Entity\dog $dog
     * @return bool
     */
    public function canCreate(IdentityInterface $user,dog $dog)
    {   
        if ($user) {
            return true;
        }
        return false;       
    }
    // Etc....
}

然后在 Dogs 控制器中:

    public function add()
    {
        $dog = $this->Dogs->newEmptyEntity();
        
        $this->Authorization->authorize($dog,'create');
        
        if ($this->request->is('post')) {
        
        //Etc....

这按预期工作,没问题。

  1. 但是,对于我所有带有下划线名称的模型(例如 dog_traits),烘焙策略似乎无法正常工作。 认烘焙 DogTraitPolicy.PHP 如下所示:
declare(strict_types=1);

namespace App\Policy;

use App\Model\Entity\dog_trait;
use Authorization\IdentityInterface;

class dog_traitPolicy
{
    /**
     * Check if $user can create dog_trait
     *
     * @param Authorization\IdentityInterface $user The user.
     * @param App\Model\Entity\dog_trait $dogTrait
     * @return bool
     */
    public function canCreate(IdentityInterface $user,dog_trait $dogTrait)
    {
        if ($user) {
            return true;
        }
        return false;  
    }
    //ETC....
}

然后在 DogTraits 控制器中:

    public function add()
    {
        $dogTrait = $this->DogTraits->newEmptyEntity();
        
        $this->Authorization->authorize($dogTrait,'create');
        
        if ($this->request->is('post')) {

        //Etc....

所以现在 URL http://localhost/myapp/dog-traits/add 抛出错误Policy for App\Model\Entity\DogTrait has not been defined.

如果我回到 DogTraitPolicy.PHP 并将类名更改为 class dogTraitPolicy(驼峰式而不是下划线),我会收到错误消息: Argument 2 passed to App\Policy\dogTraitPolicy::canCreate() must be an instance of App\Model\Entity\dog_trait,instance of App\Model\Entity\DogTrait given,called in C:\xampp\htdocs\myapp\vendor\cakePHP\authorization\src\AuthorizationService.PHP on line 93

如果我将 canCreate 方法更改为此(注意驼峰式参数名称):

    public function canCreate(IdentityInterface $user,dogTrait $dogTrait)
    {
        if ($user) {
            return true;
        }
        return false;  
    } 

然后返回错误Argument 2 passed to App\Policy\dogTraitPolicy::canCreate() must be an instance of App\Policy\dogTrait,called in C:\xampp\htdocs\myapp\vendor\cakePHP\authorization\src\AuthorizationService.PHP on line 93

如果我随后在 $dogTrait 之前删除参数名称 dogTrait,则策略按预期授权。

    public function canCreate(IdentityInterface $user,$dogTrait)
    {
        if ($user) {
            return true;
        }
        return false;  
    }

所以问题是:实体策略烘焙对于带下划线的模型名称是否正常工作?它适用于单字模型,开箱即用,但不适用于 underscored_names。我错过了什么吗?

提前感谢您的建议。

解决方法

使用带有“大小写”类名而不是带下划线的表名的烘焙命令,例如:

bin\cake bake all DogTraits
bin\cake bake policy --type entity DogTrait
bin\cake bake policy --type table DogTraits

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?