dpgjci27392 2011-04-28 12:38
浏览 41
已采纳

使用Zend_Db_Select选择忽略where子句

$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();

I have the preceding code,but when I try fetchAll() it returns all rows in the table, ignoring the where clause, when I use fetchRow() it returns the first row it finds, ignoring the where clause, I printed the SQL statement and run it separately and it executes correctly any clue ?

  • 写回答

3条回答 默认 最新

  • doujiyun8846 2011-04-30 22:22
    关注

    @ArtWorkAD is right in a certain way. But in your case you're not just using a Zend_Db_Select. You tried to extend a Zend_Db_Select obtained from a Zend_Db_Table (well, you should try to handle a Singleton pattern with Zend_Db_Table but this is another problem). Your current problem (if we except the fact you are certainly reading documentation too fast) is that this line was correct:

     $select->setIntegrityCheck(false);
    

    It make your 'select-from-a-zend-db-table' not anymore restricted to the Active Record Mode, and available for extra joins.

    But just after that you make a:

    $select = new Zend_Db_Select($table);
    

    This is the complete creation of a new object, that you put into your variable. Nothing is kept from previous variable value. You could add a $select=null; just before it would be the same. So this is just canceling the 3 previous lines.

    In quite the same confusion mode this line:

    $select->setTable($table); 
    

    Is not necessary as you're already taking the select from a Zend_Db_Table so the table is already there.

    EDIT And your last and bigger error is:

    $table->fetchAll()
    

    You do not use your built $select but your $table, so effectively everything done in your $select is ignored :-) . Fecthing from the $select shoudl give you better results

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?