This ORM is build over two basic principles :

No lazy loading : The result objects structure is what you've asked in request with dependecies that you requested. Why ? The lazy loading do requests each time you ask access for first time of a dependency. More the database is complex, less performance you will have. And if you have strong batch to launch : I expect you have a strong server to do that !

What you get is what you have asked : The objects dependencies loaded is not the reflect of absolutes relations. If you've selected in where clause a part of the list of a dependency, only this part is loaded and is accessible.

Here is your first DAO :


<?php

namespace Sebk\ProjectBundle\Dao;

use 
Sebk\SmallOrmBundle\Dao\AbstractDao;

class 
Project extends AbstractDao {

    
/**
     * Build structure of DAO
     * @throws \Sebk\SmallOrmBundle\Dao\DaoException
     */
    
protected function build() {
        
$this->setDbTableName("project");
        
$this->setModelName("Project");
        
$this->addPrimaryKey("id""id");
        
$this->addField("name""name");
        
$this->addField("branch_prefix""branchPrefix");
        
$this->addField("leader_user_id""leaderId");
        
$this->addToOne("leader", array("leaderId" => "id"), "User""SebkSmallUserBundle");
    }

}


You can note that your DAO always extends AbstractDao. The method "build" is required and must set a few parameters :

  • $this->setDbTableName("project");

    This statement define that your dao concern the table "project" in database.

  • $this->setModelName("Project");

    This is the class name of your corresponding model.

  • $this->addPrimaryKey("id", "id");

    Here is your primary key : first parameter for db name and second parameter for the name of correponding property of your model class.

  • $this->addField("branch_prefix", "branchPrefix");

    Here is a simple field : as for the primary key, first parameter for db name and second parameter for the name of correponding property of your model class.

  • $this->addToOne("leader", array("leaderId" => "id"), "User", "SebkSmallUserBundle");

    This statement define a to one relation.

    The first parameter defined the property that will map the sub object.

    The second parameter is an array to define the foreign keys of relation : The key of array is the from field and the second key is the field of foreign model.

    The Third parameter is the name of destination model

    The last (optionnal) parameter is the bundle of destination model. If ommited, the ORM look in the same bundle.

    Yes you can do requests cross bundle and corss databases in SmallOrm !

Good practice is creating requests in your DAO object :


<?php

namespace Sebk\ProjectBundle\Dao;

use 
Sebk\SmallOrmBundle\Dao\AbstractDao;

class 
Project extends AbstractDao {

    
/**
     * Build structure of DAO
     * @throws \Sebk\SmallOrmBundle\Dao\DaoException
     */
    
protected function build() {
        
$this->setDbTableName("project");
        
$this->setModelName("Project");
        
$this->addPrimaryKey("id""id");
        
$this->addField("name""name");
        
$this->addField("branch_prefix""branchPrefix");
        
$this->addField("leader_user_id""leaderId");
        
$this->addToOne("leader", array("leaderId" => "id"), "User""SebkSmallUserBundle");
    }
    
    
/**
     * List my projects
     * @param mixed $userId
     * @return array
     */
    
public function getMyProjects($userId) {
        
$query $this->createQueryBuilder("project")
                ->
innerJoin("project""leader")->endJoin();
        
$query->where()
                ->
firstCondition($query->getFieldForCondition("leaderId""project"), "="":userId")
                ->
endWhere()
                ->
setParameter("userId"$userId);
        
        return 
$this->getResult($query);
    }

}


  • First step is creating the query builer object

    $this->createQueryBuilder("project")

    This statement create a query on this model aliased by "project". Traducted to sql this initiate this query statement :

    select * from project as project

  • Second step is join models that you want to map in your result objects.

    $query->innerJoin("project", "leader")->endJoin();

    First parameter is model who own the relation (see the addToOne statement of DAO) and the second is the relation name.

    At this step, our sql statement will be :

    select * from project as project inner join user as leader on project.leader_user_id = leader.id

  • We can now add or condition

    First create where object : $whereObject = $query->where();

    We can create our first condition :
    $whereObject->firstCondition($query->getFieldForCondition("leaderId", "project"), "=", ":userId")

    $query->getFieldForCondition("leaderId", "project") return the field object "leaderId" from model aliased by "project".

    Our sql staement is now :

    select * from project as project inner join user as leader on project.leader_user_id = leader.id
    where project.leaderId = :userId

    Just set parameter : $query->setParameter("userId", $userId);

  • Let execute query : $this->getResult($query);

    Who get this result :

        [
            {
                "id": 10,
                "name": "my project 1",
                "branchPrefix": "dev1",
                "leaderId": 3,
                "leader": {
                    "id": 3
                    "nickname": "sebk69"
                }
            },
            {
                "id": 15,
                "name": "my project 2",
                "branchPrefix": "dev2",
                "leaderId": 3,
                "leader": {
                    "id": 3
                    "nickname": "sebk69"
                }
            }
        ]
    
    These result objects are models object and you can access properties and subobjects with getters and setters :
        $name0 = $resultProjects[0]->getName();
        $leader0Nickname = $resultProjects[0]->getLeader()->getNickname();