繁体   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