Showing posts with label kohana 3 database. Show all posts
Showing posts with label kohana 3 database. Show all posts

Kohana 3 :: ORM Simple example


This is a simple example of a single ORM model, that has no relationships, but uses validation on the fields.

SQL schema

CREATE TABLE IF NOT EXISTS `members` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(32) NOT NULL,
  `firstname` varchar(32) NOT NULL,
  `lastname` varchar(32) NOT NULL,
  `email` varchar(127) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;

Model

Kohana 3 :: ORM Relationships


Kohana ORM supports four types of object relationships: belongs_tohas_manyhas_many "through" and has_one. The has_many "through" relationship can be used to function like Active Record's has_many_and_belongs_to relationship type.

belongs_to

belongs_to relation should be used when you have one model that belongs to another. For example, a Child model belongs_to a Parent or a Flag model belongs_to a Country.
This is the base belongs_to relationship:

Kohana 3 :: ORM Basic Usage


Load a new model instance

To create a new Model_User instance you can do two things:
$user = ORM::factory('user');
// or
$user = new Model_User();

Inserting