Jump to content

Can't Save [CakePHP]


theweirdone

Recommended Posts

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.

Link to comment
Share on other sites

       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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.