Zend_Db ve katılmak

1 Cevap php

Benim programda Zend_Db'nin nasıl kullanılacağını anlamaya çalışıyorum ama bazı sorunlar var. Ben bunu basit bir sorgu geçirdiğinizde (DatabaseService) aşağıda sınıfı çalışır. Ben bir katılmak fıkra ile bunu bir sorgu geçirirseniz Ancak, benim sayfa sadece asılı ve hiçbir hata döndürdü. Ben kesilmiş ve bir sorgu tarayıcıda Qry yapıştırın ve geçerli

Herhangi bir yardım çok iyi olurdu

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

 }
}

1 Cevap

Bu işe neden göremiyorum. Bu ZF belirli bir sürümdeki bir hata olabilir ama bildiğim kadarıyla söyleyebilirim hiçbir SQL sözdizimi hataları vardır. Neler yapabileceğini sizin DatabaseService sınıfta yaptıklarını gibi index.php dosyasındaki gibi bir yerde sisteminizde Zend_Db sınıfının Bootstrap olduğunu:

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

Ve sonra Zend Framework sizin için bağlantı işlemini işlemesi gerekir. Sonra yerine bir DatabaseService sınıf olan sadece bu kadar gibi gereken her tablo için bir model oluşturmak:

<?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';
}

Bunu yapmak automagically Sonra da oldukça yararlı olabilir sorguları için Zend_Db_Table_Select kullanabilirsiniz vb) (bulmak, böyle fetchRow (), fetchAll () gibi fonksiyonlara erişim verecektir. Şöyle:

<?php

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

Sonra Endpoints model bunu gibi bu işlevi yaratacaktır:

...

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);
{