Bedřich Schindler
Bedřich Schindler

Reputation: 256

Problems with Doctrine mapping in Symfony

I have been programming a project in the Symfony 3 using the Doctrine ORM. I have main entity called 'Race', which contains some fields and relations to another entities like 'RaceRelationToType', 'RaceRelationToOrganizer' and so on - you can see it in the picture of the database schema bellow. (Only for information, 'RaceRelationTo*' entities are entities which prevents change of original data.)

The problem: The problem becames when I fetch data from database using $this->em->getRepository('AppBundle:Race')->findAll();. Data in database are stored correctly, but If I fetch them, they have wrong data type. raceRelationToOrganizer variable should be of data type RaceRelationToOrganizer, not RaceRelationToSeason and so on.

enter image description here

Everything had worked right until I reset a database. When I insert new race to cleared database, id of raceRelationToSeason is 1, raceRelationToType is 1, raceRelationToOrganizer is 1 ans so on. Problems occured after reset of database (and reset of auto increment).

If ids of all relations are different, than it works OK. Magic? Error? Does anybody know what to do with it? Can you find some problem in files bellow? I have been trying to solve it for hours. Contents of important files are bellow.

enter image description here

I use yaml configuration files for ORM mapping. Race.orm.yml contains:

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 ]

Entity Race.php contains:

<?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;
    }
}

Upvotes: 1

Views: 475

Answers (1)

Bedřich Schindler
Bedřich Schindler

Reputation: 256

I solved it exactly after 23 hours, it was my fault. I want to say sorry to all who have spent their time with trying to help me.

As you can see in the database schema above, there is the table RaceRelationToType that is table between table called Race and table called TypeOfRace. This table is here because when you remove original data from TypeOfRace, data are still stored in a copy in RaceRelationToType+ - it is because of archiving data. It is same with another tables beggining with RaceRelationToXXX.

Mistake was in RaceRelationToOrganizer.php entity. I had extended RaceRelationToOrganizer entity with VenueOfRace entity instead of OrganizerOfRace entity. I don't know why, but It had worked before, but not after reset of database.

So there was no error shown, but Doctrine didn't know what to do with it (fields in all this between-tables are identical) and it mapped random data types. So that's all.

Thank you and sorry!

Upvotes: 2

Related Questions