【问题标题】:Where clause with OR operator Model_Crud, FuelPHP带有 OR 运算符 Model_Crud、FuelPHP 的 Where 子句
【发布时间】:2013-09-18 09:33:59
【问题描述】:

我正在使用 win7、FuelPHP 1.6 和 xampp 1.8.1 (PHP 5.4.7)

我创建了新的模型 crud 类:

class Model_Message extends \Model_Crud {
protected static $_table_name = 'message';

public static function list_message(){
    $list_messages = Model_Message::find(array(
        'select' => array('id', 'type', 'content', 'from_user', 'to_user', 'created_at', 'updated_at'),
        'where' => array(
            'from_user' => '1',
            'or' => array('to_user' => '2'),
        ),
        'limit' => 50,
    ));
    return $list_messages;
}

这是错误

注意!

Fuel\Core\PhpErrorException [ Notice ]: Undefined offset: 0
COREPATH/classes/database/query/builder/where.php @ line 60

55        {
56            foreach ($column as $key => $val)
57            {
58                if (is_array($val))
59                {
60                    $this->and_where($val[0], $val[1], $val[2]);
61                }
62                else
63                {
64                    $this->and_where($key, '=', $val);
65                }

如果我删除 array('to_user' => '2'), >,它会起作用。 但我想要过滤器 1 或 2。

【问题讨论】:

    标签: mysql model where crud fuelphp


    【解决方案1】:

    关于本页:http://fuelphp.com/docs/packages/orm/crud.html

    如果使用find()函数,则不能使用or_where。

    这是一个很好的方法:

    public static function list_message(){
        $list_messages = Model_Message::query()->select('id', 'type', 'content', 'from_user', 'to_user', 'created_at', 'updated_at')->where('from_user', '1')->or_where('to_user', '2')->limit(50);
        return $list_messages;
    }
    

    希望对你有所帮助。

    【讨论】:

      【解决方案2】:

      来自手册页here

      class Model_Message extends \Model_Crud {
      protected static $_table_name = 'message';
      
      public static function list_message(){
          $list_messages = Model_Message::find(array(
              'select' => array('id', 'type', 'content', 'from_user', 'to_user', 'created_at', 'updated_at'),
              'where' => array(
                  array('from_user' => '1'), // <-- note the array
                  'or' => array('to_user' => '2'),
              ),
              'limit' => 50,
          ));
          return $list_messages;
      }
      

      看起来你的语法有点错误

      【讨论】:

      • $entry = Model_Article::find('all', array('where' => array( array('category_id', 1), 'or' => array( array('category_id' , 2), ), ), )); =======> 不工作
      • 通过此页面github.com/fuel/fuel/blob/1.6/master/CHANGELOG.md#v16 方法 Find() 已弃用。
      • 这是关于 Model_Crud,而不是关于 ORM。
      【解决方案3】:

      你可以这样试试..

      public static function list_message(){
          $list_messages = Model_Message::query()->select('id', 'type', 'content', 'from_user', 'to_user', 'created_at', 'updated_at')->where('from_user', 1)->or_where('to_user', 2)->limit(50);
          return $list_messages;
      }
      

      如果您需要更多参考,请查看这部分文档。 http://fuelphp.com/docs/packages/orm/crud.html

      【讨论】:

        【解决方案4】:

        这与 WanWizard 所说的 ORM 无关。

        Model_Crud 不能按常规方式使用“or”,因为“find”静态方法在内部调用“Model_DB::and_where()。我找到了这样的方法:

        $list_messages = Model_Message::find(array(
            'select' => array('id', 'type', 'content', /*...*/ 'created_at', 'updated_at'),
            'where' => function($query) {
               $query->where("from_user", 1)->or_where("to_user", 2);
            },
            'limit' => 50,
        ));
        

        不聪明...没有什么好的解决办法吗?

        http://fuelphp.com/docs/classes/database/qb_where.html#/method_and_where

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-06-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多