Skip to content

Instantly share code, notes, and snippets.

@randm-ch

randm-ch/Report.php Secret

Last active Aug 29, 2015
Embed
What would you like to do?
<?php
namespace MyCompany\MyApplication\Domain\Model;
use TYPO3\Flow\Annotations as Flow;
use Doctrine\ORM\Mapping as ORM;
/**
* @Flow\Entity
*/
class Report {
/**
* @var string
* @Flow\Validate(type="NotEmpty")
*/
protected $name;
/**
* @var \MyCompany\MyApplication\Domain\Model\DqlStatement
* @ORM\OneToOne
*/
protected $dqlStatement;
/**
* @var \TYPO3\Flow\Object\ObjectManager
* @Flow\Inject
*/
protected $objectManager;
/**
* @var \Doctrine\Common\Collections\ArrayCollection
* @Flow\Transient
*/
protected $results = NULL;
/**
* @return string
*/
public function getName() {
return $this->name;
}
/**
* @param string $name
* @return void
*/
public function setName($name) {
$this->name = $name;
}
/**
* @return \MyCompany\MyApplication\Domain\Model\DqlStatement
*/
public function getDqlStatement() {
return $this->dqlStatement;
}
/**
* @param \MyCompany\MyApplication\Domain\Model\DqlStatement $dqlStatement
* @return void
*/
public function setDqlStatement($dqlStatement) {
$this->dqlStatement = $dqlStatement;
}
/**
* Populates $this->results.
*
* @return void
*/
protected function getQueryResults() {
try {
if($this->dqlStatement instanceof \MyCompany\MyApplication\Domain\Model\DqlStatement) {
/** @var \Doctrine\ORM\Query $query */
$query = $this->objectManager->get('Doctrine\Common\Persistence\ObjectManager')->createQuery($this->dqlStatement->getDql());
$this->results = new \Doctrine\Common\Collections\ArrayCollection($query->execute());
}
else
throw new \Doctrine\ORM\Query\QueryException('DQL Statement object not found.');
}
catch(\Doctrine\ORM\Query\QueryException $e) {
$this->results = new \Doctrine\Common\Collections\ArrayCollection();
}
}
/**
* @return array
*/
public function getResults() {
$results = array();
if($this->results === NULL) $this->getQueryResults();
if($this->results->count() > 0) {
foreach($this->results as $result) {
foreach($result as $propertyName => $propertyValue) {
if($propertyValue instanceof \DateTime) $result[$propertyName] = $propertyValue->format('d.m.Y');
}
$results[] = $result;
}
}
return $results;
}
/**
* @return array
*/
public function getProperties() {
if($this->results === NULL) $this->getQueryResults();
if($this->results->count() > 0)
return array_keys($this->results->first());
return array();
}
/**
* @return array
*/
public function getPropertyNames() {
$propertyNames = array();
if($this->results === NULL) $this->getQueryResults();
if($this->results->count() > 0) {
foreach(array_keys($this->results->first()) as $property) {
$propertyNames[$property] = preg_replace('/\d+$/', '', $property);
}
}
return $propertyNames;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment