gabordemooij / redbean

ORM layer that creates models, config and database on the fly
https://www.redbeanphp.com
2.3k stars 279 forks source link

OODB RedBean #931

Closed tihiydo closed 4 months ago

tihiydo commented 11 months ago

Notice: Undefined offset: 0 in /var/www/api/vendor/gabordemooij/redbean/RedBeanPHP/Repository.php on line 450

$sql = $this->connection->find( 'webusers', 'username = ?', ['username' => [ $login ] ]);

gabordemooij commented 11 months ago

Interesting, will look into it asap.

Lynesth commented 11 months ago

At a glance it seems to me that you either have to write it

$sql = $this->connection->find( 'webusers', 'username = ?', [ $login ]);

OR

$sql = $this->connection->find( 'webusers', 'username = :username', [ ':username' => $login ]);

OR

$sql = $this->connection->find( 'webusers', ['username' => [ $login ] ]);
tihiydo commented 11 months ago

Interesting, will look into it asap.

To be honest, I still don’t understand if I’m using the object-oriented version correctly, but I’ll say that the documentation on $toolbox->getRedBean() is not enough, and the fact that there is only find() is also a shame.

ConnectDb.php image

<?php

require_once __DIR__ . "/../api/methods/public/response.php";
require_once __DIR__ . "/../vendor/autoload.php";

use RedBeanPHP\R;
use RedBeanPHP\OODBBean;

class connectDb
{
    public $connection;

    public function __construct($data) 
    {
        $this->initConnection();
    }

    public function initConnection()
    {
        R::setup('mysql:host=localhost;dbname=NLtrade','admin', 'TcSMSQFbGV#&z3Nv+rCJ^wxPK!hsD!FCY^UmYv&gsrv7NSTuy4b-Yct=yJkLpchA');
        $isConnected = R::testConnection();

        if(!$isConnected)
        {
            new response(false, "Can't connect database");
            exit(0);
        }
        else
        {
            $this->connection = R::$toolbox->getRedBean();
        }
    }
}
?>

auth.php image

<?php

require_once __DIR__ . "/../../../database/connectDb.php";
require_once __DIR__ . "/response.php";

use RedBeanPHP\R;
use RedBeanPHP\OODBBean;

class auth extends connectDb
{
    private $data;

    public function __construct($data) 
    {
        $this->data = $data;
        $this->initConnection();
        //$this->showData();
        $this->checkPassword($data['login'], $data['password']);
    }

    private function showData()
    {
        print_r($this->data);
    }

    private function checkPassword($login, $password)
    {
        $sql = $this->connection->find( 'webusers', 'username = ?', ['username' => [ $login ] ]);
        $user = reset($sql);

        if(strval($user->password) == strval($password))
        {
            $respData = [];
            $respData['id'] = $user->id;
            new response(true, $respData);
        }
        else
        {
            new response(false);
        }

        exit(0);
    }
}

?>

_printr($this->data)

Array
(
    [login] => wefewefewfwf
    [password] => 6OhKpwK2x3By6Ys9e38X
)

_printr($sql)

