Skip to content

Commit

Permalink
Preliminary end of chapter 4 - tags working
Browse files Browse the repository at this point in the history
  • Loading branch information
eristoddle committed Dec 10, 2013
1 parent 8164c2d commit ecf5081
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 36 deletions.
24 changes: 4 additions & 20 deletions app/controllers/PostsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,8 @@ public function createAction() {

$success = $post->save();

$rawTags = explode(",",$this->request->getPost("tags", array("trim", "lower")));
foreach ($rawTags as $t){
$tag = new Tags();
$tag->tag = $t;
$tag->save();
$postTag = new PostTags();
$postTag->posts_id = $post->id;
$postTag->tags_id = $tag->id;
$postTag->save();
}
$tags = explode(",",$this->request->getPost("tags", array("trim", "lower")));
Posts::addTags($tags,$post->id);

if (!$success) {
foreach ($post->getMessages() as $message) {
Expand Down Expand Up @@ -222,16 +214,8 @@ public function saveAction() {

$success = $post->save();

$rawTags = explode(",",$this->request->getPost("tags", array("trim", "lower")));
foreach ($rawTags as $t){
$tag = new Tags();
$tag->tag = $t;
$tag->save();
$postTag = new PostTags();
$postTag->posts_id = $post->id;
$postTag->tags_id = $tag->id;
$postTag->save();
}
$tags = explode(",",$this->request->getPost("tags", array("trim", "lower")));
Posts::addTags($tags,$post->id);

if (!$success) {

Expand Down
62 changes: 46 additions & 16 deletions app/models/Posts.php
Original file line number Diff line number Diff line change
@@ -1,72 +1,102 @@
<?php


class Posts extends \Phalcon\Mvc\Model
{
class Posts extends \Phalcon\Mvc\Model {

/**
*
* @var integer
*/
public $id;

/**
*
* @var integer
*/
public $users_id;

/**
*
* @var string
*/
public $title;

/**
*
* @var string
*/
public $body;

/**
*
* @var string
*/
public $excerpt;

/**
*
* @var string
*/
public $published;

/**
*
* @var string
*/
public $updated;

/**
*
* @var string
*/
public $pinged;

/**
*
* @var string
*/
public $to_ping;

/**
* Initialize method for model.
*/
public function initialize()
{
$this->hasMany("id", "Comments", "posts_id", NULL);
$this->hasMany("id", "PostTags", "posts_id", NULL);
$this->belongsTo("users_id", "Users", "id", array("foreignKey"=>true));
public function initialize() {
$this->hasMany("id", "Comments", "posts_id", NULL);
$this->hasMany("id", "PostTags", "posts_id", NULL);
$this->belongsTo("users_id", "Users", "id", array("foreignKey" => true));

}

/**
* Adds tags to a post
*/
static function addTags($tags, $post_id) {
foreach ($tags as $t) {
$t = trim($t);
$tag = Tags::findFirst(array("tag = '$t'"));
if (!$tag) {
$tag = new Tags();
$tag->tag = $t;
$tag->save();
}
$postTag = PostTags::findFirst(
array(
"conditions" => "posts_id = ?1 AND tags_id = ?2",
"bind" => array(
1 => $post_id,
2 => $tag->id
)
)
);
if (!$postTag) {
$postTag = new PostTags();
$postTag->posts_id = $post_id;
$postTag->tags_id = $tag->id;
$postTag->save();
}
unset($tag);
unset($postTag);
}
}

}

0 comments on commit ecf5081

Please sign in to comment.