本文介绍了Symfony2/主义,通过关系发现了一个新实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在48小时内解决此问题.我有一个实体 BookingRequest.php ,其内容是:

trying to fix this over 48 hours. I have an entity BookingRequest.php which content is:

<?php

namespace Kutiwa\PlatformBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * BookingRequest
 *
 * @ORM\Table(name="booking_request")
 * @ORM\Entity(repositoryClass="Kutiwa\PlatformBundle\Repository\BookingRequestRepository")
 */
class BookingRequest
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="code", type="string", length=255, unique=true)
     */
    private $code;

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

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

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

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

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

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

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

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

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

    /**
     * @ORM\ManyToOne(targetEntity="Kutiwa\PlatformBundle\Entity\City")
     * @ORM\JoinColumn(nullable=false)
     */
    private $destinationCity;

    /**
     * @ORM\ManyToOne(targetEntity="Kutiwa\PlatformBundle\Entity\City")
     * @ORM\JoinColumn(nullable=false)
     */
    private $customerCity;

    /**
     * @ORM\OneToOne(targetEntity="Kutiwa\PlatformBundle\Entity\MissionConfig", cascade={"persist"})
     */
    private $missionConfig;

    /**
     * @ORM\OneToOne(targetEntity="Kutiwa\PlatformBundle\Entity\TourismConfig", cascade={"persist"})
     */
    private $tourismConfig;

    /**
     * @ORM\OneToMany(targetEntity="Kutiwa\PlatformBundle\Entity\RoomsConfig", mappedBy="bookingRequest")
     */
    private $roomsConfigs;


    public function __construct() {
        $this->setRequestDate(date("d/m/Y"));
    }

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

    /**
     * Set code
     *
     * @param string $code
     *
     * @return BookingRequest
     */
    public function setCode($code)
    {
        $this->code = $code;

        return $this;
    }

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

    /**
     * Set arrival
     *
     * @param string $arrival
     *
     * @return BookingRequest
     */
    public function setArrival($arrival)
    {
        $this->arrival = $arrival;

        return $this;
    }

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

    /**
     * Set departure
     *
     * @param string $departure
     *
     * @return BookingRequest
     */
    public function setDeparture($departure)
    {
        $this->departure = $departure;

        return $this;
    }

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

    /**
     * Set requestDate
     *
     * @param string $requestDate
     *
     * @return BookingRequest
     */
    public function setRequestDate($requestDate)
    {
        $this->requestDate = $requestDate;

        return $this;
    }

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

    /**
     * Set rooms
     *
     * @param integer $rooms
     *
     * @return BookingRequest
     */
    public function setRooms($rooms)
    {
        $this->rooms = $rooms;

        return $this;
    }

    /**
     * Get rooms
     *
     * @return integer
     */
    public function getRooms()
    {
        return $this->rooms;
    }

    /**
     * Set minPricing
     *
     * @param string $minPricing
     *
     * @return BookingRequest
     */
    public function setMinPricing($minPricing)
    {
        $this->minPricing = $minPricing;

        return $this;
    }

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

    /**
     * Set maxPricing
     *
     * @param string $maxPricing
     *
     * @return BookingRequest
     */
    public function setMaxPricing($maxPricing)
    {
        $this->maxPricing = $maxPricing;

        return $this;
    }

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

    /**
     * Set customerName
     *
     * @param string $customerName
     *
     * @return BookingRequest
     */
    public function setCustomerName($customerName)
    {
        $this->customerName = $customerName;

        return $this;
    }

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

    /**
     * Set customerEmail
     *
     * @param string $customerEmail
     *
     * @return BookingRequest
     */
    public function setCustomerEmail($customerEmail)
    {
        $this->customerEmail = $customerEmail;

        return $this;
    }

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

    /**
     * Set customerPhone
     *
     * @param string $customerPhone
     *
     * @return BookingRequest
     */
    public function setCustomerPhone($customerPhone)
    {
        $this->customerPhone = $customerPhone;

        return $this;
    }

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

    /**
     * Set destinationCity
     *
     * @param \Kutiwa\PlatformBundle\Entity\City $destinationCity
     *
     * @return BookingRequest
     */
    public function setDestinationCity(\Kutiwa\PlatformBundle\Entity\City $destinationCity)
    {
        $this->destinationCity = $destinationCity;

        return $this;
    }

    /**
     * Get destinationCity
     *
     * @return \Kutiwa\PlatformBundle\Entity\City
     */
    public function getDestinationCity()
    {
        return $this->destinationCity;
    }

    /**
     * Set customerCity
     *
     * @param \Kutiwa\PlatformBundle\Entity\City $customerCity
     *
     * @return BookingRequest
     */
    public function setCustomerCity(\Kutiwa\PlatformBundle\Entity\City $customerCity)
    {
        $this->customerCity = $customerCity;

        return $this;
    }

    /**
     * Get customerCity
     *
     * @return \Kutiwa\PlatformBundle\Entity\City
     */
    public function getCustomerCity()
    {
        return $this->customerCity;
    }

    /**
     * Set missionConfig
     *
     * @param \Kutiwa\PlatformBundle\Entity\MissionConfig $missionConfig
     *
     * @return BookingRequest
     */
    public function setMissionConfig(\Kutiwa\PlatformBundle\Entity\MissionConfig $missionConfig = null)
    {
        $this->missionConfig = $missionConfig;

        return $this;
    }

    /**
     * Get missionConfig
     *
     * @return \Kutiwa\PlatformBundle\Entity\MissionConfig
     */
    public function getMissionConfig()
    {
        return $this->missionConfig;
    }

    /**
     * Set tourismConfig
     *
     * @param \Kutiwa\PlatformBundle\Entity\TourismConfig $tourismConfig
     *
     * @return BookingRequest
     */
    public function setTourismConfig(\Kutiwa\PlatformBundle\Entity\TourismConfig $tourismConfig = null)
    {
        $this->tourismConfig = $tourismConfig;

        return $this;
    }

    /**
     * Get tourismConfig
     *
     * @return \Kutiwa\PlatformBundle\Entity\TourismConfig
     */
    public function getTourismConfig()
    {
        return $this->tourismConfig;
    }

    /**
     * Add roomsConfig
     *
     * @param \Kutiwa\PlatformBundle\Entity\RoomsConfig $roomsConfig
     *
     * @return BookingRequest
     */
    public function addRoomsConfig(\Kutiwa\PlatformBundle\Entity\RoomsConfig $roomsConfig)
    {
        $this->roomsConfigs[] = $roomsConfig;

        return $this;
    }

    /**
     * Remove roomsConfig
     *
     * @param \Kutiwa\PlatformBundle\Entity\RoomsConfig $roomsConfig
     */
    public function removeRoomsConfig(\Kutiwa\PlatformBundle\Entity\RoomsConfig $roomsConfig)
    {
        $this->roomsConfigs->removeElement($roomsConfig);
    }

    /**
     * Get roomsConfigs
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getRoomsConfigs()
    {
        return $this->roomsConfigs;
    }
}

和另一个实体 RoomsConfig.php

<?php

namespace Kutiwa\PlatformBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * RoomsConfig
 *
 * @ORM\Table(name="rooms_config")
 * @ORM\Entity(repositoryClass="Kutiwa\PlatformBundle\Repository\RoomsConfigRepository")
 */
