使用symfony 4的树枝循环中不存在变量

问题描述 投票:0回答:1

我收到的“变量“任务”不存在。”在树枝循环中,

我尝试使用“如果已定义”循环,但我的任务没有出现在屏幕上。

我到处都检查过,不知道从哪里来。

所以这就是我的模板:

{% extends 'base.html.twig' %}

{% block title 'Agence lorem ipsum' %}

{% block body %}
    <div class="jumbotron text-center">
        <h1>Trouvez une mission qui vous correspond</h1>
        <p>My super est encore en developpement , vous pouvez apporter vos idées via le formulaire de contact</p>
    </div>

    <div class="container">
        <h2>Les dernieres missions</h2>
        <div class="row flex">

            {% for Mission in Missions %}
                <div class="col-3">
                    <div class="card">
                        <div class="card-body">
                            <h5 class="card-title">
                                <a href="{{ path('lesmissions/show.html.twig', {id: mission.id, slug: mission.slug}) }}">{{ mission.titre }}</a>
                            </h5>
                            <p class="card-text">{{ mission.adresse }} ({{ mission.createdat|date('Y-m-d') }})</p>
                            <div class="text-primary" style="font-weight: bold;font-size: 2rem;">{{ mission.gain }} €</div>
                        </div>
                    </div>
                </div>
            {% endfor %}

        </div>
    </div>
{% endblock %}

那是我的任务实体

<?php

namespace App\Entity;

use App\Repository\MissionRepository;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass=MissionRepository::class)
 */
class Mission
{






    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=90)
     */
    private $titre;

    /**
     * @ORM\Column(type="text")
     */
    private $description;

    /**
     * @ORM\Column(type="date")
     */
    private $date_debut;

    /**
     * @ORM\Column(type="date", nullable=true)
     */
    private $date_fin;

    /**
     * @ORM\Column(type="integer")
     */
    private $gain;

    /**
     * @ORM\Column(type="boolean" , options={"default" : false})
     */
    private $Urgence;

    /**
     * @ORM\Column(type="boolean", nullable=true , options={"default" : false})
     */
    private $Accepted;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $adresse;

    /**
     * @ORM\Column(type="date", nullable=true)
     */
    private $created_at;

    public function __construct()
    {
        $this->created_at = new \Date();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getTitre(): ?string
    {
        return $this->titre;
    }

    public function setTitre(string $titre): self
    {
        $this->titre = $titre;

        return $this;
    }

    public function getSlug(): string
    {
        return (new Slugify())->slugify($this->titre);
    }


    public function getDescription(): ?string
    {
        return $this->description;
    }

    public function setDescription(string $description): self
    {
        $this->description = $description;

        return $this;
    }

    public function getDateDebut(): ?\DateTimeInterface
    {
        return $this->date_debut;
    }

    public function setDateDebut(\DateTimeInterface $date_debut): self
    {
        $this->date_debut = $date_debut;

        return $this;
    }

    public function getDateFin(): ?\DateTimeInterface
    {
        return $this->date_fin;
    }

    public function setDateFin(?\DateTimeInterface $date_fin): self
    {
        $this->date_fin = $date_fin;

        return $this;
    }

    public function getGain(): ?int
    {
        return $this->gain;
    }

    public function setGain(int $gain): self
    {
        $this->gain = $gain;

        return $this;
    }

    public function getUrgence(): ?bool
    {
        return $this->Urgence;
    }

    public function setUrgence(bool $Urgence): self
    {
        $this->Urgence = $Urgence;

        return $this;
    }

    public function getAccepted(): ?bool
    {
        return $this->Accepted;
    }

    public function setAccepted(?bool $Accepted): self
    {
        $this->Accepted = $Accepted;

        return $this;
    }

    public function getAdresse(): ?string
    {
        return $this->adresse;
    }

    public function setAdresse(?string $adresse): self
    {
        $this->adresse = $adresse;

        return $this;
    }

    public function getCreatedAt(): ?\DateTimeInterface
    {
        return $this->created_at;
    }

    public function setCreatedAt(?\DateTimeInterface $created_at): self
    {
        $this->created_at = $created_at;

        return $this;
    }
}

