-
-
Save Sorendil/0029671a1be1f4bf90ee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace MMDN\Microcebes\MicrocebesBundle\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; | |
/** | |
* ResultatTest | |
* | |
* @UniqueEntity( | |
* message = "Un résultat de test existe déjà pour cet animal et ce test", | |
* fields = {"animal", "test"}) | |
* @ORM\Table( | |
* name="resultat_test", | |
* uniqueConstraints={ | |
* @ORM\UniqueConstraint( | |
* name="resultat_test_uc", | |
* columns={"animal_id", "test_id"} | |
* ) | |
* }) | |
* @ORM\Entity(repositoryClass="MMDN\Microcebes\MicrocebesBundle\Entity\ResultatTestRepository") | |
* @ORM\InheritanceType("JOINED") | |
* @ORM\DiscriminatorColumn(name="discr", type="string") | |
* @ORM\DiscriminatorMap({ | |
* "simple" = "ResultatTestSimple", | |
* "geno" = "TestGenotypage", | |
* "immuno" = "TestImmunologique", | |
* "induc" = "TestInduction", | |
* "irm" = "Irm", | |
* "sequence" = "SequencageAdn"}) | |
*/ | |
class ResultatTest | |
{ | |
/** | |
* @var integer | |
* | |
* @ORM\Column(name="id", type="integer") | |
* @ORM\Id | |
* @ORM\GeneratedValue(strategy="AUTO") | |
*/ | |
private $id; | |
/** | |
* @ORM\ManyToOne(targetEntity="Animal", inversedBy="resultatsTests") | |
* @ORM\JoinColumn(name="animal_id", referencedColumnName="id", nullable=false) | |
**/ | |
private $animal; | |
/** | |
* @ORM\ManyToOne(targetEntity="Test", inversedBy="resultatsTests", cascade={"persist"}) | |
* @ORM\JoinColumn(name="test_id", referencedColumnName="id", nullable=false) | |
**/ | |
private $test; | |
// ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace MMDN\Microcebes\MicrocebesBundle\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
/** | |
* ResultatTestSimple | |
* | |
* @ORM\Table(name="resultat_test_simple") | |
* @ORM\Entity(repositoryClass="MMDN\Microcebes\MicrocebesBundle\Entity\ResultatTestSimpleRepository") | |
*/ | |
class ResultatTestSimple extends ResultatTest | |
{ | |
/** | |
* @var string | |
* | |
* @ORM\Column(name="mesure", type="string", length=255) | |
*/ | |
private $mesure; | |
/** | |
* @var string | |
* | |
* @ORM\Column(name="description", type="string", length=255) | |
*/ | |
private $description; | |
// ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace MMDN\Microcebes\MicrocebesBundle\Form; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\OptionsResolver\OptionsResolverInterface; | |
class ResultatTestSimpleType extends AbstractType | |
{ | |
private $typeTest_type; | |
public function __construct($typeTest_type) | |
{ | |
$this->typeTest_type = $typeTest_type; | |
} | |
/** | |
* @param FormBuilderInterface $builder | |
* @param array $options | |
*/ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$builder | |
->add('mesure') | |
->add('description') | |
->add('test', 'test_selector', [ | |
'attr' => [ | |
'typeTest_type' => $this->typeTest_type | |
] | |
]); | |
; | |
} | |
/** | |
* @param OptionsResolverInterface $resolver | |
*/ | |
public function setDefaultOptions(OptionsResolverInterface $resolver) | |
{ | |
$resolver->setDefaults(array( | |
'data_class' => 'MMDN\Microcebes\MicrocebesBundle\Entity\ResultatTestSimple' | |
)); | |
} | |
/** | |
* @return string | |
*/ | |
public function getName() | |
{ | |
return 'mmdn_microcebes_microcebesbundle_resultattestsimple'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace MMDN\Microcebes\MicrocebesBundle\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
use Symfony\Component\Validator\Constraints as Assert; | |
/** | |
* Test | |
* | |
* @ORM\Table( | |
* name="test", | |
* uniqueConstraints={ | |
* @ORM\UniqueConstraint( | |
* name="test_uc", | |
* columns={"type_test_id", "date", "experimentateur"} | |
* ) | |
* }) | |
* @ORM\Entity(repositoryClass="MMDN\Microcebes\MicrocebesBundle\Entity\TestRepository") | |
*/ | |
class Test | |
{ | |
/** | |
* @var integer | |
* | |
* @ORM\Column(name="id", type="integer") | |
* @ORM\Id | |
* @ORM\GeneratedValue(strategy="AUTO") | |
*/ | |
private $id; | |
/** | |
* @Assert\NotNull() | |
* | |
* @ORM\ManyToOne(targetEntity="TypeTest") | |
* @ORM\JoinColumn(name="type_test_id", referencedColumnName="id", nullable=false) | |
**/ | |
private $typeTest; | |
// ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace MMDN\Microcebes\MicrocebesBundle\Form; | |
use Doctrine\Common\Persistence\ObjectManager; | |
use Doctrine\ORM\EntityRepository; | |
use MMDN\Microcebes\MicrocebesBundle\Entity\Test; | |
use MMDN\Microcebes\MicrocebesBundle\Entity\TestRepository; | |
use MMDN\Microcebes\MicrocebesBundle\Entity\TypeTest; | |
use MMDN\Microcebes\MicrocebesBundle\Entity\TypeTestRepository; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\Form\FormEvent; | |
use Symfony\Component\Form\FormEvents; | |
use Symfony\Component\OptionsResolver\OptionsResolverInterface; | |
class TestSelectorType extends AbstractType | |
{ | |
const TYPE_TEST_SIMPLE = "simple"; | |
/** | |
* @var ObjectManager | |
*/ | |
private $om; | |
/** | |
* @param ObjectManager $om | |
*/ | |
public function __construct(ObjectManager $om) | |
{ | |
$this->om = $om; | |
} | |
/** | |
* @param FormBuilderInterface $builder | |
* @param array $options | |
*/ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$typeTest_type = $options['attr']['typeTest_type']; | |
if ( ! $typeTest_type) | |
{ | |
throw new \Exception('Pas de type de TypeTest !'); | |
} | |
$typeTest_type_isUnique = in_array($typeTest_type, array_keys(TypeTest::$TYPES_TEST_UNIQUE)); | |
// Si il s'agit d'un typeTest unique | |
if(! $typeTest_type_isUnique) | |
{ | |
$builder | |
->add('typeTest', 'entity', [ | |
'class' => 'MMDNMicrocebesMicrocebesBundle:TypeTest', | |
'empty_value' => 'Sélectionnez le type de test.', | |
'required' => true, | |
'multiple' => false, | |
'query_builder' => function(TypeTestRepository $er) use ($typeTest_type) { | |
return $er->findTypesTestsByTypeForForm($typeTest_type); | |
}, | |
'property' => 'labelAndUniteMesure' | |
]); | |
} | |
$builder | |
->add('date', 'date', [ | |
'label' => 'Date du test', | |
'required' => true, | |
'widget' => 'single_text' | |
]) | |
->add('experimentateur', null, [ | |
'label' => 'Expérimentateur', | |
'required' => true, | |
'attr' => array( | |
'data-help' => "Entrez le nom de la personne qui a réalisé le test." | |
) | |
]) | |
->addEventListener( | |
FormEvents::SUBMIT, | |
function(FormEvent $event) use ($typeTest_type, $typeTest_type_isUnique) { | |
/** @var Test $test */ | |
$test = $event->getData(); | |
// Si le type de TypeTest est unique, | |
// on le cherche et on le récupère à la main ! | |
if($typeTest_type_isUnique) | |
{ | |
/** @var TypeTestRepository $typeTestRepository */ | |
$typeTestRepository = $this->om | |
->getRepository('MMDNMicrocebesMicrocebesBundle:TypeTest'); | |
$typeTest = $typeTestRepository->findOneByType($typeTest_type); | |
$test->setTypeTest($typeTest); | |
} | |
// On cherche si un test existe déjà avec les mêmes données | |
/** @var TestRepository $testRepository */ | |
$testRepository = $this->om | |
->getRepository('MMDNMicrocebesMicrocebesBundle:Test'); | |
$findTest = $testRepository | |
->findOneByDateAndExpAndType( | |
$test->getDate(), | |
$test->getExperimentateur(), | |
$test->getTypeTest() | |
); | |
if($findTest) | |
{ | |
$event->setData($findTest); | |
} | |
} | |
); | |
} | |
/** | |
* @param OptionsResolverInterface $resolver | |
*/ | |
public function setDefaultOptions(OptionsResolverInterface $resolver) | |
{ | |
$resolver->setDefaults([ | |
'data_class' => 'MMDN\Microcebes\MicrocebesBundle\Entity\Test', | |
'by_reference' => false, | |
'cascade_validation' => true | |
]); | |
} | |
/** | |
* @return string | |
*/ | |
public function getName() | |
{ | |
return 'test_selector'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment