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

Open
opened 2026-01-22 12:57:39 +01:00 by admin · 0 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:39 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#969