I'm working on a project under Symfony2 and I'm encountering a small but disturbing problem.
I've got an entity that represents projects led by the association to which the website is dedicated. In this entity I have a field named "$inscriptionsOuvertes" that registers if students can register themselves for each project or not.
I want to create a page where I can modify easily the state of this variable for each project, but the form I have made has no impact on my database.
The $inscriptionsOuvertes variable is always set to false, no matter what I do. Moreover, if I manually change it to true for any project under phpmyadmin, the moment I submit my form it goes back to false.
Here is the relevant code for the entity :
<?php
namespace CEC\SecteurProjetsBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Projet
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="CEC\SecteurProjetsBundle\Entity\ProjetRepository")
*/
class Projet
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="nom", type="string", length=255)
*/
private $nom;
/**
* @var string
*
* @ORM\Column(name="slug", type="string", length=100)
*/
private $slug; <br>//Autres propriétés de la classe
/**
* @var boolean
*
* @ORM\Column(name="inscriptions_ouvertes", type="boolean")
*/
private $inscriptionsOuvertes = false;
//Other properties, getters et setters...
/**
* Set inscriptionsOuvertes
*
* @param boolean $etat
* @return Projet
*/
public function setInscriptionsOuvertes($etat)
{
$this->inscriptionsOuvertes = $etat;
return $this;
}
/**
* Set inscriptionsOuvertes
*
* @return Projet
*/
public function switchInscriptionsOuvertes()
{
$this->inscriptionsOuvertes = !$this->inscriptionsOuvertes;
return $this;
}
/**
* Get inscriptionsOuvertes
*
* @return boolean
*/
public function getInscriptionsOuvertes()
{
return $this->inscriptionsOuvertes;
}
}
Here is the code for the form I've created :
{% extends 'CECSecteurProjetsBundle:Projets:base.html.twig' %}
{% block right %}
{{parent()}}
<div class="well" style = "padding-left:20px;padding-right:15px;">
<h1>Ouverture des inscriptions aux projets </h1>
<form class="form form-horizontal" method="post" action="{{ path('ouverture_inscription') }}"><br/>
Voulez-vous ouvrir les inscriptions aux projets ?<br/>
{% for projet in projets%}
<label for="{{projet.slug}}">{{projet.nom}}</label>
<div class="btn-group" data-toggle="buttons" id="{{projet.slug}}">
<label class="btn btn-success" >
<input type="radio" name="{{projet.slug}}" id="option1" value="true" autocomplete="off" {% if projet.inscriptionsOuvertes %}checked {% endif %}> Oui
</label>
<label class="btn btn-danger">
<input type="radio" name="{{projet.slug}}" id="option2" value="false" autocomplete="off" {% if not projet.inscriptionsOuvertes %}checked {% endif %}> Non
</label>
</div><br/>
{% endfor %}
<div class="footer-controls">
<br/>
<input type="submit" value="Mettre à jour les inscriptions aux projets" class="btn btn-primary" />
<a href="{{ path('description_projets') }}" class="btn pull-right">Annuler</a>
</div>
</form>
</div>
{% endblock %}
And finally here is the method in charge of validating the form and updating the database.
<?php
namespace CEC\SecteurProjetsBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use CEC\SecteurProjetsBundle\Form\ProjetType;
use CEC\SecteurProjetsBundle\Form\ReunionType;
use CEC\SecteurProjetsBundle\Form\DossierType;
use CEC\SecteurProjetsBundle\Entity\Reunion;
use CEC\SecteurProjetsBundle\Entity\Dossier;
class ProjetsController extends Controller
{
//Other methods of the controller
/**
* Mise à jour de l'état d'ouverture des inscriptions des projets
*
* @Template();
*/
public function inscriptionsAction()
{
$em = $this->getDoctrine()->getEntityManager();
$projets = $this->getDoctrine()->getRepository('CECSecteurProjetsBundle:Projet')->findAll();
$request = $this->getRequest();
$data = $request->request->all();
$message ='';
if($request->isMethod('POST'))
{
foreach($projets as $projet)
{
$slug = $projet->getSlug();
$projet->setInscriptionsOuvertes($data[$slug]);
$em->flush();
}
$this->get('session')->setFlash('success', 'L\'ouverture des inscriptions a bien été mise à jour. ');
return $this->redirect($this->generateUrl('description_projets'));
}
return array('projets'=>$projets);
}
}
I have looked on this site and found this subject to be quite similar to mine, yet different and not answering my question (in my opinion).
Do any of you have an idea of what is going on in this form ? I must admit I fail to see the logical pattern behind all this for the moment.
Thanks so much in advance for your answers !
Aucun commentaire:
Enregistrer un commentaire