class RoomsConfig
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

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

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

    /**
     * @var string
     *
     * @ORM\Column(name="tv", type="boolean")
     */
    private $tv;

    /**
     * @ORM\ManyToOne(targetEntity="Kutiwa\PlatformBundle\Entity\BookingRequest", inversedBy="roomsConfigs", cascade={"persist"})
     * @ORM\JoinColumn(nullable=false)
     */
    private $bookingRequest;


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



    /**
     * Set airConditionner
     *
     * @param boolean $airConditionner
     *
     * @return RoomsConfig
     */
    public function setAirConditionner($airConditionner)
    {
        $this->airConditionner = $airConditionner;

        return $this;
    }

    /**
     * Get airConditionner
     *
     * @return boolean
     */
    public function getAirConditionner()
    {
        return $this->airConditionner;
    }

    /**
     * Set wifi
     *
     * @param boolean $wifi
     *
     * @return RoomsConfig
     */
    public function setWifi($wifi)
    {
        $this->wifi = $wifi;

        return $this;
    }

    /**
     * Get wifi
     *
     * @return boolean
     */
    public function getWifi()
    {
        return $this->wifi;
    }

    /**
     * Set balcony
     *
     * @param boolean $balcony
     *
     * @return RoomsConfig
     */
    public function setBalcony($balcony)
    {
        $this->balcony = $balcony;

        return $this;
    }

    /**
     * Get balcony
     *
     * @return boolean
     */
    public function getBalcony()
    {
        return $this->balcony;
    }

    /**
     * Set tv
     *
     * @param boolean $tv
     *
     * @return RoomsConfig
     */
    public function setTv($tv)
    {
        $this->tv = $tv;

        return $this;
    }

    /**
     * Get tv
     *
     * @return boolean
     */
    public function getTv()
    {
        return $this->tv;
    }

    /**
     * Set bookingRequest
     *
     * @param \Kutiwa\PlatformBundle\Entity\BookingRequest $bookingRequest
     *
     * @return RoomsConfig
     */
    public function setBookingRequest(\Kutiwa\PlatformBundle\Entity\BookingRequest $bookingRequest)
    {
        $this->bookingRequest = $bookingRequest;

        $bookingRequest->addRoomsConfig($this);

        return $this;
    }

    /**
     * Get bookingRequest
     *
     * @return \Kutiwa\PlatformBundle\Entity\BookingRequest
     */
    public function getBookingRequest()
    {
        return $this->bookingRequest;
    }
}

