【问题标题】:Select ignores where clause using Zend_Db_SelectSelect 使用 Zend_Db_Select 忽略 where 子句
【发布时间】:2011-04-28 12:38:12
【问题描述】:
$table = new Zend_Db_Table(array('name'=>'rules'));

    $select = $table->select();
    $select->setTable($table); 
    $select->setIntegrityCheck(false);

    $select = $select
    ->from(array('ru'=>'rules'),array('ru.*'))
    ->join(array('ro'=>'roles'),'ro.id=ru.role_id',array('role_id'=>'ro.id'))
    ->join(array('g'=>'groups'),'ro.group_id=g.id',array('group_id'=>'g.id'))
    ->join(array('ug'=>'user_groups'),"ug.group_id=g.id",array('user_group_id'=>'ug.id'))
    ->where("ug.user_id={$userId}")
    ->where("ru.resource='{$resource}'")
    ->where("ru.privilege='{$privilege}'");
    echo "select: ".$select->__toString();

$row = $table->fetchAll();

我有前面的代码,但是当我尝试 fetchAll() 它返回表中的所有行,忽略 where 子句,当我使用 fetchRow() 它返回它找到的第一行,忽略 where 子句,我打印SQL语句并单独运行它并正确执行 有什么线索吗?

【问题讨论】:

    标签: php zend-framework zend-db-table zend-db-select


    【解决方案1】:

    这就是您正确创建数据库选择对象的方式

    $db = Zend_Db::factory( ...options... );
    $select = new Zend_Db_Select($db);
    

    或者你使用数据库适配器的 select() 方法

    $db = Zend_Db::factory( ...options... );
    $select = $db->select();
    

    你可以添加子句

      // Build this query:
      //   SELECT *
      //   FROM "table1"
      //   JOIN "table2"
      //   ON "table1".column1 = "table2".column1
      //   WHERE column2 = 'foo'
    
      $select = $db->select()
                   ->from('table1')
                   ->joinUsing('table2', 'column1')
                   ->where('column2 = ?', 'foo');
    

    查看Zend_Db Reference Guide 了解更多信息

    【讨论】:

      【解决方案2】:

      @ArtWorkAD 在某些方面是正确的。但在您的情况下,您不只是使用 Zend_Db_Select。您尝试扩展从 Zend_Db_Table 获得的 Zend_Db_Select(好吧,您应该尝试使用 Zend_Db_Table 处理单例模式,但这是另一个问题)。您当前的问题(如果我们排除您确实阅读文档太快的事实)是这行是正确的:

       $select->setIntegrityCheck(false);
      

      它使您的 'select-from-a-zend-db-table' 不再局限于 Active Record 模式,并且可用于额外的连接。

      但在那之后你会做一个:

      $select = new Zend_Db_Select($table);
      

      这是一个新对象的完整创建,您将其放入变量中。之前的变量值没有任何保留。您可以在它相同之前添加一个$select=null;。所以这只是取消了前 3 行。

      在完全相同的混淆模式下这行:

      $select->setTable($table); 
      

      没有必要,因为您已经从 Zend_Db_Table 中进行选择,因此该表已经存在。

      编辑 你的最后一个更大的错误是:

      $table->fetchAll()
      

      你不使用你构建的$select,而是你的$table,所以实际上你在$select中所做的一切都被忽略了:-)。从$select 获取应该会给你更好的结果

      【讨论】:

      • $select = new Zend_Db_Select($table);在我尝试选择对象时添加。我已将其删除。
      • 您对 setTable 方法也是正确的。我也删除了它,没有任何改变,但仍然没有修复。
      • 我已经用你的最后一个错误编辑了答案,使用 $table 而不是 $select
      • 非常感谢,现在解决了。我将 $select 传递给了 fetchAll() 方法
      【解决方案3】:

      这应该可行。刚刚测试过了。

          $table = new Zend_Db_Table('rules');
          $select = $table->getAdapter()->select();
      
          $select->from(array('ru' => 'rules'), array('ru.*'))
                 ->join(array('ro'=>'roles'), 'ro.id = ru.role_id', array('role_id'=>'ro.id'))
                 ->join(array('g'=>'groups'), 'ro.group_id = g.id', array('group_id'=>'g.id'))
                 ->join(array('ug'=>'user_groups'),"ug.group_id=g.id",array('user_group_id'=>'ug.id'))
                 ->where('ug.user_id = ?', $userId)
                 ->where('ru.resource = ?', $resource)
                 ->where("ru.privilege = ?", $privilege);
      
          echo (string)$select;
      

      【讨论】:

      • 我的代码产生的sql和我想要的一样,在phpMyAdmin上完美运行,但是问题是在代码这里执行的时候,并不关心where子句,它只是选择表中的所有内容
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-31
      • 1970-01-01
      • 2014-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多