It seems some people have been trying to find out how to do this, so I thought I’d make a very brief guide on how to create a table with Doctrine. I will assume that you’ve already got Doctrine installed with CI. If not, you will need to do this first.

Firstly, let me paste my doctrine_pi.php file for you to see me settings.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php

require_once APPPATH.'/plugins/doctrine/lib/Doctrine.php';
require_once APPPATH.'/config/database.php';
spl_autoload_register(array('Doctrine', 'autoload'));

// loop to allow multiple connections later on
foreach ($db as $connection_name => $db_values) {
  $dsn = $db[$connection_name]['dbdriver'].'://'.$db[$connection_name]['username'].':'
    .$db[$connection_name]['password'].'@'.$db[$connection_name]['hostname'].'/'
    .$db[$connection_name]['database'];

  Doctrine_Manager::connection($dsn,$connection_name);
}

// CI's model class
require_once BASEPATH.'/libraries/Model.php';

// location of app's models
Doctrine::loadModels(APPPATH.'/models');

// optional
// defaults to make table setup a little quicker

// allow use of mutators
Doctrine_Manager::getInstance()->setAttribute(
  Doctrine::ATTR_AUTO_ACCESSOR_OVERRIDE, true);

// default `not null` and unsign ints
Doctrine_Manager::getInstance()->setAttribute(
  Doctrine::ATTR_DEFAULT_COLUMN_OPTIONS, array('notnull' => true, 'unsigned' => true)
);

// auto create `id` columns
Doctrine_Manager::getInstance()->setAttribute(
  Doctrine::ATTR_DEFAULT_IDENTIFIER_OPTIONS, array('name' => 'id', 'type' => 'integer', 'length' => 4)
);

To create a table, simple create a Model that extends Doctrine_Record:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php if(!defined("APPPATH")) { die('No direct script access allowed.'); }

class Test_Table extends Doctrine_Record {
 
  // function MUST be named setTableDefition()
  public function setTableDefinition() {
    // $this->hasColumn(name, type, length, optionals = array());
    $this->hasColumn('name', 'string', 150, array('unique' => true));
    $this->hasColumn('description', 'string', 1000);
    $this->hasColumn('other', 'int', 4);
  }
 
  // same as previous function. name must be setUp()
  public function setUp() {
    // auto generates created_at & updated_at columns
    $this->actAs('Timestampable');
    // NOTE: CI's table prefix is used automatically
    $this->setTableName('Test_Table');
  }
 
}

Lastly you need to create a simple controller.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php

class Test_Controller extends Controller {
 
  public function index() {
    echo <<<HTML1
    <h2>Test Controller</h2>
    <input type="submit" name="create_tables" value="Create"/>
HTML1
;
   
    if($this->input->post('create_tables')) {
      Doctrine::createTablesFromModels();
      echo 'tables created';
    }
  }
 
}

Not the `Doctrine::createTablesFromModels();` part. This will run all models that extends Doctrine_Record for you. Magic code. :) SIMPLE!

Syntax Highlighting Test

26 Aug 2010 In: General, Programming

In an on-going effort to get this working properly, I have come to the conclusion that I’d either have to make my own Syntax Highlighter that was compatible with Habari, or switch back to WordPress. Neither ideas thrilled me greatly, but for the sake of sparing myself more time for the TeapotFramework project, I have grudgingly moved back to WordPress for the time being until I can build my own bespoke Blogging software for Teapot.

This is a quick syntax test

1
2
3
<?php if(!defined("APPPATH") { die("Some long text to pad out horizontal scrolling of the syntax highlighter"); }
  echo 'hello world';
?>

About this blog

This is the blog and homepage of Dale Emasiri - Amateur Web Developer and Co-Founder of Error-418.co.uk and the Teapot Framework for PHP. Current Projects are Teapot Framework and a bulletin board software created with the Teapot Framework.


Sponsors