theweirdone Posted February 26, 2011 Share Posted February 26, 2011 Hi, I'm pretty new to CakePHP, and have been managing so far. I've created a scraper that scrapes TV.com for episode information (episode number, airdate, title, description). The scraper works fine, and returns an array such as: Array ( [0] => Array ( [name] => Stowaway [number] => 17 [description] => No synopsis available. Write a synopsis. [date] => 3/18/2011 [show_id] => 11 ) [1] => Array ( [name] => Os [number] => 16 [description] => While Walter attempts to figure out a way to stop the spread of vortexes on This Side, the team investigate a series of thefts committed by criminals who can control gravity. [date] => 3/11/2011 [show_id] => 11 ) ) The array is definately there, because I can print_r it. On to the controller. The controller is called episodes_controller, and has the usual CRUB actions, plus the scraper. I've added an action called addAll which calls the scraper and then is meant to save the array into the database using a foreach loop. Here's the code: function addAll($id = null) { if (!$id) { $this->Session->setFlash(__('Invalid show id entered', true)); //$this->redirect('/episodes'); } $show = $this->requestAction('/shows/getTvById/'.$id); //gets the tv.com url $episodes = $this->scrape($show, $id); //scrapes tv.com for the show $this->set('episodes', $episodes); //sets $TVshowInfo for the view $count = 0; foreach ($episodes as $episode) { $this->Episode->set($episode); if($this->Episode->save()){ $count++; } } $this->Session->setFlash(__($count.' episodes were saved.', true)); //$this->redirect('/episodes'); } Every time I run it, the Flash message say's 0 episodes saved. I've tried variations such as if ($this->Episode->save($episode)){ with the same result. Any suggestions would be welcome. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/228918-cant-save-cakephp/ Share on other sites More sharing options...
mhodge_txs Posted March 3, 2011 Share Posted March 3, 2011 foreach ($episodes as $episode) { $this->Episode->create(); $this->Episode->set($episode); if($this->Episode->save()){ $count++; } } That should do the trick, the set would work by itself is the ID in the array was present, but because you have no ID present u must use the create method before issuing the save Quote Link to comment https://forums.phpfreaks.com/topic/228918-cant-save-cakephp/#findComment-1182280 Share on other sites More sharing options...
theweirdone Posted March 9, 2011 Author Share Posted March 9, 2011 Hi, so it turns out it was a problem with the validation. Because the date wasn't in the 'Y-m-d' format, it wouldn't pass validation, and therefore wouldn't save. Thanks for the help though. Quote Link to comment https://forums.phpfreaks.com/topic/228918-cant-save-cakephp/#findComment-1184943 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.