Saturday, December 4, 2010

Re: Reading External Data and Saving it to database

you probably meant

$this->set('newsItems', $newsItems);

^^

On 4 Dez., 16:13, Ryan Schmidt <google-2...@ryandesign.com> wrote:
> Let's keep this discussion going where it started: on the Google Group, not in private email.
>
> On Dec 4, 2010, at 04:08, NOOOOB wrote:
>
>
>
>
>
>
>
>
>
> > <?php
>
> >    class NewsController extends AppController {
> >       var $name = 'News';
> >       var $uses = array('GoogleNews');
> > function index() {
>
> > $rss =  $this->set('news', $this->GoogleNews->find('all'));
> > foreach ($rss as $rssItem) {
> > $data = array(
> >   'News' => $rss['GoogleNews']
> >   );
> > }
> > }
> > }
> > ?>
>
> > This is my controler, My model looks like this:
>
> > <?php
>
> > class GoogleNews extends AppModel {
> > var $name ='Feed';
> > var $useTable = false;
> > var $useDbConfig = 'MyDealZ';
> > }
> > ?>
>
> > And when i load index.ctp it views data, but I have this error
>
> > nvalid argument supplied for foreach() [APP\controllers
> > \news_controller.php, line 10]
>
> Looks like the first problem is this line:
>
> $rss =  $this->set('news', $this->GoogleNews->find('all'));
>
> You're setting the $rss variable to the return value of the $this->set method. Checking the documentation for Controller::set...
>
> http://book.cakephp.org/view/427/set
>
> ...nowhere does this suggest that $this->set provides a return value (and looking at the source code in cake/libs/controller/controller.php confirms it does not return anything). So it's no surprise your foreach loop complains when trying to iterate over an empty variable.
>
> It seems like you should instead write:
>
> // Get the data from Google News
> $newsItems = $this->GoogleNews->find('all');
>
> // Put the news items into the view
> $this->set('news', $newsItems);
>
> // Do other things with the news items
> foreach ($newsItems as $newsItem) {
>         ...
>
> }
>
> I renamed $rss to $newsItems since $rss isn't the raw RSS XML data (which is what I thought you were talking about initially) but simply an array of values, much as you would get from any other data source. If I were retrieving news items from a database, I wouldn't call the variable $db either.

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
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?hl=en

No comments: