Symfony中的Doctrine映射问题

时间:2016-09-20 21:08:16

标签: orm doctrine-orm symfony

我一直在使用Doctrine ORM在Symfony 3中编写项目。我有一个名为' Race'的主要实体,其中包含一些字段以及与另一个实体的关系,例如' RaceRelationToType' RaceRelationToOrganizer'等等 - 您可以在数据库模式的图片中看到它。 (仅用于信息,' RaceRelationTo *'实体是阻止更改原始数据的实体。)

问题:当我使用 $ this-> em-> getRepository(' AppBundle:Race')从数据库中获取数据时出现问题 - &gt ;的findAll(); 。数据库中的数据存储正确,但如果我获取它们,则它们的数据类型错误。 raceRelationToOrganizer变量应该是RaceRelationToOrganizer的数据类型,而不是RaceRelationToSeason等等。

enter image description here

在重置数据库之前,一切正常。当我将新种族插入已清除的数据库时,raceRelationToSeason的id为1,raceRelationToType为1,raceRelationToOrganizer为1 ans等。重置数据库(和重置自动增量)后出现问题。

如果所有关系的ID都不同,那么它的工作正常。魔法?错误?有人知道该怎么办吗?你能在下面的文件中找到一些问题吗?我一直试图解决它几个小时。重要文件的内容如下所示。

enter image description here

我使用yaml配置文件进行ORM映射。 Race.orm.yml 包含:

AppBundle\Entity\Race:
    type:  entity
    table: race
    id:
        id:
            type: integer
            generator:
                strategy: AUTO
    fields:
        registrationNumberOfRace:
            column: registration_number_of_race
            type: integer
            length: 4
            nullable: false
        dateFrom:
            column: date_from
            type: date
            nullable: false
        dateTo:
            column: date_to
            type: date
            nullable: false
    manyToOne:
        raceRelationToSeason:
            targetEntity: AppBundle\Entity\RaceRelationToSeason
            joinColumn:
                name: race_relation_to_season
                referencedColumnName: id
                onDelete: RESTRICT
                nullable: false
            fetch: EAGER
            cascade: [ persist,remove ]
        raceRelationToType:
            targetEntity: AppBundle\Entity\RaceRelationToType
            joinColumn:
                name: race_relation_to_type
                referencedColumnName: id
                onDelete: RESTRICT
                nullable: false
            fetch: EAGER
            cascade: [ persist,remove ]
        raceRelationToOrganizer:
            targetEntity: AppBundle\Entity\RaceRelationToOrganizer
            joinColumn:
                name: race_relation_to_organizer
                referencedColumnName: id
                onDelete: RESTRICT
                nullable: false
            fetch: EAGER
            cascade: [ persist,remove ]
        raceRelationToVenue:
            targetEntity: AppBundle\Entity\RaceRelationToVenue
            joinColumn:
                name: race_relation_to_venue
                referencedColumnName: id
                onDelete: RESTRICT
                nullable: false
            fetch: EAGER
            cascade: [ persist,remove ]

实体 Race.php包含

<?php

namespace AppBundle\Entity;

/**
 * Class Race
 * @package AppBundle\Entity
 */
class Race
{
    /**
     * @var integer Id of the race
     */
    protected $id;
    /**
     * @var SeasonOfRace
     */
    protected $seasonOfRace;
    /**
     * @var RaceRelationToSeason
     */
    protected $raceRelationToSeason;
    /**
     * @var TypeOfRace
     */
    protected $typeOfRace;
    /**
     * @var RaceRelationToType
     */
    protected $raceRelationToType;
    /**
     * @var integer Registration number of the race which depend on the type of race
     */
    protected $registrationNumberOfRace;
    /**
     * @var OrganizerOfRace
     */
    protected $organizerOfRace;
    /**
     * @var RaceRelationToOrganizer
     */
    protected $raceRelationToOrganizer;
    /**
     * @var VenueOfRace
     */
    protected $venueOfRace;
    /**
     * @var RaceRelationToVenue
     */
    protected $raceRelationToVenue;
    /**
     * @var \DateTime
     */
    protected $dateFrom;
    /**
     * @var \DateTime
     */
    protected $dateTo;

    /**
     * Race constructor.
     */
    public function __construct()
    {
        $this->dateFrom = $this->dateTo = new \DateTime();
    }

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

    /**
     * @param $id
     * @return $this
     */
    public function setId($id)
    {
        $this->id = $id;

        return $this;
    }

