RedBean:已为此密钥指定了一个数据库

时间:2016-06-16 06:03:54

标签: php redbean

我在使用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 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

3 个答案:

答案 0 :(得分:6)

如果你在构造方法中使用 R :: setup()函数,你最常使用这种语法:

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

答案 1 :(得分:0)

每次创建DAL rb的新实例时,请尝试在"默认"上设置数据库。键,尝试将R :: setup放在课堂上,一次在应用程序中

答案 2 :(得分:0)

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