chanmix51 / slapOrm

A simple ORM for Ldap

Home | Edit | New

Documentation

This is the slapOrm documentation :

General presentation:
slapOrm is an abstract layer that turns your LDAP tree into an object oriented collection written in PHP for the symfony framework. It can generate code for your model files and grant you with methods for easy access of your data.

Schema:
A schema is a file where you describe the class structure stored in your LDAP directory. This file has to be config/slaporm/schema.yml in your symfony project.

Employee:
  dn: "ou=employees,dc=my_organization,dc=com"
  objectClass: [inetOrgPerson]
  rdn: cn
  attributes:
    cn:          { type: string, length: 32768, multiple: false, required: true }
    givenName:   { type: string, length: 32768, multiple: false }
    sn:          { type: string, length: 32768, multiple: false }
    mail:        { type: mail, multiple: false }
    o:           { type: string, length: 32768, multiple: false }

Note dn and objectClass attributes are mandatory. dn is the base dn of the class and with objectClass are used to perform searches in your LDAP tree.
rdn maps to an existing attribute.
required flags set to true indicates mandatory inputs in forms (false per default).

With this definition, you can use the symfony command line tool to ask slapOrm to generate the model class files

./symfony slaporm:build-model

This will create a lib/model/slaporm directory containing 2 files :

  • Employee.class.php
  • EmployeeMap.class.php
    and a base subdirectory which contains BaseEmployeeMap.class.php.

Nothing new here if you know already about Propel and / or Doctrine :

  • Employee.class.php represents an object stored in the tree
  • EmployeeMap is a tool class with methods for retreiving or saving Employee objects from / to the LDAP directory. This class extends
  • BaseEmployeeMap which contains basic finders and information present in the schema. Do never edit this file, it is rewritten each time you build the model.

finders
The Map classes all inherit from LdapTransport which contains the basic mechanisms and finders for all the model classes. This means first you need to have an instance of the Map class of the corresponding object. In the actions class, if you want to retreive all the Employee of the Ldap tree :

$this->employees   = SlapOrm::getMapInstanceOf('Employee')->findAll(); 

This will return you a LdapResult object, we will talk about it just after. If you have the RDN of a particular record, you can also retreive it directly :

$this->employee   = SlapOrm::getMapInstanceOf('Employee')->findByRdn('Grégoire hubert');

This wil return you a Employee object. By the way, the second query will not be performed as slapOrm remembers it has already retreived this particular user using the previous findAll finder.

Results
The LdapResult class represents a collection of model objects retreived from the database. You can access it directly like an array :

There are <?php echo count($employees) ?> employees in this result:
<ul>
<?php foreach($employees as $employee): ?>
  <li><?php echo $employee->getCn() ?> has mail : <?php echo $employee->getMail() ?></li>
<?php endforeach ?>
</ul>

Queries
findAll and findByCn are nice but you will probably need something more sophisticated to make complex queries. This is where the EmployeeMap class enters in action: to declare your own finders. Let say you want to retrieve Employee working for Sensio, we are going to create a method that builds the according query :

public function findByCompany($company)
{
  $query = $this->createQuery()
    ->addAndFilter(new LdapCompareQueryOperator('o', $company))
    ;
  return $this->ldap_search($query);
}

Several things happen here:

  • the ability of the EmployeeMap class to create queries designed to retrieve … employees. This returns a LdapQuery instance.
  • the query has filters already set to ObjetClass of the Employee definition
  • we add a filter on this query which is (o=“a company name”)
  • the result is returned by the EmployeeMap’s ldap_search method

You might say it would be more convenient to have a execute() method in the query class like Doctrine, it would save a line of code. Maybe but I like this solution because you will always prefer to put these 2 lines in a method into the model instead of letting it in the actions class.
Only the attributes declared in the schema.yml will be retreived from the Ldap directory, this will save execution time if your database is bigger than what you need.

You can of course create complexe queries:

$query = $this->createQuery()
  ->addAndFilter(new LdapCompareQueryOperator('age', 25, LdapCompareQueryOperator::GREATER_EQUAL))
  ->addOrFilter(new LdapCompareQueryOperator('gender', 'F'))

This query will bring you back all employees who are either older than 24 years or female in every companies. If you want to debug your query you can do:

echo $query;

If you want to retrieve only users having a mail address:

->addAndFilter(new LdapCompareQueryOperator('mail', '*'))

You can of course limit the number of results returned by the query:

$query->limit(5);

This will return you the first 5 results. If you are only interested in grabbing a single result, you can use the following:

$this->fetchOne($query)

This is simplier because it will directly return an instance of Employee.

Last edited by chanmix51, Mon Dec 21 07:04:32 -0800 2009
Home | Edit | New
Versions: