For reference here is my Datasource file.
<?php
App::uses('HttpSocket', 'Network/Http');
class GtiSource extends DataSource
{
/**
* The description of this data source
*
* @var string
*/
public $description = 'Datasource to interact with the GTI API';
/**
* Instance of CakePHP core HttpSocket class
*
* @var HttpSocket
*/
public $Http = null;
/**
* A URL encoded JSON string
* @var String
*/
public $queryString;
public $queryData;
/**
* Loads HttpSocket class
*
* @param array $config
* @param HttpSocket $Http
*/
public function __construct($config, $Http = null)
{
parent::__construct($config);
if (!$Http)
{
$Http = new HttpSocket();
}
$this->Http = $Http;
}
public function describe($model)
{
return $this->description;
}
public function listSources()
{
}
/**
*
* @param AppModel $model
* @param array $queryData
*/
public function create(&$model, $fields = array(), $values =
array())
{
// Set the mode @TODO is this the right mode?
$queryData['fields']['mode'] = 'insert';
exit('DONT KNOW HOW TO CREATE A RECORD');
return $this->request();
}
/**
*
* @param AppModel $model
* @param array $queryData
*/
public function read(&$model, $queryData = array())
{
// Set the mode
$queryData['fields']['mode'] = 'read';
$this->queryData = $queryData;
$this->model = $model;
return $this->request();
}
/**
*
* @param AppModel $model
* @param array $queryData
*/
public function update(&$model, $fields = array(), $values =
array())
{
// Set the mode @TODO is this the right mode?
$queryData['fields']['mode'] = 'update';
exit('DONT KNOW HOW TO UPDATE A RECORD');
return $this->request();
}
/**
*
* @param AppModel $model
* @param array $queryData
*/
public function delete(&$model, $id = null)
{
var_dump(__METHOD__);
// Deleting not allowed in the GTI API
$data = array();
// Decode the data and cast the beeatch to an Array
$data[$this->model->name] = array('Deleting is not allowed
through the GTI API');
return $data;
}
/**
* Does the actual request. Creates the URI from the DB config
vars and the
* user paramters if any.
*
* @param CakeModel $model
* @return boolean
*/
public function request()
{
/*
* The GTI API requires a key. This is set in the dbConfig
*/
if (empty($this->config['key']))
{
return false; // @TODO throw exception instead.
}
$this->queryData['fields']['key'] = $this->config['key'];
/*
* If the request has not specified a mode then the default is
"read"
*/
if (empty($this->queryData['fields']['mode']))
{
$this->queryData['fields']['mode'] = 'read';
}
/*
* If the request has not specified a cache setting then use
the default
* from dbConfig
*/
if (empty($this->queryData['fields']['cache']))
{
$this->queryData['fields']['cache'] = $this-
>config['cache'];
}
/**
* Create the full URI include the query string because I dont
know how
* to get Cake's HttpSocket to accept it any other way. This
works for now
* but should be reviewed at some point.
*/
$uri = $this->config['url'] . ':' . $this->config['port'] .
'/' . urlencode(json_encode($this->queryData['fields']));
/**
* New Socket
*/
$HttpSocket = new HttpSocket();
/*
* Get the results
*/
$results = $HttpSocket->get($uri);
/**
* If the returned HTTP status code is anything other than 200
then
* something went wrong.
*/
if ($results->code == '200')
{
$data = array();
// Decode the data and cast the beeatch to an Array
$data[$this->model->name] = (Array)
json_decode($results['body']);
return $data;
}
else
{
// @TODO Where is error data supposed to go in the Model?
$model->data = json_decode($results);
return false;
}
}
public function calculate(&$model, $func, $params = array())
{
//var_dump(__METHOD__);
//var_dump($model, $func, $params);
return 1;
}
}
?>
On Nov 16, 9:24 am, RhythmicDevil <[email protected]> wrote:
> I am writing a custom datasource. I was implementing the delete action
> in the datasource and ran into a problem. I expected that when I call
> Model::delete($id, false); it would execute the Datasource::delete()
> method. But that does not happen. First a method called calculate() is
> executed and then Datasource::read() is executed. I dont know why.
>
> 1. So why does the calculate() method get called and how do I prevent
> that as it makes no sense my implementation.
>
> 2. Why does the read() method get called before delete() and how do I
> prevent that?
>
> 3. Is there a document somewhere that lists the methods called in
> order of execution for CRUD operations?
--
Our newest site for the community: CakePHP Video Tutorials
http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others
with their CakePHP related questions.
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php