Contents

Kohana module

I have just build my first Kohana module. It is a mess right now, but it is only a try that become to something. It is possible that I must to rewrite it completely.

You can get the code to test it , but it can be changed quickly and it can be not behind compatible.

This module allows to generate dinamically views for our model and controller.

PHP

Steps to use it are next ones:

Make the “modules/defaultsite” folder and download there my module.

Edit the file bootstrap.php, keyword “kohana::modules”, and add the line: ‘defaultsite’ => MODPATH.‘defaultsite’

Now the fun begins: we are going to make our site.

  • Create your database.
  • Create your model. It is just like ever.
  • Create your controller: just the same as ever with Kohana, but inheriting from Controller_Default. Writing the constructor is enough.

That’s all. You can access your site.

Example

BBDD

 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
CREATE  TABLE IF NOT EXISTS 'kohana'.'example1' (
  'id' INT(11) NOT NULL AUTO_INCREMENT,
  'text' VARCHAR(45) NULL DEFAULT NULL ,
  'number' INT(11) NULL DEFAULT NULL ,
  'file' BLOB NULL DEFAULT NULL ,
  'date' DATE NULL DEFAULT NULL ,
  'boolean' TINYINT(1) NULL DEFAULT NULL ,
  'example2_id' INT(11) NULL ,
  PRIMARY KEY ('id') ,
  INDEX 'fk_example1_example21' ('example2_id' ASC) ,
  CONSTRAINT 'fk_example1_example21'
    FOREIGN KEY ('example2_id' )
    REFERENCES 'kohana'.'example2' ('id' )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_swedish_ci;

CREATE  TABLE IF NOT EXISTS 'kohana'.'example2' (
  'id' INT(11) NOT NULL AUTO_INCREMENT,
  'title' VARCHAR(45) NULL DEFAULT NULL ,
  PRIMARY KEY ('id') )
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;

model/example2.php

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<?php defined('SYSPATH') OR die('No Direct Script Access');

class Model_Example2 extends ORM
{
 protected $_table_name = 'example2';

 public function __toString ()
 {
  return $this->title;
 }
}

controller/example2.php

1
2
3
4
5
6
7
8
9
<?php defined('SYSPATH') OR die('No Direct Script Access');

class Controller_Example2 extends Controller_Default
{
 public function __construct(Kohana_Request $request)
 {
  parent::__construct($request, 'example2');
 }
}

Explaining

In this point, you can admin “type 2” elements.

In my host, I have to access /index.php/example2 to admin it. As you can see, pages are very simple, but completely functional.

Index is dinamically generated and it allows you to delete, update or create new items.

You can change columns, but I’m going to skip that because it is only a scaffold.

If you want to see the other table functionality, it is necessary to add some values.

model/example1.php

1
2
3
4
5
6
7
8
9
<?php defined('SYSPATH') OR die('No Direct Script Access');

class Model_Example1 extends ORM
{
 protected $_table_name = 'example1';

 protected $_belongs_to = array ('example1' =>
  array('model'=>'example2', 'foreign_key' => 'example2_id'));
}

controller/example1.php

1
2
3
4
5
6
7
8
9
<?php defined('SYSPATH') OR die('No Direct Script Access');

class Controller_Example1 extends Controller_Default
{
 public function __construct(Kohana_Request $request)
 {
  parent::__construct($request, 'example1');
 }
}

Explaining

If we add some “tipo Example1” items, we will see that the different types of buttons are sensitive to the selected type: texts are textarea, dates use jquery to show a calendar, BLOB show an input file filed (ouch, it is a scaffold yet) and foreign keys are shown like a combobox with all its items.

Final

Nowadays it is only a scaffold, because I have been 2 weeks with Kohana and my PHP knowledge is too poor. Help is needed!!