Zend框架按查询顺序排列

时间:2012-10-18 02:00:39

标签: mysql zend-framework

        $field=$userVo->field;
        $order=$userVo->order;

    $selectQry      =   $this->_util->db->select()->from(array('M'=>$this->_name))
                            ->join(array('MX'=>TBL_MEDICATIONS_XREF),
                            'M.medication_id = MX.medication_id')
                            ->join(array('U'=>TBL_USERS),
                            'U.user_id = MX.user_id',array('U.identifier','U.email','U.mobile_number','U.first_name','U.last_name'))
                            ->where('MX.user_id = ?',mysql_escape_string($userId));

            if (!empty($field) && !empty($order))
            {
                // if(($field=="notes") || ($field=="rx_number"))
                if($field=="rx_number") 
                $selectQry->order("MX.$field  $order"); 

                if($field=="nick_name")
                $selectQry->order("M.$field  $order");


            } 

$medications        =   $this->_util->db->fetchAll($selectQry);

if($field=="rx_number")条件不起作用,原因是什么?

1 个答案:

答案 0 :(得分:1)

           if($field=="rx_number") 
            $selectQry->order("MX.$field  $order"); 

          if($field=="nick_name")
            $selectQry->order("M.$field  $order");

而不是它,你必须这样写。

          if($field=="rx_number") 
          {
            $orderfield = "MX.".$field." ".$order; 
            $selectQry->order($orderfield); 
          }

          if($field=="nick_name")
          {
            $orderfield2 = "M.".$field." ".$order;
            $selectQry->order($orderfiled2);
          }