那是我的存储库

<?php

namespace App\Repository;

use App\Entity\Mission;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\ORM\QueryBuilder;


/**
 * @method Mission|null find($id, $lockMode = null, $lockVersion = null)
 * @method Mission|null findOneBy(array $criteria, array $orderBy = null)
 * @method Mission[]    findAll()
 * @method Mission[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class MissionRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, Mission::class);
    }

    /**
     * @return Mission[]
     */
    public function findAllVisible(): array
    {
        return $this->findVisibleQuery()
            ->getQuery()
            ->getResult();
    }

    /**
     * @return Mission[]
     */
    public function findLatest(): array
    {
        return $this->findVisibleQuery()
            ->setMaxResults(30)
            ->getQuery()
            ->getResult();
    }

    private function findVisibleQuery(): QueryBuilder
    {
        return $this->createQueryBuilder('m')
            ->where('m.Accepted = false');
    }

    // /**
    //  * @return Mission[] Returns an array of Mission objects
    //  */
    /*
    public function findByExampleField($value)
    {
        return $this->createQueryBuilder('m')
            ->andWhere('m.exampleField = :val')
            ->setParameter('val', $value)
            ->orderBy('m.id', 'ASC')
            ->setMaxResults(10)
            ->getQuery()
            ->getResult()
        ;
    }
    */

    /*
    public function findOneBySomeField($value): ?Mission
    {
        return $this->createQueryBuilder('m')
            ->andWhere('m.exampleField = :val')
            ->setParameter('val', $value)
            ->getQuery()
            ->getOneOrNullResult()
        ;
    }
    */
}

最后确定我的任务负责人

<?php

namespace App\Controller;


use App\Entity\Mission;
use App\Repository\MissionRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;



class MissionController extends AbstractController {



    /**
     * @var MissionRepository
     */
    private $repository;
    /**
     * @var EntityManagerInterface
     */
    private $em;

    public function __construct(MissionRepository $repository, EntityManagerInterface $em)
    {
        $this->repository = $repository;
        $this->em = $em;
    }

    /**
     * @return Response
     * @throws \Twig\Error\LoaderError
     * @throws \Twig\Error\RuntimeError
     * @throws \Twig\Error\SyntaxError
     * @Route ("/missions" , name="Missions")
     */
    public function index():Response
    {
        return $this->render('lesmissions/index.html.twig');
    }


    /**
     * @Route("/missions/{slug}-{id}", name="mission.show", requirements={"slug": "[a-z0-9\-]*"})
     * @param Mission $mission
     * @return Response
     */
    public function show(Mission $mission, string $slug): Response
    {
        if ($mission->getSlug() !== $slug) {
            return $this->redirectToRoute('mission.show', [
                'id' => $mission->getId(),
                'slug' => $mission->getSlug()
            ], 301);
        }
        return $this->render('lesmissions/show.html.twig', [
            'mission' => $mission,
            'current_menu' => 'missions'
        ]);
    }


}


?>

(这是我页面的控制器)

<?php

namespace App\Controller;

use App\Repository\MissionRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class LesmissionsController extends AbstractController
{
    /**
     * @Route("/lesmissions", name="lesmissions")
     * @param MissionRepository $repository
     * @return Response
     */
    public function index(MissionRepository $repository): Response
    {
        $missions = $repository->findLatest();
        return $this->render('lesmissions/index.html.twig', [
            'missions' => $missions
        ]);
    }

}

预先感谢:)

php symfony doctrine twig
1个回答
0
投票

在您的视图模板中:替换以下行:

{% for Mission in Missions %}

with:

{% for mission in missions %}
© www.soinside.com 2019 - 2024. All rights reserved.