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?
