public function beforeQuery(Atomik_Model_Builder $builder, Atomik_Db_Query $query)
	{
		$tableName = $builder->tableName;
		$primaryKeyName = $builder->getPrimaryKeyField()->name;
		$foreignFieldName = $this->_prefix($tableName . '_' . $primaryKeyName);
		$on = sprintf('%s.%s = %s.%s', $this->_suffix($tableName), $foreignFieldName, $tableName, $primaryKeyName);
		
		// add a join clause to the query
		$query->join($this->_suffix($tableName), $on, null, 'LEFT');
	}
	/**
	 * Builds the GROUP BY part
	 * 
	 * @return string
	 */
	protected function _buildGroupByPart()
	{
		$sql = '';
		
		if (count($this->_info['groupBy'])) {
			$sql = ' GROUP BY ' . implode(', ', $this->_info['groupBy']);
			if (count($this->_info['having'])) {
				$sql .= ' HAVING ' . $this->_query->_concatConditions($this->_info['having']);
			}
		}
		
		return $sql;
	}
	public function dbDataTable($id = null, Atomik_Db_Query $query = null, $options = array())
	{
		$this->dataTable($id, array(), $options);
		
		if ($this->options['sortColumn']) {
			$query->orderBy($this->options['sortColumn'], $this->options['sortOrder']);
		}
		
		$countQuery = clone $query;
		$result = $countQuery->count()->execute();
		$numberOfRows = $result->fetchColumn();
		$result->closeCursor();
		
		$this->options['paginateData'] = false;
		$this->options['sortData'] = false;
		$this->options['numberOfRows'] = $numberOfRows;
		
		$offset = ($this->options['currentPage'] - 1) * $this->options['rowsPerPage'];
		$query->limit($offset, $this->options['rowsPerPage']);
		
		$this->setData($query->execute());
		
		return $this;
	}
Exemple #4
0
	/**
	 * Sets which model to query 
	 * 
	 * @param	string|Atomik_Model_Builder $model
	 * @return 	Atomik_Model_Query
	 */
	public function from($model)
	{
		$this->_builder = Atomik_Model_Builder_Factory::get($model);
		$this->setInstance($this->_builder->getManager()->getDbInstance());
		return parent::from($this->_builder->tableName);
	}
	public function beforeQuery(Atomik_Model_Builder $builder, Atomik_Db_Query $query)
	{
		// only select the primary key
		$query->clearSelect()->select($builder->tableName . '.' . $builder->getPrimaryKeyField()->name);
	}
	/**
	 * Executes a query.
	 * Uses the cache version if available
	 * 
	 * @param	Atomik_Db_Query		$query
	 * @return 	Atomik_Db_Query_Result
	 */
	protected function _executeQuery(Atomik_Db_Query $query)
	{
		$hash = $query->toHash();
		if ($this->_queryCacheEnabled && isset($this->_queryCache[$hash])) {
			$this->_queryCache[$hash]->rewind();
			return $this->_queryCache[$hash];
		}
		
		if (($result = $query->execute()) === false) {
			return false;
		}
		
		if ($this->_queryCacheEnabled) {
			$this->_queryCache[$hash] = $result;
			return $this->_queryCache[$hash];
		}
		return $result;
	}
Exemple #7
0
	/**
	 * Activates the result cache on all queries
	 * 
	 * @param	string	$enable
	 */
	public static function setAlwaysCacheResults($enable = true)
	{
		self::$_cacheAll = $enable;
	}
	/**
	 * Query the adapter
	 * 
	 * @param	Atomik_Db_Query	$query
	 * @return 	Atomik_Model_Modelset
	 */
	public function query(Atomik_Db_Query $query)
	{
		if ($query->getInfo('statement') != 'SELECT') {
			require_once 'Atomik/Model/Manager/Exception.php';
			throw new Atomik_Model_Manager_Exception('Only SELECT queries can be used with Atomik_Model_Manager');
		}
		$builder = self::getBuilderFromQuery($query);
		
		$this->_notify($builder, 'BeforeQuery', array($query));
		
		if (($result = $this->_dbInstance->query($query)) === false) {
			return new Atomik_Model_Modelset($builder, array());
		}
		
		$result->setFetchMode(PDO::FETCH_ASSOC);
		$modelSet = new Atomik_Model_Modelset($builder, $result);
		
		$this->_notify($builder, 'AfterQuery', array($modelSet));
		
		return $modelSet;
	}
	/**
	 * Re-executes the query and refreshes the result
	 */
	public function reload()
	{
		$this->_query->execute(null, true, $this);
	}