gabordemooij / redbean

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

OOP in redbeanphp #543

Closed binarygamura closed 7 years ago

binarygamura commented 7 years ago

Hi,

I am trying to do some OOP with PHP and RedBeanPHP. I created a database "user" and a simple class "User" which extends Redbeans SimpleModel. The class User has one function "getUsername" which accesses a private field "username". The database "user" has a field "username".

When I load an instance from the database via R::find() i can access the field "username" simply for example by $user['username'] but i am not able to access it by calling the function "$user->getUsername()". As far as i understand the documentation, this works as intended and i need to box the value . Here some debug info about a loaded user istance. As you can see, the properties from the database are not copied from the bean to the model instance.

RedBeanPHP\OODBBean Object
(
    [properties:protected] => Array
        (
            [id] => 2
            [username] => binary
            [password] => 
            [displayname] => Binary Gamura
            [role] => 15
            [created] => 2017-02-25 23:26:34
        )
    [__info:protected] => Array
        (
            [type] => user
            [sys.id] => id
            [sys.orig] => Array
                (
                    [id] => 2
                    [username] => binary
                    [password] => 
                    [displayname] => Binary Gamura
                    [role] => 15
                    [created] => 2017-02-25 23:26:34
                )
            [tainted] => 
            [changed] => 1
            [model] => speedy\model\User Object
                (
                    [id:speedy\model\User:private] => 
                    [username:speedy\model\User:private] => 
                    [role:speedy\model\User:private] => 
                    [bean:protected] => RedBeanPHP\OODBBean Object
 *RECURSION*
                )
        )
    [beanHelper:protected] => RedBeanPHP\BeanHelper\SimpleFacadeBeanHelper Object
        (
        )
    [fetchType:protected] => 
    [withSql:protected] => 
    [withParams:protected] => Array
        (
        )
    [aliasName:protected] => 
    [via:protected] => 
    [noLoad:protected] => 
    [all:protected] => 
)

When i do something like $user->getUsername() or $user->box()->getUsername() can not access the value loaded from the database. What am I doing wrong or is it not possible to create model classes with RedBeanPHP which can have function to access member variables? I have a background of using Hibernate for Java for quite some time and im am looking for a way to load entities (not arrays) from a database to get rid of writing CRUD operations by myself.

Thanks in advance

Binary

binarygamura commented 7 years ago

Okay, i think i misread the documents. there is actually no need to create variables in my model classes. i can access all variables loaded from the database from within functions of my model with code like this:

$this->bean->somevalue;

I just had the old Bean pattern i used with hibernate in mind. Well, sorry for opening this issue. And thanks for providing such a nice lib.