}
We can then configure the datasource in our app/Config/database.php file by adding something
like this:
public $faraway = array(
’datasource’ => ’FarAwaySource’,
’apiKey’ => ’1234abcd’,
);
Then use the database config in our models like this:
class MyModel extends AppModel {
public $useDbConfig = ’faraway’;
}
We can retrieve data from our remote source using the familiar model methods:
// Get all messages from ’Some Person’
$messages = $this->MyModel->find(’all’, array(
’conditions’ => array(’name’ => ’Some Person’),
));
Similarly we can save a new message:
$this->MyModel->save(array(
’name’ => ’Some Person’,
’message’ => ’New Message’,
));
Update the previous message:
$this->MyModel->id = 42;
$this->MyModel->save(array(
’message’ => ’Updated message’,
));
And delete the message:
$this->MyModel->delete(42);
Plugin DataSources
You can also package Datasources into plugins.
Simply place your datasource file into Plugin/[YourPlugin]/Model/Datasource/[YourSource].php
and refer to it using the plugin notation:
public $faraway = array(
’datasource’ => ’MyPlugin.FarAwaySource’,
’apiKey’ => ’abcd1234’,
);