Tuesday, October 22, 2013

Re: problems saving datetime

I realize now that my original issue may not have been so much fetching data from the AppController but more so setting the data to be available in the view. To test I set a misc value  $this->set('variable', 'some data'); in the beforeFilter() of the AppController. $variable is available in every view but the ones associated with the Articles controller (I changed it from 'News' to rule out issues with naming conventions). View, add, edit, etc. it's not available in any view for that controller. The views for that controller act almost as if they are being cached. Changes to the content are immediately visible but altering the CSS takes having to navigate out of the Articles controller and then back in for the changes to take effect whereas the changes are immediate in all other views. No matter what I try I cannot access $variable in any of the article views. Could changing from 'News' to 'Articles' have caused the issue somehow? I'm not quite sure where to start looking for the cause of the problem. 

On Tuesday, October 22, 2013 1:54:34 PM UTC-4, CrotchFrog wrote:
Problem solved, I worked out the issue. I have a few spots in my layout that display dynamic data and I usually use Jquery $('#element').load() to populate those spaces. I threw $this->News->get(); into the AppController beforeFilter to temporarily retrieve the needed data as I worked on styling, etc. When I got around to creating my JS file and removed the code from beforeFilter the problem immediately went away. To confirm I put the code back and the problem returned. Here and there I'll use the AppController to house some shared logic but cannot think of a time I tried to retrieve data through it (excluding this time). Generally I'll use Jquery and/or view blocks to utilize or extend my existing views/controllers. Anyhow, I suppose I never gave it much thought and it's probably a bit unconventional but is fetching data through the AppController really enough to break your app? There are plenty of times I don't understand things as well I think or would like to but I didn't think using the  AppController in such a way was basically throwing a wrench into the works. I feel like I'm still missing something here which is generally the case :)

On Monday, October 21, 2013 1:26:38 PM UTC-4, CrotchFrog wrote:
I ran into an issue trying to save a datetime field and I haven't been able to figure out why it's not saving (not failing).
Database set up is Ubuntu Server 12.04 LTS w/ Percona Server 5.6 -- I'm using Cake 2.4.1

//table set up

DROP TABLE IF EXISTS `news`;
CREATE TABLE `news` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `title` tinytext NOT NULL,
  `article` text NOT NULL,
  `start` datetime NOT NULL,
  `stop` datetime NOT NULL,
  `created` datetime NOT NULL,
  `modified` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=latin1;

Before saving I convert user input into a savable format. I tried the conversion in both controller and model beforeSave. 

// conversion 

$xstart = explode('-', $this->request->data['News']['start']);
$start = strtotime($xstart[0].' '.$xstart[1]);
$date = date('Y-m-d H:i:s', $start);            
$this->request->data['News']['start'] = $date;
            
$xstop = explode('-', $this->request->data['News']['stop']);            
$stop = strtotime($xstop[0].' '.$xstop[1]);
$expire = date('Y-m-d H:i:s', $stop);            
$this->request->data['News']['stop'] = $expire;

// debug request data

array(  	'News' => array(  		'start' => '2013-10-21 13:13:00',  		'stop' => '2013-10-22 21:00:00',  		'title' => 'Test Article Title',  		'article' => 'Test Article Body',  		'user_id' => '5'  	)  )

The request does not fail but it also does not save to DB.

// To test what's happening bypass conversion and throw together quick data array

$this->request->data = array(                      'News' => array(                          'title' => 'this is a title',                          'article' => 'this is an article',                          'start' =>  '2013-10-22 13:00:00',                          //'stop' => '2013-10-22 13:00:00',                      ));

Data saves fine as long as 'stop' key is not in the array. As soon as I include the key, nothing saves (I changed the column name a few times thinking perhaps there was an issue with the name but it didn't help). 

If I change the array to use converted data, ie. 'start' => $date it will not save. Tried casting $date as string, it didn't help. 

So, using either formatted date from converted data will not save. My test array saves as long as I don't include the 'stop' key or try to include converted data. 
I checked all my error logs but there is nothing there. I was sure I'm overlooking something simple but I just don't see it. 

I'm stumped (or perhaps more frustrated at this point).

Any help is appreciated. 




--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.

No comments: