on-delete attribute not working with xml mapping #5924

Open
opened 2026-01-22 15:22:14 +01:00 by admin · 10 comments
Owner

Originally created by @nicolaspetitjean on GitHub (Mar 19, 2018).

Hello there,
Not sure if it is a bug or i 'm doing something wrong, but i have 2 simple entities:
First a person:

namespace App\Entity;

class Person
{
    /**
     * @var integer
     */
    private $id;

    /**
     * @var string
     */
    private $name;

    /**
     * @var Book
     */
    private $book;

    /**
     * @return Book
     */
    public function getBook(): Book
    {
        return $this->book;
    }

    /**
     * @param Book $book
     */
    public function setBook(Book $book): void
    {
        $this->book = $book;
    }

    /**
     * @return int
     */
    public function getId(): int
    {
        return $this->id;
    }

    /**
     * @param int $id
     */
    public function setId(int $id): void
    {
        $this->id = $id;
    }

    /**
     * @return string
     */
    public function getName(): string
    {
        return $this->name;
    }

    /**
     * @param string $name
     */
    public function setName(string $name): void
    {
        $this->name = $name;
    }
}

Then a book

<?php

namespace App\Entity;

class Book
{
    /**
     * @var integer
     */
    private $id;

    /**
     * @var string
     */
    private $title;

    /**
     * @return string
     */
    public function getTitle(): string
    {
        return $this->title;
    }

    /**
     * @param string $title
     */
    public function setTitle(string $title): void
    {
        $this->title = $title;
    }

    /**
     * @return int
     */
    public function getId(): int
    {
        return $this->id;
    }

    /**
     * @param int $id
     */
    public function setId(int $id): void
    {
        $this->id = $id;
    }
}

And then the xml mapping:

<?xml version="1.0" encoding="UTF-8" ?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
        http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

    <entity name="App\Entity\Person" repository-class="App\Repository\PersonRepository">
        <id name="id" type="integer">
            <generator strategy="AUTO"/>
        </id>
        <field name="name" type="string" length="100"/>
        <one-to-one
                field="book"
                target-entity="App\Entity\Book">
            <join-column name="book_id" on-delete="SET NULL"/>
        </one-to-one>
    </entity>
</doctrine-mapping>

Then i update the db schema:

...
ALTER TABLE person ADD CONSTRAINT FK_34DCD17616A2B381 FOREIGN KEY (book_id) REFERENCES book (id);

As you can see, the foreign key is not set properly. Here is what i get in my db:
capture d ecran 2018-03-17 a 10 26 20
Maybe i m doing something wrong ?
Thanks !

Originally created by @nicolaspetitjean on GitHub (Mar 19, 2018). Hello there, Not sure if it is a bug or i 'm doing something wrong, but i have 2 simple entities: First a person: ```php namespace App\Entity; class Person { /** * @var integer */ private $id; /** * @var string */ private $name; /** * @var Book */ private $book; /** * @return Book */ public function getBook(): Book { return $this->book; } /** * @param Book $book */ public function setBook(Book $book): void { $this->book = $book; } /** * @return int */ public function getId(): int { return $this->id; } /** * @param int $id */ public function setId(int $id): void { $this->id = $id; } /** * @return string */ public function getName(): string { return $this->name; } /** * @param string $name */ public function setName(string $name): void { $this->name = $name; } } ``` Then a book ```php <?php namespace App\Entity; class Book { /** * @var integer */ private $id; /** * @var string */ private $title; /** * @return string */ public function getTitle(): string { return $this->title; } /** * @param string $title */ public function setTitle(string $title): void { $this->title = $title; } /** * @return int */ public function getId(): int { return $this->id; } /** * @param int $id */ public function setId(int $id): void { $this->id = $id; } } ``` And then the xml mapping: ```xml <?xml version="1.0" encoding="UTF-8" ?> <doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd"> <entity name="App\Entity\Person" repository-class="App\Repository\PersonRepository"> <id name="id" type="integer"> <generator strategy="AUTO"/> </id> <field name="name" type="string" length="100"/> <one-to-one field="book" target-entity="App\Entity\Book"> <join-column name="book_id" on-delete="SET NULL"/> </one-to-one> </entity> </doctrine-mapping> ``` Then i update the db schema: ```bash ... ALTER TABLE person ADD CONSTRAINT FK_34DCD17616A2B381 FOREIGN KEY (book_id) REFERENCES book (id); ``` As you can see, the foreign key is not set properly. Here is what i get in my db: <img width="372" alt="capture d ecran 2018-03-17 a 10 26 20" src="https://user-images.githubusercontent.com/11570379/37553730-f48c59b2-29cd-11e8-80c7-e459672168bc.png"> Maybe i m doing something wrong ? Thanks !
Author
Owner

