Integrating with PHP & Doctrine

These examples show us how to use Doctrine ORM with the SmartClient framework.

Doctrine 2.2 can be download here. The Getting Started instructions are available here.

Doctrine 2.2 requires a minimum of PHP 5.3.0 or greater. For greatly improved performance it is also recommended that you use APC with PHP.

Handling a fetch request from RestDataSource using Doctrine

This example shows how to use Doctrine with the SmartClient ListGrid component, by connecting Doctrine Entities with a ListGrid. The following steps are required:

1. Create server-side code to fetch data from the database and represent it in the JSON format necessary for SmartClient components:

 1.1 This code will perform the fetching and return the response in JSON format:

// ... some Doctrine bootstrap code here

$entityManager = EntityManager::create($connectionOptions, $config);

// DQL query it looks like SQL
$dql = "SELECT s FROM \Entities\SupplyItem s";

$query = $entityManager->createQuery($dql);

// Fetch reponse as php array
$items = $query->getArrayResult();

// SmartClient accept JSON in special format, see format examples here http://www.smartclient.com/docs/8.2/a/system/reference/SmartClient_Explorer.html#restEditSave
$response = new \stdClass();
$response->response->data = $items;

// Create JSON from PHP data structures and output it
echo json_encode($response);

 1.2 Doctrine fetched Entity sources:

namespace Entities;

/** @Entity @Table(name="supply_item") */
class SupplyItem
{
    /**
     * @Id @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    private $id;

    /** @Column(type="string", length=50) */
    private $itemRef;

    /** @Column(type="text") */
    private $description;

    /** @Column(type="integer") */
    private $requiresJustification;

    /** @Column(type="integer") */
    private $maxQuantity;

    /** @Column(type="string", length=100) */
    private $itemName;

    /** @Column(type="float") */
    private $unitCost;

    // ...setters and getters here...
}

2. Add RestDataSource to the client-side sources with the necessary fields schema:

isc.RestDataSource.create({
    ID:"itemsDS",
    dataFormat : "json",
    fields:[
        {name:"id", title:"id", primaryKey:true, canEdit:false},
        {name:"itemName", title:"Name"},
        {name:"unitCost", title:"Unit Cost"},
        {name:"itemRef", title:"Item Ref"},
        {name:"description", title:"Description"},
        {name:"requiresJustification", title:"Require Justification"},
        {name:"maxQuantity", title:"Max Quantity"}
    ],
    // web path to your doctrine script, sources see above in 1.1
    fetchDataURL: "/smartclient/doctrine/tools/sandbox/index.php"
});

3. Setup the ListGrid component to use with RestDataSource:

isc.ListGrid.create({
    dataSource: itemsDS,
    autoFetchData:true
 })

Now the ListGrid will display the data from the SupplyItem Doctrine Entity.