Order of method execution when deleting records

2011-11-16 Thread RhythmicDevil
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
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php


Re: Order of method execution when deleting records

2011-11-16 Thread RhythmicDevil
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 rhythmicde...@gmail.com 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 

Re: Order of method execution when deleting records

2011-11-16 Thread RhythmicDevil
I am using this doc as a reference: 
http://book.cakephp.org/2.0/en/models/datasources.html

It says that I must do the following which I did so I am confused by
the results.

Methods that must be implemented

describe($model)
listSources()
At least one of:
create($model, $fields = array(), $values = array())
read($model, $queryData = array())
update($model, $fields = array(), $values = array())
delete($model, $id = null)




On Nov 16, 9:29 am, RhythmicDevil rhythmicde...@gmail.com wrote:
 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']);
    

Re: Order of method execution when deleting records

2011-11-16 Thread AD7six


On Wednesday, 16 November 2011 15:24:14 UTC+1, RhythmicDevil 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. 


calculate is called to determine the syntax for a count.
 


 2. Why does the read() method get called before delete() and how do I 
 prevent that?


That's executing the count
 


 3. Is there a document somewhere that lists the methods called in 
 order of execution for CRUD operations?



There's no better source of info than the code itself: 
https://github.com/cakephp/cakephp/blob/master/lib/Cake/Model/Model.php#L2183

the short answer to your question is the model does if (!this-exists()) { 
return false } before performing the delete, exists() simply performs a 
find count and you're focussing on the queries that generates.

If you want to skip that, you could rig exists (in the datasource) to 
always return true - may cause you other problems that way though. Of use 
deleteAll with no cascade and no callbacks.

I guess it would be handy if the model allowed for skipping the exists 
checks - but it doesn't.

AD

-- 
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
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php


Re: Order of method execution when deleting records

2011-11-16 Thread RhythmicDevil
thanks for the tips man, I appreciate it.

I will never have to do cascades or anything like that. I am simply
creating a JSON string to be sent to our API. The API will take care
all the Database stuff. In reality the model and datasource are pass
throughs that do some formatting for the API.



On Nov 16, 9:49 am, AD7six andydawso...@gmail.com wrote:
 On Wednesday, 16 November 2011 15:24:14 UTC+1, RhythmicDevil 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.

 calculate is called to determine the syntax for a count.



  2. Why does the read() method get called before delete() and how do I
  prevent that?

 That's executing the count



  3. Is there a document somewhere that lists the methods called in
  order of execution for CRUD operations?

 There's no better source of info than the code 
 itself:https://github.com/cakephp/cakephp/blob/master/lib/Cake/Model/Model.p...

 the short answer to your question is the model does if (!this-exists()) {
 return false } before performing the delete, exists() simply performs a
 find count and you're focussing on the queries that generates.

 If you want to skip that, you could rig exists (in the datasource) to
 always return true - may cause you other problems that way though. Of use
 deleteAll with no cascade and no callbacks.

 I guess it would be handy if the model allowed for skipping the exists
 checks - but it doesn't.

 AD

-- 
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
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php


Re: Order of method execution when deleting records

2011-11-16 Thread AD7six


On Wednesday, 16 November 2011 15:55:22 UTC+1, RhythmicDevil wrote:

 thanks for the tips man, I appreciate it. 

 I will never have to do cascades or anything like that. I am simply 
 creating a JSON string to be sent to our API.


I know - I mean pass the right arguments so you go directly to 
datasource::delete
https://github.com/cakephp/cakephp/blob/master/lib/Cake/Model/Model.php#L2317

Without passing go and without collection $200 :).

AD

-- 
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
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php