No getter from a association by new Persist

New topics about Symfony 2 should go here

Moderators: dcobalt, tiagojsag

No getter from a association by new Persist

Postby all4pages » Sat Aug 04, 2012 7:37 pm

Hi i have a new Problem with a Entity

I have the Entities
Project
Termin

They have a ManyToOne association

The Project has the filds dateBegin and dateEnd
And the Termin has only one date, becouse the Termin can only go for one day

I want to check in the Termin Entity if the date is between the two dates from the Project
By updating this works for me in the Termin Entity:
Code: Select all
.
.
.
use Symfony\Component\Validator\ExecutionContext;

/**
 * All4pages\PromitsoftBundle\BackendBundle\Entity\Termin
 *
 * @ORM\Table()
 * @ORM\Entity()
 *
 * @Assert\Callback(methods={"isDateValid"})
 */
class Termin
{
.
.
.
    public function isDateValid(ExecutionContext $context)
    {
       $newTerminDate = $this->date;
       $newTerminTimestamp = $newTerminDate->getTimestamp();
       
       $projectBeginDate = $this->project->getDateBegin();
       $projectBeginTimestamp = $projectBeginDate->getTimestamp();
       
       $projectEndDate = $this->project->getDateEnd();
       $projectEndTimestamp = $projectEndDate->getTimestamp();
       
       if ($newTerminTimestamp < $projectBeginTimestamp or $newTerminTimestamp > $projectEndTimestamp) {
          $context->addViolation('Der Termin liegt nicht im Zeitraum des Projektes. Der Zeitraum ist vom ' . date('d.m.Y', $projectBeginTimestamp) . ' bis zum ' . date('d.m.Y', $projectEndTimestamp), array(), null);
       }             
    }
.
.
.


but by a new Termin it dosen't work becouse the Termin dosen't know the Project, becouse it's. By the new Termin i have no $this->project->getDateBegin() and $this->project->getDateEnd()

is this working with PrePersist or so?
all4pages
Junior Member
 
Posts: 27
Joined: Tue Aug 09, 2011 6:07 pm

Re: No getter from a association by new Persist

Postby all4pages » Sat Aug 04, 2012 8:46 pm

Ok i found a solution:

In my Controler i set the $terminEntity->setProject($projectEntity) before the if ($form->isValid()) { so i have the Project in the Entity before it is persisted
Code: Select all
.
.
.
$em = $this->getDoctrine()->getEntityManager();
$projectEntity = $em->getRepository('All4pagesPromitsoftBundleBackendBundle:Project')->find($projectId);
         
$terminEntity = new Termin();
$terminEntity->setProject($projectEntity); //This is important for isDateValid() in the Termin Entity by a new Entity
      
$form = $this->createForm(new TerminType(), $terminEntity);
$form->bindRequest($request);

if ($form->isValid()) {
.
.
.
all4pages
Junior Member
 
Posts: 27
Joined: Tue Aug 09, 2011 6:07 pm


Return to General Symfony 2 discussion

Who is online

Users browsing this forum: No registered users and 5 guests