ED php framework :: Tutorial #9
Data sources & TDataSource class
You use TDataSource to store list of records. It's main usage is when the database "select" operation returns an object of this class and then you bind this object to a data grid. Let's see what else you can do with it. This is the way to manually create and fill it.
    $dataSource = new TDataSource();
    $dataSource->addRecord(Array(
                     "id" => 389,
                     "name" => "apple",
                     "type" => "fruit"
                  ));

    $dataSource->addRecord(Array(
                     "id" => 764,
                     "name" => "rabbit",
                     "type" => "animal"
                  ));

    $dataSource->insert0(Array(
                     "id" => 733,
                     "name" => "sea bass",
                     "type" => "fish"
                   ));


This is the way to sort the internal records. The 2nd parameter can be "string" or "int".
    $dataSource = new TDataSource();
    $dataSource->sortBy("name", "string", SORT_ASCENDING);

    // or omitting the 2nd and 3rd params as those values are the default ones
    $dataSource->sortBy("name");


This is the way to load data from an xml file. This file must be stored under "protected/xml".
snippet/datasource/
books.ds.xml
<DataSource>
 
    <record>
        <key name="id">51</key>
        <key name="title">Pride and Prejudice (Unabridged)</key>
        <key name="author">Jane Austen</key>
        <key name="pages">322</key>
        <key name="description">
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec sed erat non libero dignissim dictum. Phasellus sed est nisi. Maecenas mi dui, elementum vel congue vel, tincidunt eget tellus. Sed lacinia dolor a urna tristique congue. Donec at ante metus. Nam tincidunt, erat ac pellentesque cursus, sapien nibh ornare eros, nec fringilla metus orci quis lectus. Mauris accumsan lobortis orci vel sodales.
        </key>
    </record>
 
    <record>
        <key name="id">234</key>
        <key name="title">World War Z: An Oral History of the Zombie War</key>
        <key name="author">Max Brooks</key>
        <key name="pages">276</key>
        <key name="description">
            Donec pharetra lacus sit amet libero lobortis quis placerat massa fermentum. Vivamus vel massa diam, quis eleifend tellus. Duis sem magna, lobortis a sodales vel, fermentum id felis. Donec porttitor pharetra suscipit.
        </key>
    </record>
 
    <record>
        <key name="id">6453</key>
        <key name="title">The Last Lecture</key>
        <key name="author">Randy Pausch and Jeffrey Zaslow</key>
        <key name="pages">255</key>
        <key name="description">
            Morbi eleifend placerat arcu. Donec scelerisque commodo mi vel sollicitudin. Vivamus mi erat, elementum facilisis commodo ac, hendrerit sed libero. Pellentesque risus metus, pretium sit amet eleifend sed, tincidunt non ligula. Fusce hendrerit mattis lacus. Ut in tellus sapien. Nunc eget dui sollicitudin mi aliquam gravida. In non leo id orci tincidunt blandit eget sit amet sem. Nam malesuada justo in leo tempor ut tincidunt purus dictum. Mauris adipiscing nunc id eros blandit quis sodales nisl ultricies. Pellentesque vel libero nisl, at ullamcorper massa.
        </key>
    </record>
 
    <record>
        <key name="id">12422</key>
        <key name="title">The Bro Code</key>
        <key name="author">Barney Stinson</key>
        <key name="pages">435</key>
        <key name="description">
            Nunc quis magna quis tortor porta dictum non nec ipsum. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Donec pulvinar ante at tortor congue sit amet facilisis magna pretium. Donec pellentesque libero ac est elementum aliquam. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam erat volutpat. Nunc ac massa eu ipsum consectetur imperdiet.
        </key>
    </record>
 
    <record>
        <key name="id">4466123</key>
        <key name="title">Encyclopedia of Immaturity (Klutz)</key>
        <key name="author">Editors of Klutz</key>
        <key name="pages">122</key>
        <key name="description">
            Aenean eleifend nisi id libero molestie vehicula. Curabitur rhoncus malesuada erat at placerat. Phasellus feugiat nisl eu lectus ornare non dapibus purus euismod. Aenean venenatis lacus sit amet massa venenatis vitae malesuada ante ornare.
        </key>
    </record>
 
</<DataSource>
    $dataSource = TDataSource::load("books.ds.xml");


This is the way to process an existing data source. In this scenario we will add a new column, modify an existing column and we'll calculate the sum of all prices.
    $totalAmount = 0;
    for ($i = 0; $i < $dataSource->count(); $i++) {
        $record = $dataSource->getRecord($i);
        // modify column
        $record["price"] = $record["price"] * 1.2;
        // new column
        $record["currTime"] = time();
        $dataSource->setRecord($i, $record);

        $totalAmount += $record["price"];
    }


Once you have an existing data source you can manipulate it. You can keep or remove certain records.
    $dataSource = TDataSource::load("books.ds.xml");
    
    // keep all books that have 100 pages
    $dataSource->keep("pages", 100);

    // keep all books that starts with "The". Here you can use any reg expression 
    // that can be used with the function "preg_match"
    $dataSource->keep("title", "/^The.*/i");

    // remove all books that have 100 pages
    $dataSource->remove("pages", 100);

    // remove all books that starts with "The".
    $dataSource->remove("title", "/^The.*/i");