Array
(
    [4] => RedBeanPHP\OODBBean Object
        (
            [properties:protected] => Array
                (
                    [id] => 4
                    [password] => 6OhKpwK2x3By6Ys9e38X
                    [username] => wefewefewfwf
                    [email] => few
                    [telegram_id] => fewwef
                    [project_link] => fewfew
                )

            [__info:protected] => Array
                (
                    [type] => webusers
                    [sys.id] => id
                    [sys.orig] => Array
                        (
                            [id] => 4
                            [password] => 6OhKpwK2x3By6Ys9e38X
                            [username] => wefewefewfwf
                            [email] => few
                            [telegram_id] => fewwef
                            [project_link] => fewfew
                        )

                    [tainted] => 
                    [changed] => 
                    [changelist] => Array
                        (
                        )

                    [model] => 
                    [data.bundle] => Array
                        (
                        )

                )

            [beanHelper:protected] => RedBeanPHP\BeanHelper\SimpleFacadeBeanHelper Object
                (
                )

            [fetchType:protected] => 
            [withSql:protected] => 
            [withParams:protected] => Array
                (
                )

            [aliasName:protected] => 
            [via:protected] => 
            [noLoad:protected] => 
            [all:protected] => 
            [castProperty:protected] => 
        )

    [5] => RedBeanPHP\OODBBean Object
        (
            [properties:protected] => Array
                (
                    [id] => 5
                    [password] => 9A0b97VLkw5OehC31NOV
                    [username] => wefewefewfwf
                    [email] => few
                    [telegram_id] => fewwef
                    [project_link] => fewfew
                )

            [__info:protected] => Array
                (
                    [type] => webusers
                    [sys.id] => id
                    [sys.orig] => Array
                        (
                            [id] => 5
                            [password] => 9A0b97VLkw5OehC31NOV
                            [username] => wefewefewfwf
                            [email] => few
                            [telegram_id] => fewwef
                            [project_link] => fewfew
                        )

                    [tainted] => 
                    [changed] => 
                    [changelist] => Array
                        (
                        )

                    [model] => 
                    [data.bundle] => Array
                        (
                        )

                )

            [beanHelper:protected] => RedBeanPHP\BeanHelper\SimpleFacadeBeanHelper Object
                (
                )

            [fetchType:protected] => 
            [withSql:protected] => 
            [withParams:protected] => Array
                (
                )

            [aliasName:protected] => 
            [via:protected] => 
            [noLoad:protected] => 
            [all:protected] => 
            [castProperty:protected] => 
        )

    [6] => RedBeanPHP\OODBBean Object
        (
            [properties:protected] => Array
                (
                    [id] => 6
                    [password] => UHfT6ExO6sP2v1b8myLZ
                    [username] => wefewefewfwf
                    [email] => few
                    [telegram_id] => fewwef
                    [project_link] => fewfew
                )

            [__info:protected] => Array
                (
                    [type] => webusers
                    [sys.id] => id
                    [sys.orig] => Array
                        (
                            [id] => 6
                            [password] => UHfT6ExO6sP2v1b8myLZ
                            [username] => wefewefewfwf
                            [email] => few
                            [telegram_id] => fewwef
                            [project_link] => fewfew
                        )

                    [tainted] => 
                    [changed] => 
                    [changelist] => Array
                        (
                        )

                    [model] => 
                    [data.bundle] => Array
                        (
                        )

                )

            [beanHelper:protected] => RedBeanPHP\BeanHelper\SimpleFacadeBeanHelper Object
                (
                )

            [fetchType:protected] => 
            [withSql:protected] => 
            [withParams:protected] => Array
                (
                )

            [aliasName:protected] => 
            [via:protected] => 
            [noLoad:protected] => 
            [all:protected] => 
            [castProperty:protected] => 
        )

    [7] => RedBeanPHP\OODBBean Object
        (
            [properties:protected] => Array
                (
                    [id] => 7
                    [password] => BOpC3VZJ0L6pXM2INRFU
                    [username] => wefewefewfwf
                    [email] => few
                    [telegram_id] => fewwef
                    [project_link] => fewfew
                )

            [__info:protected] => Array
                (
                    [type] => webusers
                    [sys.id] => id
                    [sys.orig] => Array
                        (
                            [id] => 7
                            [password] => BOpC3VZJ0L6pXM2INRFU
                            [username] => wefewefewfwf
                            [email] => few
                            [telegram_id] => fewwef
                            [project_link] => fewfew
                        )

                    [tainted] => 
                    [changed] => 
                    [changelist] => Array
                        (
                        )

                    [model] => 
                    [data.bundle] => Array
                        (
                        )

                )

            [beanHelper:protected] => RedBeanPHP\BeanHelper\SimpleFacadeBeanHelper Object
                (
                )

            [fetchType:protected] => 
            [withSql:protected] => 
            [withParams:protected] => Array
                (
                )

            [aliasName:protected] => 
            [via:protected] => 
            [noLoad:protected] => 
            [all:protected] => 
            [castProperty:protected] => 
        )

    [8] => RedBeanPHP\OODBBean Object
        (
            [properties:protected] => Array
                (
                    [id] => 8
                    [password] => OgutPYnnVsOBkAwGGkeR
                    [username] => wefewefewfwf
                    [email] => few
                    [telegram_id] => fewwef
                    [project_link] => fewfew
                )

            [__info:protected] => Array
                (
                    [type] => webusers
                    [sys.id] => id
                    [sys.orig] => Array
                        (
                            [id] => 8
                            [password] => OgutPYnnVsOBkAwGGkeR
                            [username] => wefewefewfwf
                            [email] => few
                            [telegram_id] => fewwef
                            [project_link] => fewfew
                        )

                    [tainted] => 
                    [changed] => 
                    [changelist] => Array
                        (
                        )

                    [model] => 
                    [data.bundle] => Array
                        (
                        )

                )

            [beanHelper:protected] => RedBeanPHP\BeanHelper\SimpleFacadeBeanHelper Object
                (
                )

            [fetchType:protected] => 
            [withSql:protected] => 
            [withParams:protected] => Array
                (
                )

            [aliasName:protected] => 
            [via:protected] => 
            [noLoad:protected] => 
            [all:protected] => 
            [castProperty:protected] => 
        )

)

