public function fetchByPostId($postId) { $query = $this->_localConfig->database()->select()->from($this->_getTable())->joinLeft('post_tag', "post_tag.post_id = {$postId} AND post_tag.tag_id = tags.tag_id", array())->where('post_tag.post_tag_id IS NOT NULL')->order('tags.tag_text ASC'); $rows = $this->_localConfig->database()->fetchAll($query); $models = array(); foreach ($rows as $row) { $model = $this->_getContainer()->Tag()->setData($row); $models[] = $model; } return $models; }
public function create() { foreach ($this->_getColumns() as $column) { $data[$column] = $this->get($column); } $data['created_at'] = \Carbon\Carbon::now()->toDateTimeString(); $data['updated_at'] = \Carbon\Carbon::now()->toDateTimeString(); $this->_localConfig->database()->insert($this->_getTable(), $data); // This is probably going to cause horrible bugs. #rollingyourownormproblems $recordId = $this->_localConfig->database()->lastInsertId(); $this->set($this->_getTableIdFieldname(), $recordId); return $this; }
protected function _afterSave() { // Update the updated_at timestamp $this->getUser()->save(); // Add tags if ($this->get('tag_ids') && is_array($this->get('tag_ids'))) { $this->_localConfig->database()->delete("post_tag", "post_id = " . $this->getId()); foreach ($this->get('tag_ids') as $tagId) { $tagPostRelationship = $this->_getContainer()->PostTag()->setData(array('post_id' => $this->getId(), 'user_id' => $this->getUserId(), 'tag_id' => $tagId)); $tagPostRelationship->save(); } } }
/** * Search users by name and username. * * @param $term * * @return array */ public function search($term) { $terms = explode(" ", $term); $searchQuery = array(); foreach ($terms as $term) { $term = $this->_localConfig->database()->quote("[[:<:]]" . $term . "[[:>:]]"); $searchQuery[] = "(users.name regexp {$term}) * 5 + (users.username regexp {$term})"; } $searchQuery = implode(" + ", $searchQuery); $query = $this->selectAll(); $query->columns(new Zend_Db_Expr("({$searchQuery}) as hits")); $query->having('hits > 0'); // We need to reset the ordering that was put on in selectAll() $query->reset(Zend_Db_Select::ORDER); $query->order("hits DESC"); $rows = $this->_localConfig->database()->fetchAll($query); $models = array(); foreach ($rows as $row) { $model = $this->_getContainer()->User()->setData($row); $models[] = $model; } return $models; }