gabordemooij / redbean

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

Problems with Composer and Models #596

Closed benmajor closed 6 years ago

benmajor commented 6 years ago

When using Composer and RedBeanPHP, we have run into some issues using Models within our application. For example:

namespace RedBeanPHP;
class Model_User extends RedBean_SimpleModel { }

Produces the following error:

Fatal error: Class 'RedBeanPHP\RedBean_SimpleModel' not found...

It seems there's a scoping issue with ReadBean_SimpleModel. After digging around in the source, I tried the following:

namespace RedBeanPHP;
class Model_User extends SimpleModel {  }

Which seems to get a step further, but then we receive the following error:

Fatal error: Cannot declare class RedBeanPHP\Model_User, because the name is already in use...

Is there a fix for this?

benmajor commented 6 years ago

Okay, after some lengthy digging, I have found a solution. The following works:

class Model_User extends \RedBeanPHP\SimpleModel { }

Note: In order to use the R facade within the Model, we also need to add a use statement at the beginning of the Model. As such, Models should look like this when using Composer:

use \RedBeanPHP\R;

class Model_User extends \RedBeanPHP\SimpleModel
{
    public function check() 
    {
        R::findOne( ... );
    }
}

I think it's worth updating the Readme to include information about this, as the documentation doesn't seem to cover it. I'll make a PR and update.

benmajor commented 6 years ago

Please see https://github.com/gabordemooij/redbean/pull/597.

gabordemooij commented 6 years ago

thanks for contributing!