Skip to content
Browse files

initial commit

  • Loading branch information...
0 parents commit c11c67a5d0b93121c768cc91da602d4c0410f450 Michael Ridgway committed
Showing with 172 additions and 0 deletions.
  1. 0 README.md
  2. +98 −0 lib/ZendX/Doctrine2/FirebugProfiler.php
  3. +74 −0 lib/ZendX/Doctrine2/Paginator.php
0 README.md
No changes.
98 lib/ZendX/Doctrine2/FirebugProfiler.php
@@ -0,0 +1,98 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category ZendX
+ * @package Doctrine2
+ * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @version $Id$
+ */
+
+namespace ZendX\Doctrine2;
+
+/**
+ * Doctrine 2 and Firebug Profiler
+ *
+ * @uses \Zend_Wildfire_Plugin_FirePhp
+ * @uses \Doctrine\DBAL\Logging\SqlLogger
+ * @category ZendX
+ * @package Doctrine2
+ * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+class FirebugProfiler
+ implements \Doctrine\DBAL\Logging\SqlLogger
+{
+
+ /**
+ * Sum of query times
+ *
+ * @var float
+ */
+ protected $_totalMS = 0;
+
+ /**
+ * Total number of queries logged
+ *
+ * @var integer
+ */
+ protected $_queryCount = 0;
+
+ /**
+ * Table of queries and their times
+ *
+ * @var \Zend_Wildfire_Plugin_FirePhp_TableMessage
+ */
+ protected $_message;
+
+ public function __construct()
+ {
+ $this->_message = new \Zend_Wildfire_Plugin_FirePhp_TableMessage('Doctrine Queries');
+ $this->_message->setBuffered(true);
+ $this->_message->setHeader(array('Time','Event','Parameters'));
+ $this->_message->setOption('includeLineNumbers', false);
+ \Zend_Wildfire_Plugin_FirePhp::getInstance()->send($this->_message, 'Doctrine Queries');
+ }
+
+ /**
+ * @param string $sql The SQL statement that was executed
+ * @param array $params Arguments for SQL
+ * @param float $executionMS Time for query to return
+ */
+ public function logSQL($sql, array $params = null, $executionMS = null)
+ {
+ $this->_totalMS += $executionMS;
+ ++$this->_queryCount;
+
+ $this->_message->addRow(array(
+ number_format($executionMS, 5),
+ $sql,
+ $params
+ ));
+
+ $this->updateLabel();
+ }
+
+ /**
+ * Sets the label for the FireBug entry
+ */
+ public function updateLabel()
+ {
+ $this->_message->setLabel(
+ sprintf('Doctrine Queries (%d @ %f sec)',
+ $this->_queryCount,
+ number_format($this->_totalMS, 5))
+ );
+ }
+}
74 lib/ZendX/Doctrine2/Paginator.php
@@ -0,0 +1,74 @@
+<?php
+
+namespace ZendX\Doctrine2;
+
+class Paginator implements \Zend_Paginator_Adapter_Interface
+{
+ /**
+ * @var \Doctrine\ORM\QueryBuilder
+ */
+ protected $_qb = null;
+
+ protected $_countQb = null;
+
+ /**
+ * @var int
+ */
+ protected $_rowCount = null;
+
+ /**
+ * @param \Doctrine\ORM\QueryBuilder $qb
+ */
+ public function __construct(\Doctrine\ORM\QueryBuilder $qb)
+ {
+ $this->_qb = $qb;
+ }
+
+ /**
+ * @param int $count
+ * @return Paginator
+ */
+ public function setRowCount($count)
+ {
+ $this->_rowCount = (int)$count;
+ return $this;
+ }
+
+ /**
+ * Returns the total number of rows in the collection.
+ *
+ * @return integer
+ */
+ public function count()
+ {
+ if($this->_rowCount > 0) {
+ return $this->_rowCount;
+ } else {
+ if (null === $this->_countQb) {
+ $this->_countQb = clone $this->_qb;
+ $from = $this->_countQb->getDqlPart('from');
+ $identifierNames = \Zend_Registry::get('doctrine')->getClassMetadata($from[0]->getFrom())->getIdentifierFieldNames();
+ $this->_countQb->select('count('.$this->_countQb->getRootAlias().'.' . $identifierNames[0] . ')');
+ }
+ return $this->_rowCount = $this->_countQb->getQuery()->getSingleScalarResult();
+ }
+ }
+
+ /**
+ * Returns an collection of items for a page.
+ *
+ * @param integer $offset Page offset
+ * @param integer $itemCountPerPage Number of items per page
+ * @return array
+ */
+ public function getItems($offset, $itemCountPerPage)
+ {
+ $this->_qb->setFirstResult($offset)->setMaxResults($itemCountPerPage);
+ return $this->_qb->getQuery()->getResult();
+ }
+
+ public function setCountQueryBuilder(\Doctrine\ORM\QueryBuilder $qb)
+ {
+ $this->_countQb = $qb;
+ }
+}

0 comments on commit c11c67a

Please sign in to comment.
Something went wrong with that request. Please try again.