从Bean拆箱时RedBean返回null?(RedBean returns null when unboxing model from bean?)

编程入门 行业动态 更新时间:2024-10-26 11:26:06
从Bean拆箱时RedBean返回null?(RedBean returns null when unboxing model from bean?)

我不能让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: NULL

Clearly 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.

更多推荐

本文发布于:2023-08-06 12:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1448642.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:RedBean   null   Bean   拆箱时   model

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!