Fernando Silveira
Fernando Silveira

Reputation: 23

another Doctrine mapping error

I already read many other questions and answers and my classes seem to look OK, but still I get the 'The association Namespace\Class2#property1 refers to the owning side field Namespace\Class1#property2 which does not exist' error.

First class:

use Doctrine\Common\Collections\ArrayCollection;

/**
 * @Entity
 * @Table(name="projetcs", options={"collate"="utf8mb4_unicode_ci", "charset"="utf8mb4"})
 */
class Project
{
    /**
     * @var int
     * @Id
     * @GeneratedValue
     * @Column(type="integer", options={"unsigned":true})
     */
    protected $id;
    /**
     * @var ArrayCollection
     * @OneToMany(targetEntity="Release", mappedBy="project")
     */
    protected $releases;

    /**
     * Construtor.
     */
    public function __construct()
    {
        $this->releases = new ArrayCollection();
    }

    /**
     * Add release.
     *
     * @param \Hitec\HDS\Entity\Release $release
     *
     * @return Project
     */
    public function addRelease(\Hitec\HDS\Entity\Release $release)
    {
        $this->releases[] = $release;

        return $this;
    }

    /**
     * Remove release.
     *
     * @param \Hitec\HDS\Entity\Release $release
     */
    public function removeRelease(\Hitec\HDS\Entity\Release $release)
    {
        $this->releases->removeElement($release);
    }

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

Second class:

/**
 * @MappedSuperclass
 */
class Release
{
    /**
     * @var int
     * @Id
     * @GeneratedValue
     * @Column(type="integer", options={"unsigned":true})
     */
    protected $id;
    /**
     * @var \Hitec\HDS\Entity\Project
     * @ManyToOne(targetEntity="Project", inversedBy="releases")
     * @JoinColumn(name="project_id", referencedColumnName="id")
     */
    protected $project;

    /**
     * Set project.
     *
     * @param \Hitec\HDS\Entity\Project $project
     *
     * @return Release
     */
    public function setProject(\Hitec\HDS\Entity\Project $project)
    {
        $project->addRelease($this);
        $this->project = $project;

        return $this;
    }

    /**
     * Get project.
     *
     * @return \Hitec\HDS\Entity\Project
     */
    public function getProject()
    {
        return $this->project;
    }
}

When running doctrine orm:validate-schema, I get:

[Mapping] FAIL - The entity-class 'Hitec\HDS\Entity\Project' mapping is invalid:

May this be related to the fact second class is a MappedSuperclass?

Upvotes: 0

Views: 49

Answers (1)

Stepashka
Stepashka

Reputation: 2698

@MappedSuperclass is not the entity. You can not refer to it. Why do you wanted to do it this way?

In order to make things work you either should extend it with some @Entity

class Project
{
//... rest of the code
    /**
     * @var ArrayCollection
     * @OneToMany(targetEntity="ConcreteRelease", mappedBy="project")
     */
    protected $releases;
//... rest of the code
}

/**
 * @Entity
 */
class ConcreteRelease extends Release {
// ... rest of the code
}

Or change the annotation of Release to make it @Entity.

Ex:

/**
 * @Entity
 * @Table(name="release")
 */
class Release {...}

Another option is to use discriminator field in the DB. See doctrine doc for details:

class Project
{
//... rest of the code
    /**
     * @var ArrayCollection
     * @OneToMany(targetEntity="AppRelease", mappedBy="project")
     */
    protected $releases;
//... rest of the code
}

/**
 * @Entity
 * @InheritanceType("SINGLE_TABLE")
 * @DiscriminatorColumn(name="discr", type="string")
 * @DiscriminatorMap({"app" = "AppRelease", "package" = "PackageRelease"})
 * @Table(name="release")
 */
class AppRelease {...}

/**
 * @Entity
 */
class PackageRelease {...}

Upvotes: 1

Related Questions