示例#1
0
	/**
	 * Queries for all records from the target table that are associated with
	 * the specified record.
	 *
	 * @param \Bedrock\Model\Record $record the record to find associations with
	 * @param string $targetTableName the name of the target table from which to retrieve associated Records
	 * @param array $limit
	 *
	 * @throws \Bedrock\Model\Query\Exception if the query fails
	 * @return \Bedrock\Model\ResultSet the corresponding ResultSet
	 */
	public function associated($record, $targetTableName, $limit = array()) {
		try {
			if($this->_target == self::TARGET_PROCEDURE) {
				throw new \Bedrock\Model\Query\Exception('Stored procedure queries cannot have associated records.');
			}
			
			// Setup
			$result = new \Bedrock\Model\ResultSet();
			$connection = \Bedrock\Common\Registry::get('database')->getConnection();
			$recordTable = $record->getTable();
			$recordTableName = $recordTable->getProperty('name');
			$targetTable = new \Bedrock\Model\Table(array('name' => $targetTableName));
			
			$targetTable->load();
			
			// Verify Table Association
			$mappings = $recordTable->getMappings();
			
			if(array_key_exists($targetTableName, $mappings)) {
				// Query for Associated Records
				switch($mappings[$targetTableName]) {
					default:
					case \Bedrock\Model\Table::MAP_TYPE_ONE_ONE:
					case \Bedrock\Model\Table::MAP_TYPE_ONE_MANY:
						$foreignKeys = $targetTable->getForeignKeys();
						$query = \Bedrock\Model\Query::from($targetTableName)->where($foreignKeys[$recordTableName]->name, '=', $record->{$record->getPrimaryKey()});
						
						if(count($limit)) {
							$query = $query->limit($limit['start'], $limit['count']);
						}
						
						$result = $query->execute();
						
						break;
						
					case \Bedrock\Model\Table::MAP_TYPE_MANY_ONE:
						$foreignKeys = $recordTable->getForeignKeys();
						$query = \Bedrock\Model\Query::from($targetTableName)->where($targetTable->getPrimaryKey()->name, '=', $record->{$foreignKeys[$targetTableName]->name});
						
						if(count($limit)) {
							$query = $query->limit($limit['start'], $limit['count']);
						}
						
						$result = $query->execute();
						
						break;
						
					case \Bedrock\Model\Table::MAP_TYPE_MANY_MANY:
						$mappingTableName = \Bedrock\Model\Utilities::getMappingTableName($recordTableName, $targetTableName);
						$query = 'SELECT t.* FROM ' . $targetTableName . ' t LEFT JOIN ' . $mappingTableName . ' m ON t.' . $targetTable->getPrimaryKey()->name . ' = m.' . $targetTable->getPrimaryKey()->name . '_' . $targetTableName . ' WHERE m.' . $recordTable->getPrimaryKey()->name . '_' . $recordTableName . ' = :recordPrimaryKey';
						
						if(count($limit)) {
							$query .= ' LIMIT ' . $limit['start'] . ', ' . $limit['count'];
						}
						
						$statement = $connection->prepare($query);
						$statement->execute(array(':recordPrimaryKey' => $record->{$record->getPrimaryKey()}));
						$results = $statement->fetchAll(\PDO::FETCH_ASSOC);
						$records = array();
						
						if($results) {
							foreach($results as $result) {
								$records[] = new \Bedrock\Model\Record($targetTableName, $result);
							}
						}
						
						$result = new \Bedrock\Model\ResultSet($records);
						$result->setCountAll(count($records));
						break;
				}
			}
			return $result;
		}
		catch(\PDOException $ex) {
			\Bedrock\Common\Logger::exception($ex);
			throw new \Bedrock\Model\Query\Exception('There was a problem with the database connection, associated records could not be retrieved.');
		}
		catch(\Exception $ex) {
			\Bedrock\Common\Logger::exception($ex);
			throw new \Bedrock\Model\Query\Exception('Could not retrieve associated records for the specified Record object.');
		}
	}
示例#2
0
	/**
	 * Retrieves all Records in the specified table associated with the current
	 * Record.
	 *
	 * @param string $tableName the name of the table to use
	 * @return \Bedrock\Model\ResultSet any associated records in the table
	 */
	public function associated($tableName, $limit = array()) {
		try {
			$result = \Bedrock\Model\Query::associated($this, $tableName, $limit);
			return $result;
		}
		catch(\Exception $ex) {
			\Bedrock\Common\Logger::exception($ex);
			throw new \Bedrock\Model\Record\Exception('A problem was encountered while checking for associated records.');
		}
	}
示例#3
0
	/**
	 * Builds a query and retrieves data corresponding to the specified URL
	 * parameters.
	 */
	public function get() {
		try {
			// Setup
			$results = array();

			// Query Database
			$query = \Bedrock\Model\Query::from($this->_params['table']);
			
			// Query: Where Clause
			if($this->_params['where']) {
				foreach($this->_params['where'] as $field => $where) {
					foreach($where as $operator => $value) {
						if($value) {
							$query = $query->where($field, $operator, $value);
						}
					}
				}
			}
			
			// Query: Sort Statement
			if($this->_params['sort'] && is_array($this->_params['sort'])) {
				foreach($this->_params['sort'] as $sort) {
					$query = $query->sort($sort[0] . ' ' . strtoupper($sort[1]));
				}
			}
			
			// Query: Limit Statement
			if($this->_params['limit']) {
				if(!$this->_params['page']) {
					$this->_params['page'] = 1;
				}
				
				$query = $query->limit($this->_params['page']*$this->_params['limit']-$this->_params['limit'], $this->_params['limit']);
			}
			
			$rows = $query->execute();
			
			// Format Rows for Data Format
			switch($this->_params['format']) {
				default:
				case 'xml':
					$format = \Bedrock\Common\DataFormat::TYPE_XML;
					$reorgRows = array();

					foreach($rows as $row) {
						$reorgRows[]['row'] = array($row);
					}
					
					$results[]['rows'] = $reorgRows;
					break;
					
				case 'yaml':
					$format = \Bedrock\Common\DataFormat::TYPE_YAML;
					foreach($rows as $row) {
						$results[]['row'] = array($row);
					}
					break;
					
				case 'flexigrid':
				case 'json':
					$format = \Bedrock\Common\DataFormat::TYPE_JSON;
					$results[]['page'] = $this->_params['page'];
					$results[]['total'] = $rows->countAll();
					$results[]['rows'] = $rows;
					break;
					
				case 'csv':
					$format = \Bedrock\Common\DataFormat::TYPE_CSV;
					break;
			}
			
			$response = \Bedrock\Common\DataFormat\Factory::get($format, $results);
			$response->printData();
		}
		catch(\Exception $ex) {
			\Bedrock\Common\Logger::exception($ex);
		}
	}