28 January, 2015

Creating a Blog with PHPixie, Part 1
By: Matthew Jackson

     We assume that you have a limited knowledge of Phpixie, and you can login (ie: users). You can see how to authenticate and have a user here. You can ignore most of the user code if you prefer to have your blogs editable or postable by anyone.

     Creating a blog first means creating the database layer. We will show you a simple, but we feel effective database table based off not just our own knowledge, but what we found available on the web as well.

     `id` int(11) NOT NULL AUTO_INCREMENT,
     `title` varchar(255) NOT NULL,
     `post` longtext NOT NULL,
     `date` date NOT NULL,
     `user_id` int(11) NOT NULL,
     PRIMARY KEY (`id`) )

SQL Table Thoughts:

  • It is important to know that int(11), means that the column is an int, and in your sql visual display you will see 11 digits, but the full int will be stored whether this number is bigger or smaller. varchar(255) means that there are 255 characters! This is unlike int(11) which will be size int no matter what.
  • We use varchar for the title because of efficiency, please see the following article: http://www.pythian.com/blog/text-vs-varchar/. We expect our titles to be varchar, because no one wants long titles, but you can go up to 65535 letters with varchar.
  • We want to have users be able to post blogs, and put their name as the author automatically so we are going to link to their user id. If you assume there might be multiple authors on your blog you will need a one to many relationship, but most blogs are written by one individual, so lets not over-complicate the data structure.


       PHPixie is just simpy a GREAT framework to work with, and I think it becomes very obvious when you see the code.

Blog Modal (representing our Blog table):

namespace App\Model;
class Blog extends \PHPixie\ORM\Model {
    protected $belongs_to =  array(
        'user' => array(

   By naming the table "Blog" it looks for the plural of that model as a table in sql called "blogs". By extending Model we get all the necessary functionality, which means that phpixie will know all about the columns. All we need to do is describe the user relationship. You can specify the table name if you prefer, but for now we have no reason to. "Belongs to" means that there is a user_id key which relates to the user model (a user table) and that the user_id key is equal to the id in the user table. This means simply that a user "owns" this blog.  It is easier to understand when you see the code in action.

//Get all Blogs in order (to display or edit each one):
$blogs = $this->pixie->orm->get('blog')->order_by('date','desc');
//Get a few blogs for a front page feed:
$blogs = $this->pixie->orm->get('blog')
//Save a blog with a user:
$blog = $this->pixie->orm->get('blog');
$blog->title = 'insert title from post data later';
$blog->post = 'Hello World!';
$blog->user_id=$this->pixie->auth->user()->id; //This just writes the value straight to the column.
//OR change the user save to do it as follows:
$blog->add('user',$this->pixie->auth->user()); //This uses the relationship to add and remove, you must save the blog first.


In case you don't know how to get the data, it is pretty simple:

public function action_blog_update(){
     //The next two lines allow this url to be called via ajax, and to return "Error" or "Success" or whatever you want.
     $this->response->body = 'Error';
     $this->execute = false;
     if($this->logged_in('blogger')){ //Checks the user account can make a blog post in roles.
         if($this->request->method == 'POST'){
             $id = $this->request->post('id');
             $title = $this->request->post('title');
             $date = $this->request->post('date');
             ... //the code from above to save these variables
             $blog = this->pixie->orm->get('blog')->where('id',$id)->find();
             $blog->title = $title;


    I didn't just write out all the code or leave it as a download because, I think it is a greate chance for you to take the building blocks and make something awesome. I hope to explain a couple more complicated features to come like comments or tagging.


Tags: PHPixie, PHP, Blog