Php RedBean:已经为此密钥指定了数据库

Php RedBean:已经为此密钥指定了数据库,php,redbean,Php,Redbean,我和红豆有点问题。我遇到了一个连接错误,无法理解这是什么意思,我已经在谷歌上搜索过,找不到任何有效的解决方案,我在这里尝试了一个类似的问题,但答案对这个案例没有真正的帮助 我有两个类,User和DAL DAL等级: namespace Models; use Config\Helpers as Helpers; use Lib\RedBean\R as R; use Lib\RedBean\Plugin\ModelValidation as ModelValidation; class DA

我和红豆有点问题。我遇到了一个连接错误,无法理解这是什么意思,我已经在谷歌上搜索过,找不到任何有效的解决方案,我在这里尝试了一个类似的问题,但答案对这个案例没有真正的帮助

我有两个类,User和DAL

DAL等级:

namespace Models;

use Config\Helpers as Helpers;
use Lib\RedBean\R as R;
use Lib\RedBean\Plugin\ModelValidation as ModelValidation;

class DAL extends R
{
    function __construct()
    {
        R::setup( 'mysql:host=localhost;dbname=angularjs', 'user', 'pass' );

        R::ext('validate', function($bean)
        {
            return ModelValidation::validate($bean);
        });
    }

    public function createBean($tableName)

    public function create($bean)

    public function read($tableName, $id = 0)

    function __destruct()
    {
       R::close();
    }
}
用户类别:

namespace Models;

use Models\DAL as DAL;
use Lib\JWT\JWT as JWT;
use Config\Helpers as Helpers;
use Lib\RedBean\SimpleModel as SimpleModel;

class Usuario extends SimpleModel
{
    public static $rules = [
        'login' => [
            'label' => 'Login',
            'filter' => 'trim|sanitize_string',
            'validation' => 'required|alpha_numeric',
        ],
        'senha' => [
            'label' => 'Senha',
            'validation' => 'required|alpha_numeric|min_len,6',
            'message' => 'I don not like this password. This is an example of a bad message.'
        ],
    ];

    public function login()

    public function logout()

    public function criaUsuario()
    {
        $DAL = new DAL();

        //user creation code
    }

    public function criaToken($usuarioId = 0)
    {
        $DAL = new DAL();

        //Token creation code
    }
}
这就是错误:

Fatal error:  Uncaught exception 'Lib\RedBean\RedException' with message 'A database has already been specified for this key.' in D:\Projetos\Market Media\backend\Lib\RedBean\Facade.php:288
Stack trace:
#0 D:\Projetos\Market Media\backend\Lib\RedBean\Facade.php(206): Lib\RedBean\Facade::addDatabase('default', 'mysql:host=loca...', 'angularjs', 'root', false)
#1 D:\Projetos\Market Media\backend\Models\DAL.php(13): Lib\RedBean\Facade::setup('mysql:host=loca...', 'angularjs', 'root')
#2 D:\Projetos\Market Media\backend\Models\Usuario.php(67): Models\DAL->__construct()
#3 D:\Projetos\Market Media\backend\Models\Usuario.php(56): Models\Usuario->criaToken('27')
#4 D:\Projetos\Market Media\backend\API\UsuarioAPI.php(42): Models\Usuario->criaUsuario()
#5 D:\Projetos\Market Media\backend\API\BaseAPI.php(82): API\UsuarioAPI->criaUsuario(Array)
#6 D:\Projetos\Market Media\backend\Controllers\UsuarioController.php(13): API\BaseAPI->processAPI()
#7 D:\Projetos\Market Media\backend\index.php(26): Controllers\UsuarioController->start()
# in D:\Projetos\Market Media\backend\Lib\RedBean\Facade.php on line 288

每次创建DAL rb的新实例时,请尝试在“default”键上设置数据库,尝试将R::setup置于类外,一旦进入应用程序

如果在构造方法中使用R::setup()函数,则最常用的语法是:

public __construct (){
    if(!R::testConnection()){
        R::setup($DSN, $USER, $PASSWORD);
    }
}

此错误的另一个原因是您的连接详细信息可能不正确。例如数据库名称、用户名或密码。首先检查您的连接详细信息,然后尝试其他解决方案。

我真傻,构造函数不是放置连接的最聪明的地方!感谢Santi模式警报!