【问题标题】:Pre-packaged Redbean fetches only one (last) row预打包的 Redbean 仅获取一个(最后一个)行
【发布时间】:2011-04-14 12:11:22
【问题描述】:

我想使用来自http://www.redbeanphp.com 的预打包单文件 Redbean 1.3 ORM。当我尝试以这种方式获得结果时..

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>';

输出如下:

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

如您所见,即使有 ID 1 到 xxx 的条目,我也只能得到最后一行的结果。当我将 where 子句设置为 ID &lt; 9 时,我只打印出第 8 行。

任何想法,为什么?还是 redbean 的任何无配置替代品?

【问题讨论】:

    标签: php orm redbean


    【解决方案1】:

    RedBeanPHP 的 id 区分大小写,您需要使用 'id' 而不是 ID。或者您必须使用 Bean Formatter 来确保 RedBeanPHP 知道您想使用 ID 作为主键:

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

    【讨论】:

      【解决方案2】:

      您可以将表的主键重命名为“id”,然后 Redbean 可以识别它。如果您不想像那样更改架构,则必须格式化 bean 并相应地返回自定义 id:

      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" );
      

      代码参考:https://groups.google.com/forum/#!topic/redbeanorm/weIEM8p71eQ

      【讨论】:

        猜你喜欢
        • 2019-03-09
        • 2022-08-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-19
        相关资源
        最近更新 更多