我不能让box()方法工作,我只能从它得到NULL 。
例如,如果我这样做
$bean = \R::load('comment', 2); print("\n\nBEAN:\n"); var_dump($bean); $model = $bean->box(); print("\n\nMODEL:\n"); var_dump($model);我明白了
BEAN: class RedBeanPHP\OODBBean#68 (10) { protected $properties => array(4) { 'id' => string(1) "2" 'user' => string(1) "2" 'reply_to' => NULL 'message' => string(30) "Test comment 1" } protected $__info => array(4) { 'type' => string(7) "comment" 'sys.id' => string(2) "id" 'sys.orig' => array(5) { 'id' => string(1) "2" 'user' => string(1) "1" 'reply_to' => NULL 'message' => string(30) "Test comment 1" } 'tainted' => bool(false) 'changed' => bool(false) } protected $beanHelper => class RedBeanPHP\BeanHelper\SimpleFacadeBeanHelper#17 (0) { } protected $fetchType => NULL protected $withSql => string(0) "" protected $withParams => array(0) { } protected $aliasName => NULL protected $via => NULL protected $noLoad => bool(false) protected $all => bool(false) } MODEL: NULL很显然,bean中有数据,那么为什么box()返回NULL ?
I can't get the box() method to work, I only get NULL from it.
For example if I do this
$bean = \R::load('comment', 2); print("\n\nBEAN:\n"); var_dump($bean); $model = $bean->box(); print("\n\nMODEL:\n"); var_dump($model);I get this
BEAN: class RedBeanPHP\OODBBean#68 (10) { protected $properties => array(4) { 'id' => string(1) "2" 'user' => string(1) "2" 'reply_to' => NULL 'message' => string(30) "Test comment 1" } protected $__info => array(4) { 'type' => string(7) "comment" 'sys.id' => string(2) "id" 'sys.orig' => array(5) { 'id' => string(1) "2" 'user' => string(1) "1" 'reply_to' => NULL 'message' => string(30) "Test comment 1" } 'tainted' => bool(false) 'changed' => bool(false) } protected $beanHelper => class RedBeanPHP\BeanHelper\SimpleFacadeBeanHelper#17 (0) { } protected $fetchType => NULL protected $withSql => string(0) "" protected $withParams => array(0) { } protected $aliasName => NULL protected $via => NULL protected $noLoad => bool(false) protected $all => bool(false) } MODEL: NULLClearly there is data in the bean, so why does box() return NULL?
最满意答案
看起来这是一个命名空间问题。
该文件说明
如果你喜欢你的模型驻留在命名空间\ Model中,你可以设置下面的常量:
//with namespace Model define( 'REDBEAN_MODEL_PREFIX', '\\Model\\' )你现在可以像这样创建一个模型类:
class \Model\Band extends \RedBeanPHP\SimpleModel { ... }
措辞“......你可以 ......”让我相信它是可选的。 然而,事实证明,如果你喜欢你的模型驻留在命名空间\ Model中,并且你希望能够使用FUSE方法,你必须设置上述常量,并且你必须调用你的模型类Band 。
此外,请注意,在定义该名称空间字符串时,您必须在每处使用双反斜杠( \\ )。
为了增加混淆,在上面的文档段落之前,有一段说
RedBeanPHP使用命名约定(即Model_ {TYPE of BEAN})自动将bean与模型连接起来。
显然,在不使用名称空间时,应该使用命名约定Model_Band ,并在命名约定时使用Band ,否则就会出现问题。
Seems it was a namespacing problem.
The documentation says that
If you like your models to reside in the namespace \Model, you can set the following constant:
//with namespace Model define( 'REDBEAN_MODEL_PREFIX', '\\Model\\' )You can now create a model class like this:
class \Model\Band extends \RedBeanPHP\SimpleModel { ... }
The phrasing "... you can set ..." led me to believe that it's optional. However, it turns out that if you like your models to reside in the namespace \Model, and you want to be able to use FUSE methods, you MUST set the above constant and you MUST call your model class Band.
Also, note that you MUST use double backslashes (\\) everywhere when you define that namespace string.
To add to the confusion, right before the above documentation paragraph, there is a paragraph saying
RedBeanPHP automatically connects beans with models using a naming convention (i.e. Model_{TYPE OF BEAN}).
Apparently, you should use the naming convention Model_Band when NOT using namespaces, and the naming convention Band when you do, or things break down.
更多推荐
发布评论