1

i have a sports tournament database schema like this

enter image description here

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

  • Do you want to create one form with 2 entities? – Trone Jan 05 '16 at 11:19
  • @Trone yes, any technique that allows me to achieve the above relationship in an elegant way would be acceptable – pinch boi triggered af Jan 05 '16 at 11:22
  • @Trone you might be right, but i will only need one single domain to be associated with all the tournaments, your suggestion give a list of domains to choose from, which is not what i want, i just want one domain to be preselected for all tournaments – pinch boi triggered af Jan 05 '16 at 11:33
  • Could you post your full code ? If you want to initialize the value of domain_id to 1, you could do it directly in the constructor of your entity. You should have a look to this previous post http://stackoverflow.com/questions/3376881/default-value-in-doctrine – Sunitrams Jan 05 '16 at 12:46
  • @SylvainMARTIN added a bit more code for you – pinch boi triggered af Jan 05 '16 at 13:07

2 Answers2

2

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);
    }
}
Jason Roman
  • 7,503
  • 10
  • 32
  • 35
1

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.

Sunitrams
  • 2,186
  • 2
  • 11
  • 27
  • Excellent Suggestion! alternately, by any chance would it be good if i set the entity in the repository class? – pinch boi triggered af Jan 05 '16 at 13:57
  • This is another possibility, to my mind I prefer using forms to open as much as possible the code. You could specialize your entity with forms, maybe in a future version you will be able to select multiple domains... – Sunitrams Jan 05 '16 at 14:14