Bonjour, j'ai actuellement un gros problème sur mon projet en Symfony depuis mon passage en version 4.4. Mon versioning à était correctement réalisé cependant j'ai une page "Projet" qui affiche tout les projets crées dans la base de donnée du site. Le problème est que la création de projet est réalisable et les projets sont bien apparant dans la BDD mais le site ne les affiches pas. J'ai vérifié le code de mon controleur "ProjectController" et tout me semble normal. Cependant j'ai changé une ligne de celui-ci car la syntaxe d'écriture pour appeler les classes grâce au getRepository à changé entre les versions 3.4 et 4.4. Avant, j'appelais mes classes en faisant getRepository('App::$className') avec une variable className dont la valeur changé en fonction des classes qui était appelé sur ma page. Maintenant un getRepository n'accepte plus le 'App::' et j'ai donc dû definir le chemin de mes classes en mettant getRepository('App\Entity\'.$className). Et j'ai l'impression que le problème peux venir d'ici car peut être qu'il récupère uniquement la première classe appelée et qu'il ne passe pas à la suivante.
Je n'arrive pas à régler le problème car celui-ci n'est pas un message d'erreur, je cherche donc une aiguille dans une botte de foin. Petite précision je n'ai pas beaucoup de compétence en symfony car je n'avais jamais utilisé ce framework avant et je suis donc arrivé sur ce projet qui nécessitait juste une mise à niveau.
Je met le code mon ProjectController juste en dessous pour mieux comprendre mon explication.
<?php
namespace App\Controller;
use App\Entity\Advancement;
use App\Entity\Alert;
use App\Entity\Project;
use App\Entity\State;
use App\Form\Search\ProjectSearchType;
use App\Form\Type\AdvancementType;
use App\Form\Type\AlertType;
use App\Form\Type\ProjectType;
use App\Model\ProjectSearch;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class ProjectController extends BaseController
{
protected function listAction($className, $params = array())
{
return array_merge(array(
'title' => 'title.' . $className . '.list',
'pagetitle' => 'title.' . $className . '.list',
$className . 's' => $this->getRepository('App\\Entity\\' . $className)->getList(),
), $params);
}
protected function createAction($request, $project, $form, $className)
{
if ($form->handleRequest($request)->isSubmitted() && $form->handleRequest($request)->isValid()) {
$entity = $form->getData();
$entity->setCreatedBy($this->getUser());
$entity->setCreatedAt(new \DateTime());
$this->persist($entity);
$this->flush();
$this->addFlash('success', 'flash.success.created.' . $className);
return $this->redirect($this->generateUrl('project_edit', array('id' => $project->getId())) . '#tab-' . $className);
}
return array(
'form' => $form->createView(),
'title' => $this->trans('title.' . $className . '.create'),
'pagetitle' => $this->trans('title.' . $className . '.create'),
'projectLink' => $this->generateUrl('project_edit', array('id' => $project->getId())),
);
}
protected function editAction($request, $project, $form, $className, $class)
{
if ($form->handleRequest($request)->isSubmitted() && $form->handleRequest($request)->isValid()) {
$entity = $form->getData();
$entity->setUpdatedBy($this->getUser());
$entity->setUpdatedAt(new \DateTime());
$this->flush();
$this->addFlash('success', 'flash.success.updated.' . $className);
return $this->redirect($this->generateUrl('project_edit', array('id' => $project->getId())) . '#tab-' . $className);
}
return array(
'form' => $form->createView(),
'title' => $this->trans('title.' . $className . '.edit', array('%name%' => $class)),
'pagetitle' => $this->trans('title.' . $className . '.edit', array('%name%' => $class)),
'projectLink' => $this->generateUrl('project_edit', array('id' => $project->getId())),
);
}
protected function deleteAction($class, $className)
{
try {
$this->remove($class);
$this->flush();
$this->addFlash('success', 'flash.success.deleted.' . $className);
} catch (ForeignKeyConstraintViolationException $e) {
$this->addFlash('error', 'flash.error.deleted.' . $className);
}
if ($className != 'project') {
$id = $class->getProject()->getId();
$route = $this->generateUrl('project_edit', array('id' => $id)) . '#tab-' . $className;
} else {
$route = $this->generateUrl('project_list');
}
return $this->redirect($route);
}
/**
* @Route("/projects/{page}", defaults={"page":1}, name="project_list")
* @Template("Project\listProject.html.twig")
* @Security("has_role('ROLE_READER')")
*/
public function projectListAction(Request $request, $page = 1)
{
$searchModel = new ProjectSearch();
if (!$this->isGranted('ROLE_READER')) {
$searchModel->setCompany($this->getUser()->getCompany());
}
$searched = $searchModel;
$searchForm = $this->get('form.factory')->createNamed('', ProjectSearchType::class, $searchModel, array(
'method' => 'GET',
'action' => $this->generateUrl('project_list') . '#results',
));
$searchForm->handleRequest($request);
if ($searchForm->isSubmitted() && $searchForm->isValid()) {
$searched = $searchForm->getData();
}
$pager = $this->getPager($page, $this->getRepository(Project::class)->getSearchQuery($searched), $searchModel->getPerPage());
return $this->listAction('project', array(
'pager' => $pager,
'projects' => $pager->getCurrentPageResults(),
'searchForm' => $searchForm->createView(),
));
}
/**
* @Route("/project/new",name="project_create")
* @Template("Project\createProject.html.twig")
* @Security("has_role('ROLE_READER')")
*/
public function projectCreateAction(Request $request)
{
$project = new Project();
$project->setBeginAt(new \DateTime());
$project->setState($this->getRepository(State::class)->find(State::INITIALIZED));
$project->setCompany($this->getUser()->getCompany());
$form = $this->createForm(ProjectType::class, $project, array(
'mode' => 'create',
'display_company_select' => $this->isGranted('ROLE_SUPERADMIN'),
));
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$project = $form->getData();
$project->setCreatedBy($this->getUser());
$this->persist($project);
$this->flush();
$this->addFlash('success', 'flash.success.created.project');
return $this->redirect($this->generateUrl('project_list'));
}
return array(
'modeEdit' => false,
'form' => $form->createView(),
'title' => $this->trans('title.project.create'),
'pagetitle' => $this->trans('title.project.create'),
'project' => $project,
);
}
/**
* @Route("/project/edit/{id}", name="project_edit")
* @Template("Project\createProject.html.twig")
* @Security("is_granted('edit', project)")
*/
public function projectEditAction(Request $request, Project $project)
{
$project = $this->getManager(Project::class)->initForEdition($project);
$form = $this->createForm(ProjectType::class, $project, array(
'display_company_select' => $this->isGranted('ROLE_SUPERADMIN'),
));
try {
$alerts = $this->getRepository(Alert::class)->getAlertsByProject($project);
} catch (Exception $e) {
$alerts = array(new Alert());
}
try {
$advancements = $this->getRepository(Advancement::class)->getAdvancementsByProject($project);
} catch (Exception $e) {
$advancements = array(new Advancement());
}
if ($form->handleRequest($request)->isSubmitted() && $form->handleRequest($request)->isValid()) {
$project = $this->getManager(Project::class)->updateProject($form->getData());
$this->flush();
$this->addFlash('success', 'flash.success.updated.project');
return $this->redirect($this->generateUrl('project_list'));
}
return array(
'modeEdit' => true,
'form' => $form->createView(),
'advancements' => $advancements,
'alerts' => $alerts,
'title' => $this->trans('title.project.edit', array('%name%' => $project)),
'pagetitle' => $this->trans('title.project.edit', array('%name%' => $project)),
'project' => $project,
);
}
/**
* @Route("/project/view/{id}", name="project_view")
* @Template("viewProject.html.twig")
* @Security("is_granted('view', project)")
*/
public function projectViewAction(Request $request, Project $project)
{
return $this->projectEditAction($request, $project);
}
/**
* @Route("/project/delete/{id}", name="project_delete")
*/
public function projectDeleteAction(Request $request, Project $project)
{
// @todo : voir le delete en cascade
return $this->deleteAction($project, 'project');
}
/**
* @Route("/project/print", name="project_print")
*/
public function projectPrintAction(Request $request)
{
$projects = $this->getRepository(Project::class)->findById(array_keys($request->request->all()));
$html = '';
foreach ($projects as $project) {
$html .= $this->renderView('resume.html.twig', array(
'project' => $project,
));
}
if ($html == '') {
$this->addFlash('error', 'warn.project.check');
return $this->redirect($this->generateUrl('project_list'));
}
// Génération du pdf
$html2pdf = $this->get('html2pdf_factory')->create();
$html2pdf->WriteHTML($html);
return new Response(
$html2pdf->Output(),
200,
array(
'Content-Type' => 'application/pdf; charset=utf-8',
)
);
}
/**
* @Route("/advancements",name="advancement_list")
* @Template("listAdvancement.html.twig")
*/
public function advancementListAction(Request $request)
{
return $this->listAction('advancement');
}
/**
* @Route("/advancement/new/{id}",name="advancement_create")
* @Template("createAdvancement.html.twig")
*/
public function advancementCreateAction(Request $request, Project $project)
{
$avancement = new Advancement();
$avancement->setProject($project);
$form = $this->createForm(AdvancementType::class, $avancement, array());
return $this->createAction($request, $project, $form, 'advancement');
}
/**
* @Route("/advancement/edit/{id}", name="advancement_edit")
* @Template("createAdvancement.html.twig")
*/
public function advancementEditAction(Request $request, Advancement $advancement)
{
$form = $this->createForm(AdvancementType::class, $advancement, array());
return $this->editAction($request, $advancement->getProject(), $form, 'advancement', $advancement);
}
/**
* @Route("/advancement/delete/{id}", name="advancement_delete")
*/
public function advancementDeleteAction(Request $request, Advancement $advancement)
{
return $this->deleteAction($advancement, 'advancement');
}
/**
* @Route("/alerts",name="alert_list")
* @Template("listAlert.html.twig")
*/
public function alertListAction(Request $request)
{
return $this->listAction('alert');
}
/**
* @Route("/alert/new/{id}",name="alert_create")
* @Template("createAlert.html.twig")
*/
public function alertCreateAction(Request $request, Project $project)
{
$alerte = new Alert();
$alerte->setProject($project);
$form = $this->createForm(AlertType::class, $alerte, array());
return $this->createAction($request, $project, $form, 'alert');
}
/**
* @Route("/alert/edit/{projectid}/{alertid}", name="alert_edit")
* @ParamConverter("project", class="Project", options={"id" = "projectid"})
* @ParamConverter("alert", class="Alert", options={"id" = "alertid"})
* @Template("createAlert.html.twig")
*/
public function alertEditAction(Request $request, Project $project, Alert $alert)
{
$form = $this->createForm(AlertType::class, $alert, array());
return $this->editAction($request, $project, $form, 'alert', $alert);
}
/**
* @Route("/alert/delete/{id}", name="alert_delete")
*/
public function alertDeleteAction(Request $request, Alert $alert)
{
return $this->deleteAction($alert, 'alert');
}
/**
* @Template("_projectLoads.html.twig")
*/
public function projectLoadsAction(Request $request, $projectId)
{
$project = $this->getManager(Project::class)->getProject($projectId);
$slippingCalendar = $this->get('date.handler')->slippingCalendar();
// Returned loads can be outside start-end, so we limit to this year
$validLoads = array();
foreach ($project->getLoads() as $l) {
$id = $l->getUser()->getId();
$year = $l->getYear();
if (!array_key_exists($id, $validLoads)) {
$validLoads[$id] = array('user' => $l->getUser());
}
$validLoads[$id][$year][] = $l;
}
$activities = $this->getActivitiesByMonth($project);
return array(
'project' => $project,
'loads' => $validLoads,
'activities' => $activities,
'slippingCalendar' => $slippingCalendar, // yyyy-11-01, yyyy-12-01, yyyy-01-01,...
);
}
/**
* Build an array with the project activies spread by month and year
* @param Project $project
* @return array
*/
protected function getActivitiesByMonth(Project $project)
{
$validActivities = array();
foreach ($project->getActivities() as $a) {
$id = $a->getUser()->getId();
$date = $a->getMondayDate();
$i = 0;
do {
$year = $date->format('Y');
$month = $date->format('m');
if (!array_key_exists($id, $validActivities)) {
$validActivities[$id] = array('user' => $a->getUser());
}
if (!array_key_exists($year, $validActivities[$id])) {
$validActivities[$id][$year] = array();
}
$validActivities[$id][$year][$month][] = $a->getTimeDay($i);
$date->modify('+1 day');
$i++;
} while ($i < 7);
}
return $validActivities;
}
}
Ce que je veux
Je cherche à voir réapparaitre les projets présents dans la base de donnée sur ma page internet.
Ce que j'obtiens
Un tableau vide alors qu'il devrait être alimenté par les projets présents dans la BDD.
Si vous cliquez sur le petit point d'exclamation, vous obtenez le "vrai" message d'exception dans les logs. Si vous voulez voir la stack trace, cliquez sur le lien "Exception" dans le menu de gauche.