Description
This example shows us how to PHP with SmartClient framework.
SmartClient applications are single page applications most of the time. All the javascript and HTML is loaded or generated dynamically, and most of the time users don't need to leave the initially loaded page.
Prerequisites
- In order to connect the database, You need to download the ReadBean ORM Library. The ReadBean PHP ORM library can be download here.
Description
This example shows us how to PHP with SmartClient framework.
SmartClient applications are single page applications most of the time. All the javascript and HTML is loaded or generated dynamically, and most of the time users don't need to leave the initially loaded page.
Prerequisites
- In order to connect the database, You need to download the RedBean ORM Library. The RedBean PHP ORM library can be download here.
How to use the RedBeanPHP?
To install
After unzipping the RedBeanPHP, you will see just the rb.php.
This file contains everything you need to start RedBeanPHP. Just include it in your PHP script like this:
Code Block language php require 'rb.php';
You are now ready to use RedBeanPHP!
To setup
So, you have decided to start with RedBeanPHP. The first thing you need to get started is setting up the database. Luckily this is really easy.
Code Block language php require('rb.php'); R::setup('mysql:host=localhost; dbname=mydatabase','user','password');
InnoDB only
Info RedBeanPHP only works with the InnoDB driver for MySQL. MyISAM is toolimited.
Queries
Code Block language php R::getAll( 'select * from supplyitem' );
How to build the project with PHP?
This example shows how to use the PHP with SmartClient ListGrid component, by connection ReadBean RedBean ORM library with a ListGrid. The following steps are required:
...
This code will perform the fetching and return the response in JSON format, Please create the fetch.php.
Code Block theme Eclipse language php title fetch.php firstline 1 linenumbers true <?php // importing the ReadBeanRedBean library require 'rb.php'; // Including the DResponseDSResponse source require 'DSResponse.php'; // Connecting to the database R::setup('mysql:host=[HOSTNAME];dbname=[DB_NAME],'USER_NAME','PASSWORD'); // Get the data source list $products = R::getAll('Select * from supplyitem'); // Disconnect to the database R::close(); // SmartClient accept JSON in special format, $response = new DSResponse(); $response->setProducts>setData($products); $response->setStartRow(0); $response->setEndRow(count($products)-1); $response->setTotalRows(count($products)); $response->setStatus(0); // Convert from DResponseDSResponse Object to Array for JSON encoding $result['response'] = (Array)$response; // Create JSON from php data structure and output it. echo json_encode($result); ?>
bean.php
Code Block theme Eclipse language php title DResponse.php firstline 1 linenumbers true <? abstract class Bean { public function __get($propertyName) { $method = 'get' . ucfirst($propertyName); if (!method_exists($this, $method)) { $method = 'is' . ucfirst($propertyName); if(!method_exists($this, $method)) { throw new Exception('Invalid read property ' . $propertyName . ' in ' . get_class($this) . ' class.'); } } return $this->$method; } public function __isset($propertyName) { try { $_value = $this->__get($propertyName); return !empty($_value); } catch (Exception $e) { /* if a property isn't readable it isn't set*/ return false; } } public function __set($propertyName, $value) { $method = 'set' . ucfirst($propertyName); if ('mapper' == $method || !method_exists($this, $method)) { throw new Exception('Invalid write property ' . $propertyName . ' in ' . get_class($this) . ' class.'); } return $this->$method($value); } public function populate(array $map = null) { if(!empty($map)) { foreach($map as $key => $value) { try { $this->__set($key, $value); } catch (Exception $e) { /* evaluated $key isn't a bean writable property. Let's go to next one */ } } } return $this; } }
c. DResponseDSResponse.php
Code Block theme Eclipse language php title DResponse.php firstline 1 linenumbers true <?php require 'bean.php'; class DSResponse extends Bean { // variables /* Read/Write property*/ var $data; /* Read/Write property*/ var $startRow; /* Read/Write property*/ var $endRow; /* Read/Write property*/ var $totalRows; /* Read/Write property*/ var $status; public function setProductssetData($value) { $this->data = empty($value) ? null : (Array)$value; return $this; } public function getProductsgetData( ) { return $this->data; } public function setStartRow($value) { $this->startRow = (int)$value; return $this; } public function getStartRow( ) { return $this->startRow; } public function setEndRow($value) { $this->endRow = (int)$value; return $this; } public function getEndRow( ) { return $this->endRow; } public function setTotalRows($value) { $this->totalRows = (int)$value; return $this; } public function getTotalRows( ) { return $this->totalRows; } public function setStatus($value) { $this->status = (int)$value; return $this; } public function getStatus( ) { return $this->status; } }
...