Exemple #1
0
 /**
  * save
  *
  * @param Data $data
  *
  * @return  mixed|Data
  */
 public function save(Data $data)
 {
     $data->title = trim($data->title);
     $data->alias = $data->alias ?: OutputFilter::stringURLUnicodeSlug($data->title);
     if (!$data->alias) {
         $data->alias = OutputFilter::stringURLSafe((new Date())->toISO8601());
     }
     $mapper = new DataMapper('posts');
     // Increment alias
     $conditions = ['alias' => $data->alias, 'blog' => $data->blog];
     $conditions[] = 'id != ' . ($data->id ?: -1);
     while ($mapper->findOne($conditions)->notNull()) {
         $conditions['alias'] = $data->alias = String::increment($data->alias, String::INCREMENT_STYLE_DASH);
     }
     $data = $mapper->saveOne($data);
     // Save Categories
     $categories = $data->category ?: [];
     $dataSet = [];
     foreach ($categories as $k => $cat) {
         $dataSet[$k]['category'] = $cat;
         $dataSet[$k]['post'] = $data->id;
     }
     (new DataMapper('category_post_maps'))->flush($dataSet, ['post' => $data->id]);
     return $data;
 }
Exemple #2
0
 /**
  * Method to test saveOne().
  *
  * @return void
  *
  * @covers Windwalker\DataMapper\AbstractDataMapper::saveOne
  */
 public function testSaveOne()
 {
     $data = array('title' => 'Sakura', 'catid' => 6);
     $return = $this->instance->saveOne($data, 'id');
     $this->assertEquals('Sakura', $this->db->setQuery('SELECT title FROM ww_flower WHERE catid = 6')->loadResult());
     $this->assertEquals(98, $return->id);
     $data = array('id' => 15, 'title' => 'striped3', 'catid' => 6);
     $return = $this->instance->saveOne($data, 'id');
     $this->assertEquals('striped3', $this->db->setQuery('SELECT title FROM ww_flower WHERE id = 15')->loadResult());
     $this->assertEquals(15, $return->id);
 }
 /**
  * update
  *
  * @param Data $data
  *
  * @return  bool
  *
  * @throws ValidFailException
  */
 protected function saveAuthor($data)
 {
     $authorMapper = new DataMapper('authors');
     if (!$data->name) {
         throw new ValidFailException('Name should not be empty.');
     }
     if (!$data->image) {
         unset($data->image);
     }
     $isNew = !$data->id;
     $author = new Data();
     if ($isNew) {
         $data->uuid = $data->uuid ?: Uuid::v4();
     } else {
         $author = $authorMapper->findOne($data->id);
         $author->bind($data);
     }
     $author->blog = Blog::get()->id;
     $authorMapper->saveOne($author, 'id');
     $this->setRedirect(Router::buildHttp('admin:authors'), 'Save success', 'success');
     return true;
 }
 /**
  * Save only one row.
  *
  * @param mixed $data         The data we want to save.
  * @param array $condFields   The where condition tell us record exists or not, if not set,
  *                            will use primary key instead.
  * @param bool  $updateNulls  Update empty fields or not.
  *
  * @return  mixed|Data Saved data.
  */
 public function saveOne($data, $condFields = null, $updateNulls = false)
 {
     $this->observers->update('onBeforeSaveOne', array(&$data, &$condFields, &$updateNulls));
     $data = parent::saveOne($data, $condFields, $updateNulls);
     $this->observers->update('onAfterSaveOne', array(&$data));
     return $data;
 }