Switch branches/tags
Nothing to show
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
..
Failed to load latest commit information.
DemoCities.php
DemoCity.php
DemoCountries.php
DemoCountry.php
DemoState.php
DemoStates.php
autoload.php
index.php
readme.md

readme.md

#Demo of nested collections

##Configuration Open index.php in an editor and set the correct database connection details:

LudoDB::setDb("PHPUnit");
LudoDB::setUser("root");
LudoDB::setPassword("administrator");
LudoDB::setHost("127.0.0.1");

##Run in your web browser The demo is best viewed using a browser with a JSON view addon/plugin installed.

URL:

http://<path to your local server>/path/to/ludoDB/examples/cities/index.php

This should give you this countent:

{"success":true, "message":"", "code":200, "response":[
    {
        "id":"1",
        "name":"Norway",
        "states/counties":[
            {
                "id":"2",
                "name":"Hordaland",
                "country":"1",
                "cities":[
                    {
                        "id":"4",
                        "name":"Bergen"
                    }
                ]
            },
            {
                "id":"1",
                "name":"Rogaland",
                "country":"1",
                "cities":[
                    {
                        "id":"3",
                        "name":"Haugesund"
                    },
                    {
                        "id":"2",
                        "name":"Sandnes"
                    },
                    {
                        "id":"1",
                        "name":"Stavanger"
                    }
                ]
            }
        ]
    },
    {
        "id":"2",
        "name":"United States",
        "states/counties":[
            {
                "id":"4",
                "name":"California",
                "country":"2",
                "cities":[
                    {
                        "id":"8",
                        "name":"Los Angeles"
                    },
                    {
                        "id":"9",
                        "name":"San Diego"
                    },
                    {
                        "id":"7",
                        "name":"San Fransisco"
                    }
                ]
            },
            {
                "id":"3",
                "name":"Texas",
                "country":"2",
                "cities":[
                    {
                        "id":"6",
                        "name":"Austin"
                    },
                    {
                        "id":"5",
                        "name":"Houston"
                    }
                ]
            }
        ]
    }
], "log":{
    "time":0.02137303352356,
    "queries":3
}}

##Classes for the demo This demo contains 3 LudoDBModel classes and 3 LudoDBCollection classes.

LudoDBModel's:

  • DemoCountry : Class representing countries
  • DemoState: Class representing states/counties
  • DemoCity: Class representing cities

The model classes are configured with default data which will be inserted into the database when you first open the index.php file in your web browser.

LudoDBCollection's:

  • DemoCountries: Collection of all countries
  • DemoStates: Collection of all states/counties.
  • DemoCitites: Collection of all citiees.

In the DemoCountries collection, countries are merged with states:

protected $config = array(
    "sql" => "select * from demo_country order by name",
    "childKey" => "states/counties",
    "merge" => array(
        array(
            "class" => "DemoStates",
            "fk" => "country",
            "pk" => "id"
        )
    )
);

In DemoStates, states are merged with cities.

protected $config = array(
    "sql" => "select * from demo_state order by name",
    "model" => "DemoState",
    "childKey" => "cities",
    "hideForeignKeys" => true,
    "merge" => array(
        array(
            "class" => "DemoCities",
            "fk" => "state",
            "pk" => "id"
        )
    )
);

In index.php, we're creating a new LudoDBRequest handler and asks it to handle the request "DemoCountries/read":

$handler = new LudoDBRequestHandler();
echo $handler->handle("DemoCountries/read");

When using LudoDBRequest handler, we'll have to implement the LudoDBService interface. It contains three methods:

  • getValidServices: Static method returning array of available services, example: array('read');
  • validateArguments: Method returning true if passed service and constructor arguments are valid.
  • validateServiceData: Method returning true if passed data are valid date for the service.
  • cacheEnabled: Returns true when the Request Handler is allowed to look into LudoDBCache for values. This is very useful if you have collections with lot's of expensive database queries.

This is the DemoCountries class:

class DemoCountries extends LudoDBCollection implements LudoDBService
{
    protected $config = array(
        "sql" => "select * from demo_country order by name",
        "childKey" => "states/counties",
        "merge" => array(
            array(
                "class" => "DemoStates",
                "fk" => "country",
                "pk" => "id"
            )
        )
    );

    public function getValidServices(){
        return array("read");
    }

    public function validateArguments($service, $arguments){
        return count($arguments) === 0;
    }

    public function validateServiceData($service, $data){
        return true;
    }

    public function cacheEnabledFor($service){
        return false;
    }
}

It's also possible to output data without using a LudoDBRequestHandler:

$countries = new DemoCountries();
echo $countries; // trigger the __toString method

or

$countries = new DemoCountries();
var_dump($countries->read()); // PHP Array

Only three database queries are executed on the server to output the tree.