Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-762

undefined index errors when the join-column of a relation is NULL

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.0-BETA4
    • Fix Version/s: 2.0-RC1
    • Component/s: ORM
    • Security Level: All
    • Labels:
      None

      Description

      When the value of the join-column of a relation is NULL, "undefined index" errors are thrown in UnitOfWork::createEntity().

      The problem is in BasicEntityPersister::_processSQLResult() ; the attached diff fixed the problem for me.

      I reproduce the problem with a code like this :
      This triggers an error when the value of the column b_id is NULL.

      <?php
      
      class A {
        @Column(name="b_id", type="integer")
        private $bId;
      
        @OneToOne(targetEntity="B")
        @JoinColumn(name="b_id", referencedColumnName="id")
        private $b;
      }
      
      $entityManager->find('A', 1);
      ?>
      

        Activity

        Hide
        Benjamin Eberlei added a comment -

        fixed formating

        Show
        Benjamin Eberlei added a comment - fixed formating
        Hide
        Benjamin Eberlei added a comment -

        This should either be fixed in MASTER, or is not an issue (redefine of the same column as key and relation). This will really only be possible with the experimental DDC-117 branch that will be merged for 2.1

        Show
        Benjamin Eberlei added a comment - This should either be fixed in MASTER, or is not an issue (redefine of the same column as key and relation). This will really only be possible with the experimental DDC-117 branch that will be merged for 2.1
        Hide
        arnaud-lb added a comment -

        > not an issue (redefine of the same column as key and relation)

        My example was not clear enough, the column is not a key.

        <?php
        
        class A {
        
          @Id
          private $id;
        
          // apart being a foreign key, this is not a key of A
          @Column(name="b_id", type="integer")
          private $bId;
        
          @OneToOne(targetEntity="B")
          @JoinColumn(name="b_id", referencedColumnName="id")
          private $b;
        }
        
        $entityManager->find('A', 1);
        ?>
        
        Show
        arnaud-lb added a comment - > not an issue (redefine of the same column as key and relation) My example was not clear enough, the column is not a key. <?php class A { @Id private $id; // apart being a foreign key, this is not a key of A @Column(name= "b_id" , type= "integer" ) private $bId; @OneToOne(targetEntity= "B" ) @JoinColumn(name= "b_id" , referencedColumnName= "id" ) private $b; } $entityManager->find('A', 1); ?>
        Hide
        Benjamin Eberlei added a comment -

        problem is that array_key_exists is 9 times slower than isset, and this method is easily called several thousand times. We need to find another solution

        Show
        Benjamin Eberlei added a comment - problem is that array_key_exists is 9 times slower than isset, and this method is easily called several thousand times. We need to find another solution
        Hide
        Benjamin Eberlei added a comment -

        @roman my proposed patch is this:

        diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php
        index 242d84b..e3cd015 100644
        --- a/lib/Doctrine/ORM/UnitOfWork.php
        +++ b/lib/Doctrine/ORM/UnitOfWork.php
        @@ -1881,7 +1881,7 @@ class UnitOfWork implements PropertyChangedListener
                                 if ($assoc['isOwningSide']) {
                                     $associatedId = array();
                                     foreach ($assoc['targetToSourceKeyColumns'] as $targetColumn => $srcColumn) {
        -                                $joinColumnValue = $data[$srcColumn];
        +                                $joinColumnValue = isset($data[$srcColumn]) ? $data[$srcColumn] : null;
                                         if ($joinColumnValue !== null) {
                                             $associatedId[$targetClass->fieldNames[$targetColumn]] = $joinColumnValue;
                                         }
        

        it sucks this applies only to this use-case however...

        Show
        Benjamin Eberlei added a comment - @roman my proposed patch is this: diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php index 242d84b..e3cd015 100644 --- a/lib/Doctrine/ORM/UnitOfWork.php +++ b/lib/Doctrine/ORM/UnitOfWork.php @@ -1881,7 +1881,7 @@ class UnitOfWork implements PropertyChangedListener if ($assoc['isOwningSide']) { $associatedId = array(); foreach ($assoc['targetToSourceKeyColumns'] as $targetColumn => $srcColumn) { - $joinColumnValue = $data[$srcColumn]; + $joinColumnValue = isset($data[$srcColumn]) ? $data[$srcColumn] : null ; if ($joinColumnValue !== null ) { $associatedId[$targetClass->fieldNames[$targetColumn]] = $joinColumnValue; } it sucks this applies only to this use-case however...
        Hide
        Benjamin Eberlei added a comment -

        Fixed

        Show
        Benjamin Eberlei added a comment - Fixed
        Hide
        arnaud-lb added a comment -

        Thanks!

        Show
        arnaud-lb added a comment - Thanks!

          People

          • Assignee:
            Benjamin Eberlei
            Reporter:
            arnaud-lb
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: