Symfony2 / Doctrine,通过关系发现了一个新的实体

时间:2017-05-03 22:56:45

标签: php symfony doctrine-orm

试图在48小时内解决这个问题。我有一个实体 BookingRequest.php ,内容是:

<?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实体时,我收到了一个错误

  

通过这种关系找到了一个新的实体   'Kutiwa \ PlatformBundle \ Entity \ BookingRequest #destinationCity'即是   未配置为级联实体的持久操作:TIKO。至   解决此问题:显式调用EntityManager#persist()on   这个未知的实体或配置级联持久存在此关联   映射例如@ManyToOne(..,cascade = {“persist”})

我不需要对City和BookingRequest关系进行级联效果,这是我的 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实体时,我收到了该错误。请帮忙。 THKS。

1 个答案:

答案 0 :(得分:1)

在坚持 RoomsConfig 之前,请尝试使用findOneBy查找 BookingRequest ,或者您用于查找实体的任何内容,然后设置找到的实体setBookingRequest