1
0
mirror of https://github.com/ToxicCrack/PrintABrick.git synced 2025-05-17 21:00:09 -07:00
PrintABrick/src/AppBundle/Repository/LDraw/SubpartRepository.php
2017-04-23 17:24:19 +02:00

45 lines
1.2 KiB
PHP

<?php
namespace AppBundle\Repository\LDraw;
use AppBundle\Entity\Color;
use AppBundle\Entity\LDraw\Subpart;
use AppBundle\Repository\BaseRepository;
class SubpartRepository extends BaseRepository
{
public function findOneByKeys($parent, $child, $color)
{
return $this->find(['parent' => $parent, 'subpart' => $child, 'color' => $color]);
}
/**
* Create new Subpart relation entity or retrieve one by foreign keys.
*
* @param $name
*
* @return Subpart
*/
public function getOrCreate($parent, $child, $count, $colorId)
{
if (($subpart = $this->findOneByKeys($parent, $child, $colorId))) {
$subpart->setCount($count);
} else {
$subpart = new Subpart();
$colorRepository = $this->getEntityManager()->getRepository(Color::class);
if(!($color = $colorRepository->find($colorId))) {
$color = $colorRepository->find(-1);
}
$subpart
->setParent($parent)
->setSubpart($child)
->setCount($count)
->setColor($color);
}
return $subpart;
}
}