问题是,当我尝试保留RoomsConfig实体时,出现错误

The problem is, when I try to persist a RoomsConfig entity, I got an error

我不需要城市和BookingRequest关系的级联效应,这是我的 BookingRequestType.php

I don't need a cascade effect on City and BookingRequest relation, here is my BookingRequestType.php

<?php

namespace Kutiwa\PlatformBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\OptionsResolver\OptionsResolver;

class BookingRequestType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('arrival', TextType::class)
            ->add('departure', TextType::class)
            ->add('rooms', IntegerType::class)
            ->add('customerName', TextType::class)
            ->add('customerEmail', TextType::class, array('required' => false))
            ->add('customerPhone', TextType::class)
            ->add('destinationCity', EntityType::class, array(
                'choice_label' => 'name',
                'class' => 'KutiwaPlatformBundle:City'
            ))
            ->add('customerCity', EntityType::class, array(
                'choice_label' => 'name',
                'class' => 'KutiwaPlatformBundle:City'
            ));
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Kutiwa\PlatformBundle\Entity\BookingRequest'
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'kutiwa_platformbundle_bookingrequest';
    }


}

如果我尝试仅保留BookingRequest实体,则没有问题,但是当我保留RoomsConfig实体时,出现此错误.请帮助.ks.

If I try to persist only BookingRequest entity, there is no problem, but when I persist RoomsConfig entity, I got that error. Help please. Thks.

推荐答案

在持久保存 RoomsConfig 之前,请尝试使用 findOneBy 查找 BookingRequest ,或用于查找实体的任何方法,然后使用 setBookingRequest 设置找到的实体.

Before persist your RoomsConfig, try to use findOneBy to find BookingRequest, or whatever you use to find the entity, and then set the found entity with setBookingRequest.

这篇关于Symfony2/主义,通过关系发现了一个新实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 09:50