通过控制器访问实体 Symfony 3

Access Entity through Controller Symfony 3

为了学校的项目,我需要开发一个纸牌游戏。

我正在处理卡存款。 我所有的卡都存储在数据库中,当我存入卡时,我只是以隐藏的形式发送一张带有卡 ID 的表格。

问题是:如何通过我的控制器访问打出的牌的属性?我已经测试了一切,实际上我可以得到它们,但我必须在 public 中设置我所有实体的 属性,我认为这真的很糟糕。

我真的不知道该怎么做,也许一些帮助或提示可以帮助我。

这是我的控制器的功能:

/**
     * @Route("/poser/{partie}", name="poser_carte")
     */
    public function poserCarte(Request $request, Partie $partie){
        $cartePosee = $request->request->all();
        $cartePoseeTrait = $cartePosee['carte_id'];

        $em = $this->getDoctrine()->getManager();
        $carte = $em->getRepository('AppBundle:Cartes')->findById($cartePoseeTrait);

        var_dump($carte[0]->categorie);

        $tourde = $partie->getPartieTourJoueurId();
        var_dump($tourde);

        $joueur1 = $partie->getJoueur1Id()->getId();
        var_dump($joueur1);

        $joueur2 = $partie->getJoueur2Id()->getId();
        var_dump($joueur2);

        echo $cartePoseeTrait;

        if ($tourde == $joueur1){
            echo 'coucou joueur 1';
            $mainJ1 = json_decode($partie->getMainJoueur1());
            var_dump($mainJ1);
            //on supprime la valeur du tableau et on l'ajoute dans l'autre tableau correspondant.
        }
        elseif ($tourde == $joueur2){
            echo 'coucou joueur 2';
            $mainJ2 = json_decode($partie->getMainJoueur2());
        }
        else {
            echo 't\'as rien à faire ici !';
        }
    }

这是我的卡片实体:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

/**
 * Cartes
 *
 * @ORM\Table(name="cartes")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\CartesRepository")
 * @Vich\Uploadable
 */
class Cartes
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    public $id;

    /**
     * @var string
     *
     * @ORM\Column(name="categorie", type="string", length=255)
     */
    public $categorie;

    /**
     * @var string
     *
     * @ORM\Column(name="nom", type="string", length=255)
     */
    public $nom;

    /**
     * @var int
     *
     * @ORM\Column(name="valeur", type="integer")
     */
    public $valeur;

    /**
     * @var bool
     *
     * @ORM\Column(name="extra", type="boolean")
     */
    public $extra;

    /**
     * NOTE: This is not a mapped field of entity metadata, just a simple property.
     *
     * @Vich\UploadableField(mapping="carte_image", fileNameProperty="imageName")
     *
     * @var File
     */
    public $imageFile;

    /**
     * @ORM\Column(type="string", length=255)
     *
     * @var string
     */
    public $imageName;

    /**
     * @ORM\Column(type="datetime")
     *
     * @var \DateTime
     */
    public $updatedAt;


    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set categorie
     *
     * @param string $categorie
     *
     * @return Cartes
     */
    public function setCategorie($categorie)
    {
        $this->categorie = $categorie;

        return $this;
    }

    /**
     * Get categorie
     *
     * @return string
     */
    public function getCategorie()
    {
        return $this->categorie;
    }

    /**
     * Set nom
     *
     * @param string $nom
     *
     * @return Cartes
     */
    public function setNom($nom)
    {
        $this->nom = $nom;

        return $this;
    }

    /**
     * Get nom
     *
     * @return string
     */
    public function getNom()
    {
        return $this->nom;
    }

    /**
     * Set valeur
     *
     * @param integer $valeur
     *
     * @return Cartes
     */
    public function setValeur($valeur)
    {
        $this->valeur = $valeur;

        return $this;
    }

    /**
     * Get valeur
     *
     * @return int
     */
    public function getValeur()
    {
        return $this->valeur;
    }

    /**
     * Set image
     *
     * @param string $image
     *
     * @return Cartes
     */
    public function setImage($image)
    {
        $this->image = $image;

        return $this;
    }

    /**
     * Get image
     *
     * @return string
     */
    public function getImage()
    {
        return $this->image;
    }

    /**
     * Set extra
     *
     * @param boolean $extra
     *
     * @return Cartes
     */
    public function setExtra($extra)
    {
        $this->extra = $extra;

        return $this;
    }

    /**
     * Get extra
     *
     * @return bool
     */
    public function getExtra()
    {
        return $this->extra;
    }

    /**
     * If manually uploading a file (i.e. not using Symfony Form) ensure an instance
     * of 'UploadedFile' is injected into this setter to trigger the  update. If this
     * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
     * must be able to accept an instance of 'File' as the bundle will inject one here
     * during Doctrine hydration.
     *
     * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
     *
     * @return Product
     */
    public function setImageFile(File $image = null)
    {
        $this->imageFile = $image;

        if ($image) {
            // It is required that at least one field changes if you are using doctrine
            // otherwise the event listeners won't be called and the file is lost
            $this->updatedAt = new \DateTimeImmutable();
        }

        return $this;
    }

    /**
     * @return File|null
     */
    public function getImageFile()
    {
        return $this->imageFile;
    }

    /**
     * @param string $imageName
     *
     * @return Product
     */
    public function setImageName($imageName)
    {
        $this->imageName = $imageName;

        return $this;
    }

    /**
     * @return string|null
     */
    public function getImageName()
    {
        return $this->imageName;
    }
}

我知道这当然很容易做到,但我真的找不到:(

感谢您的帮助。

您不应该(永远不?)将实体的属性设置为 public,这就是为什么我们有 getter 和 setter。

比如这个

    $carte = $em->getRepository('AppBundle:Cartes')->findById($cartePoseeTrait);

    var_dump($carte[0]->categorie);

应该是(我假设 carte 的 id 是唯一的)

    $carte = $em->getRepository('AppBundle:Cartes')->findOneById($cartePoseeTrait);

    var_dump($carte->getCategorie());// You call the getter of the categorie private
                                     // property $category

另外不要忘记检查 $carte 是否为空数组(或者如果您使用 findById)为空数组。

看看那些文档 :

http://symfony.com/doc/current/doctrine.html#generating-getters-and-setters

或法语

https://openclassrooms.com/courses/developpez-votre-site-web-avec-le-framework-symfony2/la-couche-metier-les-entites#/id/r-2080446

http://www.apprendre-php.com/tutoriels/tutoriel-49-mthodes-magiques-set-et-get.html

我不确定这是否是您想要的答案,但我希望它能对您有所帮助:-)