Bolt\Legacy\Storage::saveContent PHP Method

saveContent() public method

Save a record.
public saveContent ( Content $content, string $comment = null ) : integer
$content Content
$comment string
return integer
    public function saveContent(Content $content, $comment = null)
    {
        $contenttype = $content->contenttype;
        $fieldvalues = $content->values;
        if (empty($contenttype)) {
            $this->app['logger.system']->error('Contenttype is required for ' . __FUNCTION__, ['event' => 'exception']);
            throw new StorageException('Contenttype is required for ' . __FUNCTION__);
        }
        // Test to see if this is a new record, or an update
        if (empty($fieldvalues['id'])) {
            $create = true;
        } else {
            $create = false;
        }
        // We need to verify if the slug is unique. If not, we update it.
        $getId = $create ? null : $fieldvalues['id'];
        $fieldvalues['slug'] = $this->getUri($fieldvalues['slug'], $getId, $contenttype['slug'], false, false);
        // Update the content object
        $content->setValues($fieldvalues);
        // Dispatch pre-save event
        if (!$this->inDispatcher && $this->app['dispatcher']->hasListeners(StorageEvents::PRE_SAVE)) {
            $event = new StorageEvent($content, ['contenttype' => $contenttype, 'create' => $create]);
            $this->app['dispatcher']->dispatch(StorageEvents::PRE_SAVE, $event);
        }
        // Decide whether to insert a new record, or update an existing one.
        if ($create) {
            $this->insertContent($content, $comment);
        } else {
            $this->updateContent($content, $comment);
        }
        // Update taxonomy and record relationships
        $this->updateTaxonomy($contenttype, $content->values['id'], $content->taxonomy);
        $this->updateRelation($contenttype, $content->values['id'], $content->relation);
        // Dispatch post-save event
        if (!$this->inDispatcher && $this->app['dispatcher']->hasListeners(StorageEvents::POST_SAVE)) {
            // Block loops
            $this->inDispatcher = true;
            $event = new StorageEvent($content, ['contenttype' => $contenttype, 'create' => $create]);
            $this->app['dispatcher']->dispatch(StorageEvents::POST_SAVE, $event);
            // Re-enable the dispatcher
            $this->inDispatcher = false;
        }
        return $content->values['id'];
    }

Usage Example

Esempio n. 1
0
 public function testSaveContent()
 {
     $app = $this->getApp();
     $app['request'] = Request::create('/');
     $storage = new Storage($app);
     // Test missing contenttype handled
     $content = new Content($app);
     $this->setExpectedException('Bolt\\Exception\\StorageException', 'Contenttype is required for saveContent');
     $this->assertFalse($storage->saveContent($content));
     // Test dispatcher is called pre-save and post-save
     $content = $storage->getContent('showcases/1');
     $presave = 0;
     $postsave = 0;
     $listener = function () use(&$presave) {
         $presave++;
     };
     $listener2 = function () use(&$postsave) {
         $postsave++;
     };
     $app['dispatcher']->addListener(StorageEvents::PRE_SAVE, $listener);
     $app['dispatcher']->addListener(StorageEvents::POST_SAVE, $listener2);
     $storage->saveContent($content);
     $this->assertEquals(1, $presave);
     $this->assertEquals(1, $postsave);
 }
All Usage Examples Of Bolt\Legacy\Storage::saveContent