Pre-packaged Redbean fetches only one (last) row

2019-09-01 03:07发布

I'd like to use the pre-packaged one-file Redbean 1.3 ORM from http://www.redbeanphp.com. When I try to get a result this way..

require_once ('redbean/rb.php');
require_once ('config.php');

R::setup('mysql:host='.MYSQL_HOST.';dbname=mydb', MYSQL_USER, MYSQL_PASS);
$test = R::find('function', ' ID < 10 ');
foreach ($test as $val) echo $val->Class.'<br>';

the output is as follows:

Notice: Undefined index: id in rb.php on line 3686

Notice: Undefined index: id in rb.php on line 3686

Notice: Undefined index: id in rb.php on line 3686
value.of.class.field.from.function.table    // << prints only the 9th value

As you can see I get only the result of the last row even though there are entries for ID 1 to xxx. When I set the where clause to ID < 9 I get only the 8th row printed out.

Any ideas, why? Or any configless alternatives to redbean?

标签: php orm redbean
2条回答
劫难
2楼-- · 2019-09-01 03:48

You can rename your primary key of your table to 'id', then Redbean can identify it. If you don't wish to change your schema like that then you have to format the bean and return the custom id accordingly:

class MyTableFormatter implements RedBean_IBeanFormatter{
    public function formatBeanTable($table) {
            return $table;
    }
    public function formatBeanID( $table ) {
             if ($table=="user") return "user_id";
            return "id";
    }
}

R::$writer->tableFormatter = new MyTableFormatter;
$user = R::find( "user" );

Code Ref: https://groups.google.com/forum/#!topic/redbeanorm/weIEM8p71eQ

查看更多
仙女界的扛把子
3楼-- · 2019-09-01 04:01

RedBeanPHP's id is case sensitive, you need to use 'id' instead of ID. Or you have to use a Bean Formatter to ensure RedBeanPHP knows you want to use ID as your primary key:

http://www.redbeanphp.com/#/Custom_Keys

查看更多
登录 后发表回答