As a result, we have <b>Notice</b>: Undefined offset: 0 in <b>/var/www/api/vendor/gabordemooij/redbean/RedBeanPHP/Repository.php</b> on line <b>450</b>


At a glance it seems to me that you either have to write it

$sql = $this->connection->find( 'webusers', 'username = ?', [ $login ]);

OR

$sql = $this->connection->find( 'webusers', 'username = :username', [ ':username' => $login ]);

OR

$sql = $this->connection->find( 'webusers', ['username' => [ $login ] ]);

The first option will return empty.

The second is the same error on which I created the issue.

The third one seems to work, but still I would like some clearer documentation. I'm just sure that when I need to return data in batches, some other problems will arise. Ideally, all the same, make findOne and findAll available via OODB.

Lynesth commented 11 months ago

Oh my bad, I read your code too fast earlier, didn't notice you were using the database class to query stuff (not that it was entirely clear up to that point :)). Since you don't want to use the Facade, you'll probably want to use the Finder for your "finding" needs.

$finder = new Finder( $toolbox );

....

$finder->find( 'webusers', 'username = ?', [ $login ]);
$finder->findOne( 'webusers', 'username = ?', [ $login ]);
// etc
tihiydo commented 11 months ago

Ох, плохо, раньше я слишком быстро читал ваш код и не заметил, что вы используете класс базы данных для запроса (не то чтобы до этого момента это было совсем ясно :)). Поскольку вы не хотите использовать Facade, вы, вероятно, захотите использовать Finder для своих «поисковых» нужд.

$finder = new Finder( $toolbox );

....

$finder->find( 'webusers', 'username = ?', [ $login ]);
$finder->findOne( 'webusers', 'username = ?', [ $login ]);
// etc

if finder via getRedBean() doesn't work then why is it there? can you clarify me a little

Lynesth commented 11 months ago

It works and it is used internally a lot by RedBean, but think of it as a low level function while the ones in Finder are higher level ones (which are the ones used by Facade methods as well).

gabordemooij commented 11 months ago

It works and it is used internally a lot by RedBean, but think of it as a low level function while the ones in Finder are higher level ones (which are the ones used by Facade methods as well).

RedBean allows you to access low level functions for flexibility, but it is recommended to use the more high-level functions like Finder. I always recommend to use the simple static functions, they are the most user-friendly ones.

I also see that you are using RedBean to check passwords, personally I use matchUp() for this functionality, https://redbeanphp.com/index.php?p=/data_tools#matchup.

tihiydo commented 11 months ago

how to activate inspect function via Toolbox

Lynesth commented 11 months ago

Just take a look at the code of that Facade function:

public static function inspect( $type = NULL )
{
    return ($type === NULL) ? self::$writer->getTables() : self::$writer->getColumns( $type );
}

So if you want the same thing you'll use one of those two:

// To get the list of tables
$toolbox->getWriter()->getTables();

// To get the columns of a table
$toolbox->getWriter()->getColumns('mytable');
tihiydo commented 10 months ago

Just take a look at the code of that Facade function:

public static function inspect( $type = NULL )
{
    return ($type === NULL) ? self::$writer->getTables() : self::$writer->getColumns( $type );
}

So if you want the same thing you'll use one of those two:

// To get the list of tables
$toolbox->getWriter()->getTables();

// To get the columns of a table
$toolbox->getWriter()->getColumns('mytable');

What method is there similar to FindAll through class approach?

Lynesth commented 10 months ago

Did you even check the code of the findAll method of the Facade that you are trying to replicate? Just as with my previous answer, go and check the code, I'm pretty sure it's like 1 line long, and you'll find your answer there.