簡體   English   中英

zend_db並加入

[英]zend_db and join

我試圖了解如何在程序中使用Zend_DB,但遇到了一些問題。 當我向其傳遞簡單查詢時,下面的類(DatabaseService)起作用。 但是,如果我通過帶有join子句的查詢將其掛起,則不會返回任何錯誤。 我在查詢瀏覽器中剪切並粘貼了qry,它是有效的

任何幫助都會很棒

$SQL = "select name from mytable"

$db = new DatabaseService($dbinfo)

$db ->fetchall($SQL )  // works

-----------------------------------------------------------
$SQL = "select count(*) as cnt from EndPoints join CallID on EndPoints.`CallID` = CallID.CallID where EndPoints.LastRegister >= '2010-04-21 00:00:01' and EndPoints.LastRegister <= '2010-04-21 23:59:59' "

$db = new DatabaseService($dbinfo)

$db ->fetchall($SQL )  // DOES NO WORK

class DatabaseService
{


 function DatabaseService($dbinfo,$dbname="")
 {

  try
  {
  $dbConfig = array( 
      'host'     => $this->host,
    'username' => $this->username,
    'password' => $password,
    'dbname'   => $this->dbname );

                 $this->db = Zend_Db::factory($this->adapter, $dbConfig); 

      Zend_Db_Table::setDefaultAdapter($this->db);
  }
  catch(Zend_Exception $e)
  {
   $this->error = $e->getMessage();
   Helper::log($this->error);
   return false;
  }
 }

 public function connnect()
 {
  if($this->db !=null)
  {
   try

   {
    $this->db->getConnection(); 
       return true; 
   }
   catch (Zend_Exception $e) 
   {   
    $err = "FAILED ::".$e->getMessage()." <br />";


   }
  }
  return false;
 } 

 public function fetchall($sql)
 {

  $res= $this->db->fetchAll($sql);
  return $res;

 }
}

我不明白為什么這行不通。 在特定版本的ZF中,這可能是一個錯誤,但據我所知,沒有SQL語法錯誤。 您可以做的是像在DatabaseService類中一樣,在系統中某個位置(例如在index.php文件中)引導Zend_Db類:

$dbConfig = array(
    'host'     => 'hostname',
    'username' => 'username',
    'password' => 'password',
    'dbname'   => 'dbname'
);

$db = Zend_Db::factory('mysqli', $dbConfig);
$db->setFetchMode(Zend_Db::FETCH_OBJ);
Zend_Db_Table::setDefaultAdapter($db);

然后Zend Framework應該為您處理連接過程。 然后,不必像創建DatabaseService類那樣,只需為所需的每個表創建一個模型,如下所示:

<?php

class EndPoints extends Zend_Db_Table_Abstract
{
    protected $_name = 'EndPoints';

    /**
     * the default is 'id'. So if your table's primary key field name is 'id' you
     * will not be required to set this.  If your primary key is something like
     * 'EndPointsID' you MUST set this.
     * @var primary key field name
     */
    protected $_primary = 'EndPointsID';
}

這樣做將自動為您提供訪問諸如fetchRow(),fetchAll(),find()等功能的權限。然后,您也可以使用Zend_Db_Table_Select進行查詢,這將非常有用。 像這樣:

<?php

$endPointsModel = new EndPoints();
$callIdCount = $endPointsModel->getCallIdCount('2010-04-21 00:00:01', '2010-04-21 00:00:01');

然后,在您的EndPoints模型中,您將創建該函數,如下所示:

...

public function getCallIdCount($fromDate, $toDate)
{
    $cols = array('cnt' => 'count(*)');
    $select = $this->select->setIntegrityCheck(false) // this is crucial
        ->from($this->_name, $cols)
        ->join('CallID', "{$this->_name}.CallID = CallID.CallID", array())
        ->where("{$this->_name}.LastRegister >= ?", $fromDate)
        ->where("{$this->_name}.LastRegister <= ?", $toDate); 

    // if you need to see what the whole query will look like you can do this:
    // echo $select->__toString();
    return $this->fetchAll($select);
{

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM