1. Create a model class first. Take the user as an example.Application/classes/model/user. create a user in the PHP path. PHP, and a table corresponds to a model, and the table name must be added "S" after the class name, that is, the table name should be users. In this file, you must inherit the ORM class:
<?phpclass Model_User extends ORM{ ...}?>
Create an ORM instance in the Controller (the access method must be prefixed with "Action _", and the inherited class "controller_admin" is used to facilitate permission Control ):
<? Phpclass controller_admin_user extends controller_admin {public function action_test () {$ user = ORM: Factory ('user ');
// Insert
$ User-> name = 'tina ';
$ User-> age = '22 ';
$ User-> Save ();
// Query records. The result is an object.
$ Result = ORM: Factory ('user')-> where ('id', '=', 1)-> Find ();
// Update. The second parameter is the primary_key of the users table, which is equivalent to ORM: Factory ('user')-> where ('id', '=', 1) -> Find ();
$ User_update = ORM: Factory ('user', 1 );
// Determine whether to load the loaded Method
If ($ user_update-> loaded ()){
$ User_update-> name = 'jack ';
$ User_update-> Save ();
}
// Delete
Orm: Factory ('user', 1)-> Delete () ;}}?>
Basic use of the Kohana framework ORM class