Home > Backend Development > PHP Tutorial > Basic use of Kohana framework ORM classes, kohana framework orm_PHP tutorial

Basic use of Kohana framework ORM classes, kohana framework orm_PHP tutorial

WBOY
Release: 2016-07-13 10:19:13
Original
1127 people have browsed it

Basic use of Kohana framework ORM classes, kohana framework orm

1. First you need to create a model class, taking user as an example, in application/classes/model/user.php路径下创建user.php,并且一个表对应一个模型,且表名必须在类名后加“S”,即表名应该为users,在这个文件中,需要继承ORM类:

<?php
<span>class</span> Model_User <span>extends</span><span> ORM
{
    </span>...<span>
}
</span>?>
Copy after login

Create an ORM instance in the controller (the access method must be prefixed with "action_", and the inherited class "Controller_Admin" is to facilitate permission control):

<?<span>php

</span><span>class</span> Controller_Admin_User <span>extends</span><span> Controller_Admin
{
    </span><span>public</span> <span>function</span><span> action_test()
    {
        </span><span>$user</span> = ORM::factory('user'<span>);<br />        //insert<br />        $user->name = 'Tina';<br />        $user->age = '22';<br />        $user->save();<br />        //查询记录,得到的结果是一个对象<br />        $result = ORM::factory('user')->where('id','=',1)->find();<br />        //update,其中第二个参数是表users的primary_key,相当于ORM::factory('user')->where('id','=',1)->find();<br />        $user_update = ORM::factory('user',1);<br />        //loaded方法判断是否加载<br />        if($user_update->loaded()){<br />            $user_update->name = 'Jack';<br />            $user_update->save();<br />        }<br />        //delete<br />        ORM::factory('user',1)->delete();
    }
}
</span>?>
Copy after login

What is the ORM framework and its specific usage

ORM - Object/Relation Mapping
For detailed description, please see: baike.baidu.com/view/197951.htm

Roughly speaking, this type of framework is to map class objects and relationships. An intermediate layer is established between the application and the IO of the database. In the program, you only need to directly operate objects (add, delete, modify, and query objects in the database), without having to worry about the columns, relationships, etc. of the tables in the database

For example:
I used to eat alone at home. I had to buy rice and groceries by myself, and then cook them myself. I had to clean up after I was done. I thought it was very troublesome, but I had to do it. I couldn’t help it. It was painful. So single -
This is equivalent to a traditional operating relationship (without using ORM);
Finally one day, I found that it is very convenient to go to a restaurant to eat, and I don’t have to worry about buying food or anything, and I don’t have to worry about eating. After that, you have to clean up a lot of things, order the food, eat, pay and leave - other people will do all the cooking and cooking, and you don't have to worry about how they do it - -
This restaurant is equivalent to an ORM mapping framework, which handles those tedious and boring things for you, and only lets you do the most important part-eating-
And ordering food is equivalent to you being in For ORM mapping configuration, you tell the restaurant what you want to eat, and the restaurant will prepare the side dishes according to your needs and serve them to you when they are ready!

How to configure the php kohana framework after downloading it?

not found MODPATH\\database\\classes\\kohana\\db.php [ 63 ] 58 * @param Please indicate how you call database or ORM in the process. It may be that the method you call is wrong. .

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/877841.htmlTechArticleBasic use of Kohana framework ORM classes, kohana framework orm 1. First you need to create a model class, taking user as an example , create user.php under the application/classes/model/user.php path, and a...
Related labels:
orm
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template