Reputation: 544
i have a sports tournament database schema like this
here the doman
has one to many
relatinship with tournament
, i have a domain
form which collects the domain details and stores it in the database, i have the tournament and domain table like this
tournament table
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| domain_id | int(11) | YES | MUL | NULL | |
| description | longtext | NO | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
Domain Table
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| subdomain | varchar(255) | NO | | NULL | |
+-----------+--------------+------+-----+---------+----------------+
i have a tournamentType
which collects the details of the tournament, but when submited there will obviously be a null
value for the domain_id
. how do i set the domain value for all tournaments
that will be created to lets say domain_id = 1
.
i could do it in the controller using the setters but that would be messy, is there a way to set the one
side from the many
side in the symfony embedded forms,
edit: right now i'm doing this, inside the controller
$tournament->setDomain($em->getRepository("CricketBundle\Model\Entity\Domain")->findOneById(1));
$em->persist($tournament);
$em->flush();
and inside tournament entity's setter
public function setDomain(\CricketBundle\Model\Entity\Domain $domain = null)
{
$domain->addTournament($this);
$this->domain = $domain;
return $this;
}
every thing works great, i just dont want to use the setter inside the controller, i want to move it somewhere else
Upvotes: 1
Views: 160
Reputation: 8276
You could always just set the default domain id on your tournament table in the database and let the database handle it for you.
Another option would be to use a Doctrine entity listener on your Tournament entity on the prePersist
event, which only occurs before that entity is inserted into the database for the first time, and not on updates. Symfony has some documentation on how to do this. Earlier versions of Doctrine did not support listeners on a single entity, and required you to listen on the events for the entire entity manager and then check to see if it was the proper entity you wanted, but I'm going to assume you are on the latest versions.
So, first you would define the listener on your Tournament entity as a service in whatever configuration file is being loaded:
services:
listener.tournament_entity:
class: CricketBundle\EventListener\TournamentListener
tags:
- { name: doctrine.orm.entity_listener }
Then you would create your TournamentListener:
<?php
namespace CricketBundle\EventListener;
use Doctrine\ORM\Event\LifecycleEventArgs;
use CricketBundle\Model\Entity\Tournament;
class TournamentListener
{
public function prePersist(Tournament $tournament, LifecycleEventArgs $eventArgs)
{
$em = $eventArgs->getEntityManager();
$domain = $em->getRepository('CricketBundle\Model\Entity\Domain')->find(1);
$tournament->setDomain($domain);
}
}
Upvotes: 2
Reputation: 2375
In your form tournamentType
form code, you need to add / or replace the following code :
$builder->add('domain', 'entity', array(
'class' => "YourBundle:DomainClass",
'empty_data' => '1')
);
This code initalize your field to the first domain.
If you want your field to be hidden in the form, you must change the type of your field to hidden
.
Upvotes: 1