Bolt\Legacy\Storage::deleteContent PHP 메소드

deleteContent() 공개 메소드

Delete a record.
public deleteContent ( string $contenttype, integer $id ) : integer
$contenttype string
$id integer
리턴 integer The number of affected rows.
    public function deleteContent($contenttype, $id)
    {
        if (empty($contenttype)) {
            $this->app['logger.system']->error('Contenttype is required for' . __FUNCTION__, ['event' => 'exception']);
            throw new StorageException('Contenttype is required for ' . __FUNCTION__);
        }
        // Make sure $contenttype is a 'slug'
        if (is_array($contenttype)) {
            $contenttype = $contenttype['slug'];
        }
        // Get the old content record
        $tablename = $this->getContenttypeTablename($contenttype);
        $oldContent = $this->findContent($tablename, $id);
        // Dispatch pre-delete event
        if ($this->app['dispatcher']->hasListeners(StorageEvents::PRE_DELETE)) {
            $event = new StorageEvent($oldContent, ['contenttype' => $contenttype]);
            $this->app['dispatcher']->dispatch(StorageEvents::PRE_DELETE, $event);
        }
        $this->logDelete($contenttype, $id, $oldContent);
        $res = $this->app['db']->delete($tablename, ['id' => $id]);
        // Make sure relations and taxonomies are deleted as well.
        if ($res) {
            $this->app['db']->delete($this->getTablename('relations'), ['from_contenttype' => $contenttype, 'from_id' => $id]);
            $this->app['db']->delete($this->getTablename('relations'), ['to_contenttype' => $contenttype, 'to_id' => $id]);
            $this->app['db']->delete($this->getTablename('taxonomy'), ['contenttype' => $contenttype, 'content_id' => $id]);
        }
        // Dispatch post-delete event
        if ($this->app['dispatcher']->hasListeners(StorageEvents::POST_DELETE)) {
            $event = new StorageEvent($oldContent, ['contenttype' => $contenttype]);
            $this->app['dispatcher']->dispatch(StorageEvents::POST_DELETE, $event);
        }
        return $res;
    }

Usage Example

예제 #1
0
 public function testDeleteContent()
 {
     $app = $this->getApp();
     $app['request'] = Request::create('/');
     $storage = new Storage($app);
     // Test delete fails on missing params
     $this->setExpectedException('Bolt\\Exception\\StorageException', 'Contenttype is required for deleteContent');
     $this->assertFalse($storage->deleteContent('', 999));
     $content = $storage->getContent('showcases/1');
     // Test the delete events are triggered
     $delete = 0;
     $listener = function () use(&$delete) {
         $delete++;
     };
     $app['dispatcher']->addListener(StorageEvents::PRE_DELETE, $listener);
     $app['dispatcher']->addListener(StorageEvents::POST_DELETE, $listener);
     $storage->deleteContent(['slug' => 'showcases'], 1);
     $this->assertFalse($storage->getContent('showcases/1'));
     $this->assertEquals(2, $delete);
 }