|
Project Information
|
An Hibernate like ORM for Web2py built above the DAL.
Example:In the model:db.define_table('person',
Field('surname', type='string', length=50, required=True, label=T('Surname')),
Field('name', type='string', length=50, required=True, label=T('Name')),
Field('salary', type='integer', label=T('Salary')))In the dao.py module (modules folder)from orm import DAO
class PersonDao(DAO):
def __init__(self, db):
DAO.__init__(self, db, 'person')In the controller:## import the modules from applications.your_app.modules.orm import * from applications.your_app.modules.dao import * person_dao = PersonDao(db) ## Find the person with id = 1 person = person_dao.findById(1) ## Modify the name person['name'] = 'A changed name' ## Update the person into the database person_dao.update(person) ## Create new person ## First, generate an empty person new_person = PersonDao(db).dto ## Second, initialize the attributes new_person['name'] = 'Mnemonic' new_person['surname'] = 'Johnny' new_person['salary'] = 2000 ## Alternative method of initialization ## new_person.name.value = 'Mnemonic' ## new_person.surname.value = 'Johnny' ## new_person.salary.value = 2000 ## Third, save the person into the database id = person_dao.persist(new_person) ## To remove the new person from database person_dao.removeById(id) ## Other find methods persons = person_dao.findAll() persons = person_dao.findByExample(person) persons = person_dao.findLike(person) ## To compare all persons to an example person ## First, create an empty person example_person = PersonDao(db).dto ## Second, initialize only the attributes to compare example_person['salary'] = 2000 persons_with_salary_greater_than_2000 = person_dao.findByComparison(Operator.GREATER_THAN, example_person) return dict(person = person, persons = persons) In the view<p>{{=person.id.label}}: {{=person.id.value}}</p>
<p>{{=person.surname.label}}: {{=person.surname.value}}</p>
<p>{{=person.name.label}}: {{=person.name.value}}</p>
<hr />
{{for person in persons:}}
<div>id: {{=person.id.value}}, surname: {{=person.surname.value}}, name: {{=person.name.value}}</div>
{{pass}}
<hr />
|