Creating a blog app in platform PT pt 1 ;)
Now I am going to show you how to create a simple blog app in platform PT.
You should have a plain installation of platform PT as described here. To obtain platform PT, grab the latest version from the SVN repository: http://tools.assembla.com/svn/platformpt/
I deleted the default app. All apps are located inside
./build
I removed the directory
./build/default
And created a new one
./build/blog
And I created the first file
./build/blog/blog.ctrl.php
<?php
class BlogController extends PAppController
{
// I made it a habit to place this into the default app
const REQUIRED_PLATFORM_VERSION = '0.2b10';
public function __construct()
{
// and compare it here
if (version_compare(PLATFORM_VERSION, self::REQUIRED_PLATFORM_VERSION, '<')) {
throw new PException('Require platform version >= '.self::REQUIRED_PLATFORM_VERSION);
}
parent::__construct();
}
// mandatory
public function index()
{
}
// mandatory
public function output()
{
echo 'Hello, world.';
}
}
?>
As this class is going to become our default controller, the method output() is mandatory. For any other application, only the index() method is mandatory.
The output() method is called at the very end of runtime.
Next file needed is the application information file.
./build/blog/build.xml
<?xml version="1.0" encoding="utf-8"?>
<build>
<app name="Blog"></app>
<files>
<file class="BlogController">blog.ctrl.php</file>
</files>
</build>
Now we will have to tell platform PT which application is called by default.
./inc/defaults.inc.php
function PPckup()
{
return 'BlogController';
}
That’s it. To your surprise you will magically discover the string “Hello, world.”, when you request the page.
Now to the model. First of all we should create a (MySQL) table. Here is the structure I propose:
Structure for table `blogs` +-------+------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------------------+------+-----+---------+-------+ | id | int(10) unsigned | NO | PRI | | | | title | varchar(75) | NO | UNI | | | | flags | int(10) unsigned | NO | MUL | 0 | | +-------+------------------+------+-----+---------+-------+
Note that I am not using the auto_increment feature. It is only supported by MySQL afaik. Our database abstraction layer uses “sequences” instead to define the handling for different backends. Our MySQL abstraction will eventually create a table “blogs_seq” with an auto_increment column to generate the ids.
The flags field is also a habit of mine. It doesn’t hurt, and I can add new flags in my PHP code easily and in a flexible way.
The PHP file looks like this:
./build/blog/blog.model.php
<?php
class Blog extends PAppModel
{
// Also a habit, constants are often underrated
const TABLE_NAME = 'blogs';
// any protected properties represent a column with the same name
protected $id;
protected $title;
protected $flags;
public function __construct($data = false)
{
parent::__construct($data);
$this->setTableName(self::TABLE_NAME);
$this->setPrimaryKey('id');
}
}
?>
The constructor should take the $data argument. We will come back to that later. After you added the file to the build.xml file
...
<file class="Blog">blog.model.php</file>
...
you should have a quite powerful object to work with.
// what you can do:
$Blog = new Blog();
// get/set properties
$Blog->setTitle('hello world blog');
if ($Blog->hasTitle()) {
echo $Blog->getTitle();
}
// the method names set/get/hasTitle() is taken from the propery name
// protected Blog::$sample_field would map to Blog::hasSampleField()
// protected Blog::$sample_field_plus to Blog::hasSampleFieldPlus() etc.
// save the entry to the database
if ($Blog->save()) {
$blogId = $Blog->getId();
echo ";Blog has id: $blogId ";
$Blog = new Blog();
if ($Blog->loadByPrimaryKey($blogId)) {
echo " and title: ".$Blog->getTitle();
}
// since `title` is unique, we can do
if ($Blog->loadByTitle('hello world blog')) {
echo " and loading by that title is cool.";
// make this demo repeatable
$Blog->setTitle($Blog->getTitle().$Blog->getId());
$Blog->save();
}
}
// now find all blogs
$Blog = new Blog();
if ($blogs = $Blog->getAll()) {
echo " ids used: ";
foreach ($blogs as $Blog) {
echo $Blog->getId().' ';
}
}
Now there should be an entry in the table blogs and a new table blogs_seq should have been created.
I included my build directory for download here.
Kommentare
Schreibe einen Kommentar