Skip to content

Instantly share code, notes, and snippets.

@Raistlfiren
Created December 28, 2015 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Raistlfiren/9b1051c3fb79a9f36fc8 to your computer and use it in GitHub Desktop.
Save Raistlfiren/9b1051c3fb79a9f36fc8 to your computer and use it in GitHub Desktop.
<?php
/**
* Extract lookup table information from a database to be used in a select field.
* For example, a page can be published, rejected, or pending...
*
*/
namespace ICS\CoreBundle\Library;
use Doctrine\Common\Persistence\ObjectManager;
class ChoiceFiller
{
/** @var ObjectManager $_em */
protected $_em;
public function __construct(ObjectManager $em)
{
$this->em = $em;
}
/**
* @param $entities
* @param $id
* @param $name
* @return mixed
* @throws \ErrorException
*/
protected function entityExtractor($entities, $id, $name)
{
foreach($entities as $entity) {
$entityList[$entity->{$id}()] = $entity->{$name}();
}
if (empty($entityList))
return array();
//throw new \ErrorException('Query did not execute correctly or didn\'t return any results');
return $entityList;
}
}
<?php
namespace ICS\FrontEnd\PageBundle\Library;
use ICS\CoreBundle\Library\ChoiceFiller;
class StatusChoiceFiller extends ChoiceFiller
{
/**
* @return \ICS\FrontEnd\PageBundle\Entity\CmsStatus[]
* @throws \ErrorException
*/
public function extractStatuses()
{
$statuses = $this->em->getRepository('PageBundle:CmsStatus')->findAll();
return $this->entityExtractor($statuses, 'getId', 'getDescription');
}
}
//Find and add all statuses to an array
$statuses = new StatusChoiceFiller($em);
$form = $this->createForm(new PagesType(), $entity, array(
'action' => $this->generateUrl('pages_create'),
'method' => 'POST',
'statuses' => $statuses->extractStatuses(),
));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment