DDC-789: missing check for valid 'mappedBy' mapping is causing E_FATAL #971

Closed
opened 2026-01-22 12:57:43 +01:00 by admin · 4 comments
Owner

Originally created by @doctrinebot on GitHub (Sep 4, 2010).

Jira issue originally created by user jpieper:

These errors (see below) occur because the "mappedBy" value for "articles" field in my "Author" entity is wrong. It must be "author" instead of "author_id".

You need to check whether the the field, which has been defined by "mappedBy", is existent within the target entity. If not, throw an exception.


namespace Entites;

/*** @Entity @Table(name="author") **/
class Author {
    /*** @OneToMany(targetEntity="Article", mappedBy="author_id") **/
    private $articles;
}```

```<?php

namespace Entites;

/*** @Entity @Table(name="article") **/
class Article {
    /*** @ManyToOne(targetEntity="Author", inversedBy="articles") **/
    private $author;
}```

```$em->find('Entities\Author', $authorId)->getArticles();```

```Notice: Undefined index: author_id in C:\Server\php\include\Doctrine\ORM\lib\Doctrine\ORM\Persisters\BasicEntityPersister.php on line 1149

Warning: Invalid argument supplied for foreach() in C:\Server\php\include\Doctrine\ORM\lib\Doctrine\ORM\Persisters\BasicEntityPersister.php on line 1151

Notice: Undefined index: author_id in C:\Server\php\include\Doctrine\ORM\lib\Doctrine\ORM\PersistentCollection.php on line 167

Fatal error: Call to a member function setValue() on a non-object in C:\Server\php\include\Doctrine\ORM\lib\Doctrine\ORM\PersistentCollection.php on line 168```

```index 9fa5906..30ec3ac 100644
--- a/lib/Doctrine/ORM/Mapping/MappingException.php
<ins></ins><ins> b/lib/Doctrine/ORM/Mapping/MappingException.php
@@ -78,6 </ins>78,11 @@ class MappingException extends \Doctrine\ORM\ORMException
         return new self("OneToMany mapping on field '$fieldName' requires the 'mappedBy' attribute.");
     }