    /**
     * @return SeasonOfRace
     */
    public function getSeasonOfRace()
    {
        if ($this->raceRelationToSeason) {
            if ($this->raceRelationToSeason->getOriginal()) {
                $this->seasonOfRace = $this->raceRelationToSeason->getOriginal();

                return $this->seasonOfRace;
            } else {
                return $this->seasonOfRace;
            }
        } else {
            return $this->seasonOfRace;
        }
    }

    /**
     * @param SeasonOfRace $seasonOfRace
     * @return Race
     */
    public function setSeasonOfRace(SeasonOfRace $seasonOfRace)
    {
        $this->seasonOfRace = $seasonOfRace;

        if ($this->seasonOfRace) {
            if ($this->raceRelationToSeason) {
                $this->raceRelationToSeason->setLabel($this->seasonOfRace->getLabel());
                $this->raceRelationToSeason->setShortLabel($this->seasonOfRace->getShortLabel());
                $this->raceRelationToSeason->setOriginal($this->seasonOfRace);
            } else {
                $this->raceRelationToSeason = new RaceRelationToSeason();
                $this->raceRelationToSeason->setLabel($this->seasonOfRace->getLabel());
                $this->raceRelationToSeason->setShortLabel($this->seasonOfRace->getShortLabel());
                $this->raceRelationToSeason->setOriginal($this->seasonOfRace);
            }
        }

        return $this;
    }

    /**
     * @return RaceRelationToSeason
     */
    public function getRaceRelationToSeason()
    {
        return $this->raceRelationToSeason;
    }

    /**
     * @param RaceRelationToSeason $raceRelationToSeason
     * @return Race
     */
    public function setRaceRelationToSeason(RaceRelationToSeason $raceRelationToSeason)
    {
        $this->raceRelationToSeason = $raceRelationToSeason;

        return $this;
    }

    /**
     * @return TypeOfRace
     */
    public function getTypeOfRace()
    {
        if ($this->raceRelationToType) {
            if ($this->raceRelationToType->getOriginal()) {
                $this->typeOfRace = $this->raceRelationToType->getOriginal();

                return $this->typeOfRace;
            } else {
                return $this->typeOfRace;
            }
        } else {
            return $this->typeOfRace;
        }
    }

    /**
     * @param TypeOfRace $typeOfRace
     * @return Race
     */
    public function setTypeOfRace(TypeOfRace $typeOfRace)
    {
        $this->typeOfRace = $typeOfRace;

        if ($this->typeOfRace) {
            if ($this->raceRelationToType) {
                $this->raceRelationToType->setLabel($this->typeOfRace->getLabel());
                $this->raceRelationToType->setShortLabel($this->typeOfRace->getShortLabel());
                $this->raceRelationToType->setOriginal($this->typeOfRace);
            } else {
                $this->raceRelationToType = new RaceRelationToType();
                $this->raceRelationToType->setLabel($this->typeOfRace->getLabel());
                $this->raceRelationToType->setShortLabel($this->typeOfRace->getShortLabel());
                $this->raceRelationToType->setOriginal($this->typeOfRace);
            }
        }

        return $this;
    }

    /**
     * @return RaceRelationToType
     */
    public function getRaceRelationToType()
    {
        return $this->raceRelationToType;
    }

    /**
     * @param RaceRelationToType $raceRelationToType
     * @return Race
     */
    public function setRaceRelationToType(RaceRelationToType $raceRelationToType)
    {
        $this->raceRelationToType = $raceRelationToType;

        return $this;
    }

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

    /**
     * @param $registrationNumberOfRace
     * @return Race
     */
    public function setRegistrationNumberOfRace($registrationNumberOfRace)
    {
        $this->registrationNumberOfRace = $registrationNumberOfRace;

        return $this;
    }

    /**
     * @return VenueOfRace
     */
    public function getVenueOfRace()
    {
        if ($this->raceRelationToVenue) {
            if ($this->raceRelationToVenue->getOriginal()) {
                $this->venueOfRace = $this->raceRelationToVenue->getOriginal();

                return $this->venueOfRace;
            } else {
                return $this->venueOfRace;
            }
        } else {
            return $this->venueOfRace;
        }
    }

