0

I have two entities, Item and SpecialItem

SpecialItem extends Item, where Item is normal shopping item with price property, and the SpecialItem is a shopping item on sale with extra discount property.

I'm using Class Table Inheritance

The Item:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;


/**
 * Item
 *
 * @ORM\Table(name="item")
 * @ORM\Entity
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="item_type", type="string")
 * @ORM\DiscriminatorMap({"item" = "Item", "special" = "SpecialItem"})
 */

class Item
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="title", type="string", length=255, nullable=false)
 */
private $title;

/**
 * @var string
 *
 * @ORM\Column(name="price", type="decimal", precision=10, scale=0, nullable=false)
 */
private $price;

/**
 * @var string
 *
 * @ORM\Column(name="description", type="text", length=65535, nullable=true)
 */
private $description;


}

The Special Item :

<?php


 namespace AppBundle\Entity;

 use Doctrine\ORM\Mapping as ORM;

 /**
 * @ORM\Entity
 */
 class SpecialItem extends Item
 {

 /**
 * @var string
 *
 * @ORM\Column(name="discount", type="decimal", precision=10, scale=0,   nullable=false)
 */
 private $discount;


 }

If I created a SpecialItem and persisted it (now it has one row in table item and one row in table specialitem with discount value), and now I decided to remove the discount so the special item will now be a normal item.

How can I remove the discount related rows only not the whole item and change the DiscriminatorColumn to reflect the parent type Item ?

Omar
  • 1
  • 4
  • Possible duplicate of [Doctrine: Update discriminator for SINGLE\_TABLE Inheritance](http://stackoverflow.com/questions/12610421/doctrine-update-discriminator-for-single-table-inheritance) – d.garanzha May 16 '16 at 15:21

1 Answers1

0

As far as I know you can't change entity type(discriminator) directly.

1) You could write plain SQL query (bad idea but id remain the same):

 DELETE FROM special_item WHERE id = 15;

2) More clean solution: create a new instance of the Item and copy the data from SpecialItem.

class Item
{
     public static function fromSpecialItem(SpecialItem $specialItem)
     {
          $item = new self();
          $item->title = $specialItem->getTitle();

          return $item;
     }
}

$newItem = Item::fromSpecialItem($speciaItem);
$entityManager->remove($speciaItem);
$entityManager->persist($newItem);
$entityManager->flush();
d.garanzha
  • 813
  • 1
  • 9
  • 15