PartUnitSetupService.php (1227B)
1 <?php 2 3 namespace PartKeepr\SetupBundle\Services; 4 5 use Doctrine\ORM\EntityManager; 6 use PartKeepr\PartBundle\Entity\PartMeasurementUnit; 7 8 class PartUnitSetupService 9 { 10 /** 11 * @var EntityManager 12 */ 13 private $entityManager; 14 15 public function __construct( 16 EntityManager $entityManager 17 ) { 18 $this->entityManager = $entityManager; 19 } 20 21 /** 22 * Sets up the default part unit (pieces) if it doesn't exist. 23 * 24 * @return bool True if a default unit was created, false if it already exists 25 */ 26 public function setupDefaultPartUnit() 27 { 28 $dql = "SELECT COUNT(p) FROM PartKeepr\PartBundle\Entity\PartMeasurementUnit p WHERE p.default = :default"; 29 $query = $this->entityManager->createQuery($dql); 30 $query->setParameter('default', true); 31 32 if ($query->getSingleScalarResult() == 0) { 33 $partUnit = new PartMeasurementUnit(); 34 $partUnit->setName('Pieces'); 35 $partUnit->setShortName('pcs'); 36 $partUnit->setDefault(true); 37 38 $this->entityManager->persist($partUnit); 39 $this->entityManager->flush(); 40 41 return true; 42 } else { 43 return false; 44 } 45 } 46 }