    /**
     * @param VenueOfRace $venueOfRace
     * @return Race
     */
    public function setVenueOfRace(VenueOfRace $venueOfRace)
    {
        $this->venueOfRace = $venueOfRace;

        if ($this->venueOfRace) {
            if ($this->raceRelationToVenue) {
                $this->raceRelationToVenue->setLabel($this->venueOfRace->getLabel());
                $this->raceRelationToVenue->setShortLabel($this->venueOfRace->getShortLabel());
                $this->raceRelationToVenue->setOriginal($this->venueOfRace);
            } else {
                $this->raceRelationToVenue = new RaceRelationToVenue();
                $this->raceRelationToVenue->setLabel($this->venueOfRace->getLabel());
                $this->raceRelationToVenue->setShortLabel($this->venueOfRace->getShortLabel());
                $this->raceRelationToVenue->setOriginal($this->venueOfRace);
            }
        }

        return $this;
    }

    /**
     * @return RaceRelationToVenue
     */
    public function getRaceRelationToVenue()
    {
        return $this->raceRelationToVenue;
    }

    /**
     * @param RaceRelationToVenue $raceRelationToVenue
     * @return Race
     */
    public function setRaceRelationToVenue(RaceRelationToVenue $raceRelationToVenue)
    {
        $this->raceRelationToVenue = $raceRelationToVenue;

        return $this;
    }

    /**
     * @return OrganizerOfRace
     */
    public function getOrganizerOfRace()
    {
        if ($this->raceRelationToOrganizer) {
            if ($this->raceRelationToOrganizer->getOriginal()) {
                $this->organizerOfRace = $this->raceRelationToOrganizer->getOriginal();

                return $this->organizerOfRace;
            } else {
                return $this->organizerOfRace;
            }
        } else {
            return $this->organizerOfRace;
        }
    }

    /**
     * @param OrganizerOfRace $organizerOfRace
     * @return Race
     */
    public function setOrganizerOfRace(OrganizerOfRace $organizerOfRace)
    {
        $this->organizerOfRace = $organizerOfRace;

        if ($this->organizerOfRace) {
            if ($this->raceRelationToOrganizer) {
                $this->raceRelationToOrganizer->setLabel($this->organizerOfRace->getLabel());
                $this->raceRelationToOrganizer->setShortLabel($this->organizerOfRace->getShortLabel());
                $this->raceRelationToOrganizer->setOriginal($this->organizerOfRace);
            } else {
                $this->raceRelationToOrganizer = new RaceRelationToOrganizer();
                $this->raceRelationToOrganizer->setLabel($this->organizerOfRace->getLabel());
                $this->raceRelationToOrganizer->setShortLabel($this->organizerOfRace->getShortLabel());
                $this->raceRelationToOrganizer->setOriginal($this->organizerOfRace);
            }
        }

        return $this;
    }

    /**
     * @return RaceRelationToOrganizer
     */
    public function getRaceRelationToOrganizer()
    {
        return $this->raceRelationToOrganizer;
    }

    /**
     * @param RaceRelationToOrganizer $raceRelationToOrganizer
     * @return Race
     */
    public function setRaceRelationToOrganizer(RaceRelationToOrganizer $raceRelationToOrganizer)
    {
        $this->raceRelationToOrganizer = $raceRelationToOrganizer;

        return $this;
    }

    /**
     * @return \DateTime
     */
    public function getDateFrom()
    {
        return $this->dateFrom;
    }

    /**
     * @param \DateTime $dateFrom
     * @return $this
     */
    public function setDateFrom(\DateTime $dateFrom)
    {
        $this->dateFrom = $dateFrom;

        return $this;
    }

    /**
     * @return \DateTime
     */
    public function getDateTo()
    {
        return $this->dateTo;
    }

    /**
     * @param \DateTime $dateTo
     */
    public function setDateTo(\DateTime $dateTo)
    {
        $this->dateTo = $dateTo;
    }
}

1 个答案:

答案 0 :(得分:2)

我在23个小时后完全解决了,这是我的错。我想对那些花时间试图帮助我的人说声抱歉。

正如您在上面的数据库模式中所看到的,有一个表RaceRelationToType,它是名为Race的表和名为TypeOfRace的表之间的表。此表在此处是因为当您从TypeOfRace中删除原始数据时,数据仍存储在RaceRelationToType +中的副本中 - 这是因为存档数据。与使用RaceRelationToXXX开始的另一个表相同。

错误在RaceRelationToOrganizer.php实体中。我使用VenueOfRace实体而不是OrganizerOfRace实体扩展了RaceRelationToOrganizer实体。我不知道为什么,但它之前有效,但在数据库重置后没有。

所以没有显示错误,但是Doctrine并不知道如何处理它(所有这些表之间的字段是相同的)并且它映射了随机数据类型。这就是全部。

谢谢你,抱歉!