@Ocramius commented on GitHub (Mar 19, 2018):

@nicolaspetitjean what version did you try this against?

@Ocramius commented on GitHub (Mar 19, 2018): @nicolaspetitjean what version did you try this against?
Author
Owner

@nicolaspetitjean commented on GitHub (Mar 20, 2018):

Sorry i forgot this
So:
doctrine/orm v2.6.1 Object-Relational-Mapper for PHP
Thanks !

@nicolaspetitjean commented on GitHub (Mar 20, 2018): Sorry i forgot this So: `doctrine/orm v2.6.1 Object-Relational-Mapper for PHP` Thanks !
Author
Owner

@Ocramius commented on GitHub (Mar 20, 2018):

@nicolaspetitjean can you maybe dump the $em->getClassMetadata(\App\Entity\Person::class) output and see if the on-delete is there?

@Ocramius commented on GitHub (Mar 20, 2018): @nicolaspetitjean can you maybe dump the `$em->getClassMetadata(\App\Entity\Person::class)` output and see if the `on-delete` is there?
Author
Owner

@nicolaspetitjean commented on GitHub (Mar 20, 2018):

Sure here the result:

object(Doctrine\ORM\Mapping\ClassMetadata)#356 (40) {
  ["name"]=>
  string(17) "App\Entity\Person"
  ["namespace"]=>
  string(10) "App\Entity"
  ["rootEntityName"]=>
  string(17) "App\Entity\Person"
  ["customGeneratorDefinition"]=>
  NULL
  ["customRepositoryClassName"]=>
  string(31) "App\Repository\PersonRepository"
  ["isMappedSuperclass"]=>
  bool(false)
  ["isEmbeddedClass"]=>
  bool(false)
  ["parentClasses"]=>
  array(0) {
  }
  ["subClasses"]=>
  array(0) {
  }
  ["embeddedClasses"]=>
  array(0) {
  }
  ["namedQueries"]=>
  array(0) {
  }
  ["namedNativeQueries"]=>
  array(0) {
  }
  ["sqlResultSetMappings"]=>
  array(0) {
  }
  ["identifier"]=>
  array(1) {
    [0]=>
    string(2) "id"
  }
  ["inheritanceType"]=>
  int(1)
  ["generatorType"]=>
  int(4)
  ["fieldMappings"]=>
  array(2) {
    ["name"]=>
    array(4) {
      ["fieldName"]=>
      string(4) "name"
      ["type"]=>
      string(6) "string"
      ["length"]=>
      int(100)
      ["columnName"]=>
      string(4) "name"
    }
    ["id"]=>
    array(4) {
      ["id"]=>
      bool(true)
      ["fieldName"]=>
      string(2) "id"
      ["type"]=>
      string(7) "integer"
      ["columnName"]=>
      string(2) "id"
    }
  }
  ["fieldNames"]=>
  array(2) {
    ["name"]=>
    string(4) "name"
    ["id"]=>
    string(2) "id"
  }
  ["columnNames"]=>
  array(2) {
    ["name"]=>
    string(4) "name"
    ["id"]=>
    string(2) "id"
  }
  ["discriminatorValue"]=>
  NULL
  ["discriminatorMap"]=>
  array(0) {
  }
  ["discriminatorColumn"]=>
  NULL
  ["table"]=>
  array(1) {
    ["name"]=>
    string(6) "person"
  }
  ["lifecycleCallbacks"]=>
  array(0) {
  }
  ["entityListeners"]=>
  array(0) {
  }
  ["associationMappings"]=>
  array(1) {
    ["book"]=>
    array(19) {
      ["fieldName"]=>
      string(4) "book"
      ["targetEntity"]=>
      string(15) "App\Entity\Book"
      ["joinColumns"]=>
      array(1) {
        [0]=>
        array(4) {
          ["name"]=>
          string(7) "book_id"
          ["referencedColumnName"]=>
          string(2) "id"
          ["onRemoved"]=>
          string(8) "SET NULL"
          ["unique"]=>
          bool(true)
        }
      }
      ["type"]=>
      int(1)
      ["mappedBy"]=>
      NULL
      ["inversedBy"]=>
      NULL
      ["isOwningSide"]=>
      bool(true)
      ["sourceEntity"]=>
      string(17) "App\Entity\Person"
      ["fetch"]=>
      int(2)
      ["cascade"]=>
      array(0) {
      }
      ["isCascadeRemove"]=>
      bool(false)
      ["isCascadePersist"]=>
      bool(false)
      ["isCascadeRefresh"]=>
      bool(false)
      ["isCascadeMerge"]=>
      bool(false)
      ["isCascadeDetach"]=>
      bool(false)
      ["sourceToTargetKeyColumns"]=>
      array(1) {
        ["book_id"]=>
        string(2) "id"
      }
      ["joinColumnFieldNames"]=>
      array(1) {
        ["book_id"]=>
        string(7) "book_id"
      }
      ["targetToSourceKeyColumns"]=>
      array(1) {
        ["id"]=>
        string(7) "book_id"
      }
      ["orphanRemoval"]=>
      bool(false)
    }
  }
  ["isIdentifierComposite"]=>
  bool(false)
  ["containsForeignIdentifier"]=>
  bool(false)
  ["idGenerator"]=>
  object(Doctrine\ORM\Id\IdentityGenerator)#555 (1) {
    ["sequenceName":"Doctrine\ORM\Id\IdentityGenerator":private]=>
    NULL
  }
  ["sequenceGeneratorDefinition"]=>
  NULL
  ["tableGeneratorDefinition"]=>
  NULL
  ["changeTrackingPolicy"]=>
  int(1)
  ["isVersioned"]=>
  NULL
  ["versionField"]=>
  NULL
  ["cache"]=>
  NULL
  ["reflClass"]=>
  object(ReflectionClass)#318 (1) {
    ["name"]=>
    string(17) "App\Entity\Person"
  }
  ["isReadOnly"]=>
  bool(false)
  ["namingStrategy":protected]=>
  object(Doctrine\ORM\Mapping\UnderscoreNamingStrategy)#295 (1) {
    ["case":"Doctrine\ORM\Mapping\UnderscoreNamingStrategy":private]=>
    int(0)
  }
  ["reflFields"]=>
  array(3) {
    ["name"]=>
    object(ReflectionProperty)#369 (2) {
      ["name"]=>
      string(4) "name"
      ["class"]=>
      string(17) "App\Entity\Person"
    }
    ["id"]=>
    object(ReflectionProperty)#350 (2) {
      ["name"]=>
      string(2) "id"
      ["class"]=>
      string(17) "App\Entity\Person"
    }
    ["book"]=>
    object(ReflectionProperty)#372 (2) {
      ["name"]=>
      string(4) "book"
      ["class"]=>
      string(17) "App\Entity\Person"
    }
  }
  ["instantiator":"Doctrine\ORM\Mapping\ClassMetadataInfo":private]=>
  object(Doctrine\Instantiator\Instantiator)#468 (0) {
  }
}
@nicolaspetitjean commented on GitHub (Mar 20, 2018): Sure here the result: ```php object(Doctrine\ORM\Mapping\ClassMetadata)#356 (40) { ["name"]=> string(17) "App\Entity\Person" ["namespace"]=> string(10) "App\Entity" ["rootEntityName"]=> string(17) "App\Entity\Person" ["customGeneratorDefinition"]=> NULL ["customRepositoryClassName"]=> string(31) "App\Repository\PersonRepository" ["isMappedSuperclass"]=> bool(false) ["isEmbeddedClass"]=> bool(false) ["parentClasses"]=> array(0) { } ["subClasses"]=> array(0) { } ["embeddedClasses"]=> array(0) { } ["namedQueries"]=> array(0) { } ["namedNativeQueries"]=> array(0) { } ["sqlResultSetMappings"]=> array(0) { } ["identifier"]=> array(1) { [0]=> string(2) "id" } ["inheritanceType"]=> int(1) ["generatorType"]=> int(4) ["fieldMappings"]=> array(2) { ["name"]=> array(4) { ["fieldName"]=> string(4) "name" ["type"]=> string(6) "string" ["length"]=> int(100) ["columnName"]=> string(4) "name" } ["id"]=> array(4) { ["id"]=> bool(true) ["fieldName"]=> string(2) "id" ["type"]=> string(7) "integer" ["columnName"]=> string(2) "id" } } ["fieldNames"]=> array(2) { ["name"]=> string(4) "name" ["id"]=> string(2) "id" } ["columnNames"]=> array(2) { ["name"]=> string(4) "name" ["id"]=> string(2) "id" } ["discriminatorValue"]=> NULL ["discriminatorMap"]=> array(0) { } ["discriminatorColumn"]=> NULL ["table"]=> array(1) { ["name"]=> string(6) "person" } ["lifecycleCallbacks"]=> array(0) { } ["entityListeners"]=> array(0) { } ["associationMappings"]=> array(1) { ["book"]=> array(19) { ["fieldName"]=> string(4) "book" ["targetEntity"]=> string(15) "App\Entity\Book" ["joinColumns"]=> array(1) { [0]=> array(4) { ["name"]=> string(7) "book_id" ["referencedColumnName"]=> string(2) "id" ["onRemoved"]=> string(8) "SET NULL" ["unique"]=> bool(true) } } ["type"]=> int(1) ["mappedBy"]=> NULL ["inversedBy"]=> NULL ["isOwningSide"]=> bool(true) ["sourceEntity"]=> string(17) "App\Entity\Person" ["fetch"]=> int(2) ["cascade"]=> array(0) { } ["isCascadeRemove"]=> bool(false) ["isCascadePersist"]=> bool(false) ["isCascadeRefresh"]=> bool(false) ["isCascadeMerge"]=> bool(false) ["isCascadeDetach"]=> bool(false) ["sourceToTargetKeyColumns"]=> array(1) { ["book_id"]=> string(2) "id" } ["joinColumnFieldNames"]=> array(1) { ["book_id"]=> string(7) "book_id" } ["targetToSourceKeyColumns"]=> array(1) { ["id"]=> string(7) "book_id" } ["orphanRemoval"]=> bool(false) } } ["isIdentifierComposite"]=> bool(false) ["containsForeignIdentifier"]=> bool(false) ["idGenerator"]=> object(Doctrine\ORM\Id\IdentityGenerator)#555 (1) { ["sequenceName":"Doctrine\ORM\Id\IdentityGenerator":private]=> NULL } ["sequenceGeneratorDefinition"]=> NULL ["tableGeneratorDefinition"]=> NULL ["changeTrackingPolicy"]=> int(1) ["isVersioned"]=> NULL ["versionField"]=> NULL ["cache"]=> NULL ["reflClass"]=> object(ReflectionClass)#318 (1) { ["name"]=> string(17) "App\Entity\Person" } ["isReadOnly"]=> bool(false) ["namingStrategy":protected]=> object(Doctrine\ORM\Mapping\UnderscoreNamingStrategy)#295 (1) { ["case":"Doctrine\ORM\Mapping\UnderscoreNamingStrategy":private]=> int(0) } ["reflFields"]=> array(3) { ["name"]=> object(ReflectionProperty)#369 (2) { ["name"]=> string(4) "name" ["class"]=> string(17) "App\Entity\Person" } ["id"]=> object(ReflectionProperty)#350 (2) { ["name"]=> string(2) "id" ["class"]=> string(17) "App\Entity\Person" } ["book"]=> object(ReflectionProperty)#372 (2) { ["name"]=> string(4) "book" ["class"]=> string(17) "App\Entity\Person" } } ["instantiator":"Doctrine\ORM\Mapping\ClassMetadataInfo":private]=> object(Doctrine\Instantiator\Instantiator)#468 (0) { } } ```
Author
Owner

