我想在添加方法中升级表属性,当我在表中添加某些内容时我想减少另一个表中的属性

时间:2019-04-16 08:26:35

标签: symfony

我有两个表“ Maison”和“ Resident”,当添加一个居民时,我想减少表“ Maison”的位置的名义数,我想让你知道我有表“ Maison”的外键在“居民”上,添加居民时,我有一个列表,从中可以选择要添加居民的“ Maison”

我尝试过:

@name

那没有用

        $nomMaison = $request->get('nom_maison');
 $maison = $this->getDoctrine()->getRepository(Maison::class)->find($nomMaison);
 foreach ($maison as $i){

             $maison->setNbrPersonne($maison->getNbrPersonne()-1);
             $em->persist($maison);
             $em->flush();
         }
ResidentController.php

    public function ajoutreAction(Request $request)

    {
        $r = new Resident();
        $Form = $this->createForm(ResidentType::class, $r);
        $Form->handleRequest($request);
        $nomMaison = $request->get('nom_maison');

        if ($Form->isSubmitted()) {
           $maison = $this->getDoctrine()->getRepository(Maison::class)->find($nomMaison);

            $em = $this->getDoctrine()->getManager();
          foreach ($maison as $i){

             $maison->setNbrPersonne($maison->getNbrPersonne()-1);
             $em->persist($maison);
             $em->flush();
         }

            $em->persist($r);
            $em->flush();


            return $this->redirectToRoute('affiche_re');


        }

        return $this->render('MaisonretraiteBundle:resident:ajoutre.html.twig', array('form' => $Form->createView()));
    }

3 个答案:

答案 0 :(得分:0)

您遇到此错误:

   $maison = $this->getDoctrine()->getRepository(Maison::class)->find($nomMaison);

您必须通过此行更改其名称(其中name是实体maison的属性名称,因为find($ idmaison)就像param一样是实体的id)

$maison = $this->getDoctrine()->getRepository(Maison::class)->findOneBy(['nomMaison' => $nomMaison]); 
$id = $maison->getIdMaison(); 

如果使用get METHOD,还需要通过$ request-> query-> get('nom_maison')来更改$ request-> get('nom_maison')。如果使用POST,则必须使用$ request-> request-> get('nom_maison')

答案 1 :(得分:0)

Maison.php

<?php

namespace MaisonretraiteBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use SBC\NotificationsBundle\Builder\NotificationBuilder;
use SBC\NotificationsBundle\Model\NotifiableInterface;
use Symfony\Component\Validator\Constraints as Assert;


/**
 * Maison
 *
 * @ORM\Table(name="maison", indexes={@ORM\Index(name="id_user", columns={"id_user"})})
 * @ORM\Entity
 */
class Maison implements NotifiableInterface
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id_maison", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $idMaison;

    /**
     * @var string
     *
     * @ORM\Column(name="nom_maison", type="string", length=20, nullable=false)
     */
    private $nomMaison;

    /**
     * @var string
     *
     * @ORM\Column(name="adresse_maison", type="string", length=20, nullable=false)
     * @Assert\NotBlank(message="Le nom est obligatoire")
     */
    private $adresseMaison;

    /**
     * @var string
     *
     * @ORM\Column(name="telephone_maison", type="string", length=8, nullable=false)
     */
    private $telephoneMaison;

    /**
     * @var string
     *
     * @ORM\Column(name="mail_maison", type="string", length=30, nullable=false)
     * @Assert\NotBlank()
     */
    private $mailMaison;

    /**
     * @var integer
     *
     * @ORM\Column(name="nbr_personne", type="integer", nullable=false)
     * @Assert\NotBlank()
     */
    private $nbrPersonne;

    /**
     * @return int
     */
    public function getIdMaison()
    {
        return $this->idMaison;
    }

    /**
     * @param int $idMaison
     */
    public function setIdMaison($idMaison)
    {
        $this->idMaison = $idMaison;
    }

    /**
     * @return string
     */
    public function getNomMaison()
    {
        return $this->nomMaison;
    }

    /**
     * @param string $nomMaison
     */
    public function setNomMaison($nomMaison)
    {
        $this->nomMaison = $nomMaison;
    }

    /**
     * @return string
     */
    public function getAdresseMaison()
    {
        return $this->adresseMaison;
    }

    /**
     * @param string $adresseMaison
     */
    public function setAdresseMaison($adresseMaison)
    {
        $this->adresseMaison = $adresseMaison;
    }

    /**
     * @return string
     */
    public function getTelephoneMaison()
    {
        return $this->telephoneMaison;
    }

    /**
     * @param string $telephoneMaison
     */
    public function setTelephoneMaison($telephoneMaison)
    {
        $this->telephoneMaison = $telephoneMaison;
    }

    /**
     * @return string
     */
    public function getMailMaison()
    {
        return $this->mailMaison;
    }

    /**
     * @param string $mailMaison
     */
    public function setMailMaison($mailMaison)
    {
        $this->mailMaison = $mailMaison;
    }

    /**
     * @return int
     */
    public function getNbrPersonne()
    {
        return $this->nbrPersonne;
    }

    /**
     * @param int $nbrPersonne
     */
    public function setNbrPersonne($nbrPersonne)
    {
        $this->nbrPersonne = $nbrPersonne;
    }

    /**
     * @var \User
     *
     * @ORM\ManyToOne(targetEntity="UserBundle\Entity\User")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="id_user", referencedColumnName="id")
     * })
     */
    private $idUser;

    /**
     * @return \User
     */
    public function getIdUser()
    {
        return $this->idUser;
    }

    /**
     * @param \User $idUser
     */
    public function setIdUser($idUser)
    {
        $this->idUser = $idUser;
    }

    public function notificationsOnCreate(NotificationBuilder $builder)
    {
        $notification = new Notification();
        $notification
            ->setTitle('New Maison')
            ->setDescription($this->nomMaison)
            ->setRoute('affiche_ma')// I suppose you have a show route for your entity
            ->setParameters(array('id' => $this->idMaison))
        ;
        $builder->addNotification($notification);

        return $builder;    }

    public function notificationsOnUpdate(NotificationBuilder $builder)
    {
        $notification = new Notification();
        $notification
            ->setTitle('Maison updated')
            ->setDescription($this->nomMaison)
            ->setRoute('affiche_ma')
            ->setParameters(array('id' => $this->idMaison))
        ;
        $builder->addNotification($notification);

        return $builder;    }

    public function notificationsOnDelete(NotificationBuilder $builder)
    {
        // in case you don't want any notification for a special event
        // you can simply return an empty $builder
        return $builder;    }


}

答案 2 :(得分:0)