<ins>    public static function oneToManyRequiresMatchingMappedBy($entityName, $fieldName)
</ins>    {
<ins>        return new self("OneToMany mapping on field '$fieldName' requires matching 'mappedBy' field in '$entityName'");
</ins>    }
<ins>
     public static function joinTableRequired($fieldName)
     {
         return new self("The mapping of field '$fieldName' requires an the 'joinTable' attribute.");
diff --git a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php
index 93f6efa..b4c974c 100644
--- a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php
</ins><ins></ins> b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php
@@ -1146,6 <ins>1146,11 @@ class BasicEntityPersister
     public function loadOneToManyCollection(array $assoc, $sourceEntity, PersistentCollection $coll)
     {
         $criteria = array();
</ins>        if (!isset($this->_class->associationMappings[$assoc['mappedBy']])) {
<ins>            throw MappingException::oneToManyRequiresMatchingMappedBy(
</ins>                $assoc['targetEntity'], $assoc['fieldName']
<ins>            );
</ins>        }
         $owningAssoc = $this->_class->associationMappings[$assoc['mappedBy']];
         $sourceClass = $this->_em->getClassMetadata($assoc['sourceEntity']);
         foreach ($owningAssoc['targetToSourceKeyColumns'] as $sourceKeyColumn => $targetKeyColumn) {```
Originally created by @doctrinebot on GitHub (Sep 4, 2010). Jira issue originally created by user jpieper: These errors (see below) occur because the "mappedBy" value for "articles" field in my "Author" entity is wrong. It must be "author" instead of "author_id". You need to check whether the the field, which has been defined by "mappedBy", is existent within the target entity. If not, throw an exception. `````` <?php namespace Entites; /*** @Entity @Table(name="author") **/ class Author { /*** @OneToMany(targetEntity="Article", mappedBy="author_id") **/ private $articles; }``` ```<?php namespace Entites; /*** @Entity @Table(name="article") **/ class Article { /*** @ManyToOne(targetEntity="Author", inversedBy="articles") **/ private $author; }``` ```$em->find('Entities\Author', $authorId)->getArticles();``` ```Notice: Undefined index: author_id in C:\Server\php\include\Doctrine\ORM\lib\Doctrine\ORM\Persisters\BasicEntityPersister.php on line 1149 Warning: Invalid argument supplied for foreach() in C:\Server\php\include\Doctrine\ORM\lib\Doctrine\ORM\Persisters\BasicEntityPersister.php on line 1151 Notice: Undefined index: author_id in C:\Server\php\include\Doctrine\ORM\lib\Doctrine\ORM\PersistentCollection.php on line 167 Fatal error: Call to a member function setValue() on a non-object in C:\Server\php\include\Doctrine\ORM\lib\Doctrine\ORM\PersistentCollection.php on line 168``` ```index 9fa5906..30ec3ac 100644 --- a/lib/Doctrine/ORM/Mapping/MappingException.php <ins></ins><ins> b/lib/Doctrine/ORM/Mapping/MappingException.php @@ -78,6 </ins>78,11 @@ class MappingException extends \Doctrine\ORM\ORMException return new self("OneToMany mapping on field '$fieldName' requires the 'mappedBy' attribute."); } <ins> public static function oneToManyRequiresMatchingMappedBy($entityName, $fieldName) </ins> { <ins> return new self("OneToMany mapping on field '$fieldName' requires matching 'mappedBy' field in '$entityName'"); </ins> } <ins> public static function joinTableRequired($fieldName) { return new self("The mapping of field '$fieldName' requires an the 'joinTable' attribute."); diff --git a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php index 93f6efa..b4c974c 100644 --- a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php </ins><ins></ins> b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php @@ -1146,6 <ins>1146,11 @@ class BasicEntityPersister public function loadOneToManyCollection(array $assoc, $sourceEntity, PersistentCollection $coll) { $criteria = array(); </ins> if (!isset($this->_class->associationMappings[$assoc['mappedBy']])) { <ins> throw MappingException::oneToManyRequiresMatchingMappedBy( </ins> $assoc['targetEntity'], $assoc['fieldName'] <ins> ); </ins> } $owningAssoc = $this->_class->associationMappings[$assoc['mappedBy']]; $sourceClass = $this->_em->getClassMetadata($assoc['sourceEntity']); foreach ($owningAssoc['targetToSourceKeyColumns'] as $sourceKeyColumn => $targetKeyColumn) {``` ``````
admin added the Bug label 2026-01-22 12:57:43 +01:00
admin closed this issue 2026-01-22 12:57:44 +01:00
Author
Owner

@doctrinebot commented on GitHub (Sep 4, 2010):

Comment created by @beberlei:

This cannot be done reliably at run-time, this is why there exists the tool:

"doctrine orm:validate-mappings"

@doctrinebot commented on GitHub (Sep 4, 2010): Comment created by @beberlei: This cannot be done reliably at run-time, this is why there exists the tool: "doctrine orm:validate-mappings"
Author
Owner

@doctrinebot commented on GitHub (Sep 4, 2010):

Issue was closed with resolution "Fixed"

@doctrinebot commented on GitHub (Sep 4, 2010): Issue was closed with resolution "Fixed"
Author
Owner

@doctrinebot commented on GitHub (Sep 4, 2010):

Comment created by jpieper:

I think it is "orm:validate-schema" because there is no "orm:validate-mappings".


[Mapping]  FAIL - The entity-class 'Entities\Article' mapping is invalid:
- The mappings Entities\Article#author and Entities\Author#articles are incosistent with each other.

[Mapping]  FAIL - The entity-class 'Entities\Author' mapping is invalid:
- The association Entities\Author#articles refers to the owning side field Entities\Article#author_id which does not exist.

[Database] OK - The database schema is in sync with the mapping files.```
@doctrinebot commented on GitHub (Sep 4, 2010): Comment created by jpieper: I think it is "orm:validate-schema" because there is no "orm:validate-mappings". ```$ doctrine orm:validate-schema [Mapping] FAIL - The entity-class 'Entities\Article' mapping is invalid: - The mappings Entities\Article#author and Entities\Author#articles are incosistent with each other. [Mapping] FAIL - The entity-class 'Entities\Author' mapping is invalid: - The association Entities\Author#articles refers to the owning side field Entities\Article#author_id which does not exist. [Database] OK - The database schema is in sync with the mapping files.```
Author
Owner

@doctrinebot commented on GitHub (Sep 4, 2010):

Comment created by @beberlei:

Yes that i meant.

@doctrinebot commented on GitHub (Sep 4, 2010): Comment created by @beberlei: Yes that i meant.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#971