@Ocramius commented on GitHub (Mar 21, 2018):

So the metadata was picked up correctly, but then wasn't applied at schema level:

["onRemoved"]=>           string(8) "SET NULL"

Can you dump the orm:schema:create --dump-sql for this?

@Ocramius commented on GitHub (Mar 21, 2018): So the metadata was picked up correctly, but then wasn't applied at schema level: ``` ["onRemoved"]=> string(8) "SET NULL" ``` Can you dump the `orm:schema:create --dump-sql` for this?
Author
Owner

@nicolaspetitjean commented on GitHub (Mar 21, 2018):

Yep, looks like the on-removed has been lost during the process:

CREATE TABLE book (id INT AUTO_INCREMENT NOT NULL, title VARCHAR(100) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB;
CREATE TABLE person (id INT AUTO_INCREMENT NOT NULL, book_id INT DEFAULT NULL, name VARCHAR(100) NOT NULL, UNIQUE INDEX UNIQ_34DCD17616A2B381 (book_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB;
ALTER TABLE person ADD CONSTRAINT FK_34DCD17616A2B381 FOREIGN KEY (book_id) REFERENCES book (id);
@nicolaspetitjean commented on GitHub (Mar 21, 2018): Yep, looks like the on-removed has been lost during the process: ```bash CREATE TABLE book (id INT AUTO_INCREMENT NOT NULL, title VARCHAR(100) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB; CREATE TABLE person (id INT AUTO_INCREMENT NOT NULL, book_id INT DEFAULT NULL, name VARCHAR(100) NOT NULL, UNIQUE INDEX UNIQ_34DCD17616A2B381 (book_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB; ALTER TABLE person ADD CONSTRAINT FK_34DCD17616A2B381 FOREIGN KEY (book_id) REFERENCES book (id); ```
Author
Owner

@Ocramius commented on GitHub (Mar 21, 2018):

@nicolaspetitjean can you try this also on 2.5.x to see if this is an accidental regression?

@Ocramius commented on GitHub (Mar 21, 2018): @nicolaspetitjean can you try this also on `2.5.x` to see if this is an accidental regression?
Author
Owner

@nicolaspetitjean commented on GitHub (Mar 22, 2018):

Oh my bad.
In order to try version 2.5.x, i first created a simple project (only doctrine, no more packages) with only 2 entities, and guess what it works with 2.6.

ALTER TABLE Person ADD CONSTRAINT FK_3370D44016A2B381 FOREIGN KEY (book_id) REFERENCES Book (id) ON DELETE SET NULL;

The project i'm working on is a symfony project, so i think there is a problem with it. But unfortunately, the answer was : 'Doctrine is not part of Symfony, please report this (if not already reported) to the Doctrine project'.
By the way thank you for your help.

@nicolaspetitjean commented on GitHub (Mar 22, 2018): Oh my bad. In order to try version 2.5.x, i first created a simple project (only doctrine, no more packages) with only 2 entities, and guess what it works with 2.6. ```bash ALTER TABLE Person ADD CONSTRAINT FK_3370D44016A2B381 FOREIGN KEY (book_id) REFERENCES Book (id) ON DELETE SET NULL; ``` The project i'm working on is a symfony project, so i think there is a problem with it. But unfortunately, the answer was : 'Doctrine is not part of Symfony, please report this (if not already reported) to the Doctrine project'. By the way thank you for your help.
Author
Owner

@Ocramius commented on GitHub (Mar 22, 2018):

@nicolaspetitjean can you clarify if the issue affects 2.5.x or 2.6.x (or both) then?

@Ocramius commented on GitHub (Mar 22, 2018): @nicolaspetitjean can you clarify if the issue affects 2.5.x or 2.6.x (or both) then?
Author
Owner

@nicolaspetitjean commented on GitHub (Mar 23, 2018):

So to sum it up:

== doctrine 2.6.1 ==
It works

== doctrine 2.5.14 ==
It works

== doctrine 2.6.1 bundled in symfony ==
It does not work

@nicolaspetitjean commented on GitHub (Mar 23, 2018): So to sum it up: == doctrine 2.6.1 == It works == doctrine 2.5.14 == It works == doctrine 2.6.1 bundled in symfony == It does not work
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5924