Redbean Models

46 views
Skip to first unread message

JTuller

unread,
Apr 12, 2018, 8:11:40 AM4/12/18
to redbeanphp
I must say, redbeanphp is super fun and I'm really excited to see that work continues on this project with updated releases.

So to that I say - thank you!

I'm just starting to get my hands dirty with this whole configuration-less pattern, and I'm trying to work through Models properly. I was under the impression that I could call methods from a Model by calling said method against a bean. For instance:

<?PHP
require_once __DIR__ . '/vendor/autoload.php';
use RedBeanPHP\R;
define('_FROZEN_DATABASE_',false);
define('_LOG_ERRORS_',false);
define('_PERSISTENCE_DATABASE_','rb_test_database');
define('_PERSISTENCE_CONNECTION_ENDPOINT_','localhost');
define('_PERSISTENCE_CONNECTION_CREDENTIALS_',['userName'=>'root','password'=>null]);
define('_PERSISTENCE_DSN_','mysql:dbname='._PERSISTENCE_DATABASE_.';host='._PERSISTENCE_CONNECTION_ENDPOINT_);

R::setup(_PERSISTENCE_DSN_,_PERSISTENCE_CONNECTION_CREDENTIALS_['userName'],_PERSISTENCE_CONNECTION_CREDENTIALS_['password'],_FROZEN_DATABASE_);

class User extends \RedBeanPHP\SimpleModel {
   public $id;
   public $userName;

    public function someMethodNeededFromModel() {
       return 'I am working';
   }
}

class UserRepo {
   /**
    * @param $id
    * @return \RedBeanPHP\OODBBean|User
    */
   public function findOrGetNew($id) {
       return R::findOneOrDispense('user', $id);
   }
   public function save($bean) {
       return R::store($bean);
   }
}

$userRepo = new UserRepo();
$user = $userRepo->findOrGetNew(1);
$user->userName = 'Test User';
$id = $userRepo->save($user);
echo 'userId of saved user: '.$id.' - some other method that returns some string: '.$user->someMethodNeededFromModel();

I would expect the output of this to be:

userId of saved user: 1 - some other method that returns some string: I am working
 
My test_database ends up with a table user and a record is inserted with the user_name 'Test User'

However someMethodNeededFromModel() does not seem to be working as described here in the FUSED methods section:

I'm sure it's something I'm doing wrong or that I have not configured properly yet.
Any help is appreciated!
Thanks!
Jeff

Lynesth

unread,
Apr 12, 2018, 9:20:27 AM4/12/18
to redbeanphp
Hi there,

By default RedBean will connect beans with a class that is prefixed with "Model_" so in your case you need to use

class Model_User extends \RedBeanPHP\SimpleModel {

If you want to be able to use "User" directly without any prefix, or if you want to set the prefix you want to use, you can call :

// No prefix for example
define
( 'REDBEAN_MODEL_PREFIX', '' );


More information on the page you linked ;)

Regards,

Lyn.


JTuller

unread,
Apr 12, 2018, 2:26:37 PM4/12/18
to redbeanphp
Well now I feel silly - 

Now I know my problem is with namespaces. I neglected to set the Model Prefix in my test script - but I am most certainly setting this in my project's code and you're correct once I set that properly in the above test code I was able to call that method directly from the bean! magic redbeans ftw :).

So, somehow I am defining the namespaces incorrectly.

I am using Composer to autoload my namespaced classes using the following namespaces:


Reply all
Reply to author
Forward
0 new messages