Monday, February 28, 2011

Re: Can't Save to DB

On Feb 28, 2011, at 14:23, theweirdone wrote:

> I've changed my code, here's the new controller:
>
> 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
> if($this->Episode->saveAll($episodes['Episode'])){
> $this->Session->setFlash(__($count.' episodes were saved.', true));
> }
> }
>
> I've also updated the scraper to return the array such as in the manual:
>
> Array
> (
> [Episode] => 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
> )
> )
>
> So now the saveAll() function should be working I think. Yet it still doesn't save anything to the database.
> Should I put in a for loop and save each episode individually using the save() function? Is there any reason that would be more likely to work?

Well.... saveAll() is described as being for saving one main record and multiple related records. Here, you're trying to save multiple unrelated records. I don't know if saveAll() is meant to work in that case, so maybe a for loop is the way for now after all. I hate recommending that, because I know a single multi-record SQL INSERT statement is more efficient than multiple single-record INSERTs, but I don't know how to do it in CakePHP. Then again, your previous code checked the validity of each save and counted up the number of successful saves, implying you expected some of them not to be successful; a single multi-record INSERT would either completely succeed or completely fail, so that may not be what you want anyway.

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

No comments: