Bravo3\Orm\Services\EntityManager::delete PHP Method

delete() public method

Any modifications to the entity will be ignored; the persisted state (ID, relationships) of the entity will be deleted. If a new entity is passed to this function, any persisted entity with matching ID & class will be deleted. No error will be raised if a persisted entity is not matched.
public delete ( object $entity )
$entity object
    public function delete($entity)
    {
        $metadata = $this->mapper->getEntityMetadata($entity);
        $reader = new Reader($metadata, $entity);
        // Force entity hydration
        /** @noinspection PhpExpressionResultUnusedInspection */
        isset($entity->_);
        if ($entity instanceof OrmProxyInterface) {
            $local_id = $entity->getOriginalId();
        } else {
            $local_id = $reader->getId();
        }
        $this->validateId($local_id);
        // Delete document
        $this->driver->delete($this->key_scheme->getEntityKey($metadata->getTableName(), $local_id));
        // Delete relationships & indices
        $this->getRelationshipManager()->deleteRelationships($entity, $metadata, $reader, $local_id);
        $this->getIndexManager()->deleteIndices($entity, $metadata, $reader, $local_id);
        $this->getQueryManager()->deleteTableQueries($entity, $metadata, $reader, $local_id);
        return $this->getProxy();
    }

Usage Example

Beispiel #1
0
 /**
  * @dataProvider entityManagerDataProvider
  * @param EntityManager $em
  */
 public function testRefs(EntityManager $em)
 {
     $members = $em->getDriver()->getMultiValueIndex($em->getKeyScheme()->getEntityRefKey('leaf', 'leaf1'));
     $this->assertCount(0, $members);
     $leaf = (new Leaf())->setId('leaf1');
     $owner = (new Owner())->setId('owner1')->setLeaf([$leaf]);
     $em->persist($leaf)->persist($owner)->flush();
     $members = $em->getDriver()->getMultiValueIndex($em->getKeyScheme()->getEntityRefKey('leaf', 'leaf1'));
     $this->assertCount(1, $members);
     $ref = new Ref(Owner::class, 'owner1', 'leaf');
     $this->assertEquals((string) $ref, $members[0]);
     $leaves = $em->sortedQuery(new SortedQuery($owner, 'leaf', 'id'));
     $this->assertCount(1, $leaves);
     $em->refresh($owner);
     $em->refresh($leaf);
     $leaf->setPublished(false);
     $em->persist($leaf)->flush();
     $leaves = $em->sortedQuery(new SortedQuery($owner, 'leaf', 'id'));
     $this->assertCount(0, $leaves);
     $leaf->setPublished(true);
     $em->persist($leaf)->flush();
     $leaves = $em->sortedQuery(new SortedQuery($owner, 'leaf', 'id'));
     $this->assertCount(1, $leaves);
     $em->delete($leaf)->flush();
     $leaves = $em->sortedQuery(new SortedQuery($owner, 'leaf', 'id'));
     $this->assertCount(0, $leaves);
 }
All Usage Examples Of Bravo3\Orm\Services\EntityManager::delete