Datasources in CakePHP

Source: Internet
Author: User
Tags php foreach

The data source in CakePHP is actually a good thing. If you have multiple forms of data sources, such as rest, RSS,
JSON, etc. You can write the CakePHP plug-in to read these data sources. This article will write a data source that reads the RSS format
To explain how to use it.

The code can be downloaded at http://www.loadsys.com/files/rss_source.zip,
Place the decompressed code in/APP/models/datasources and set
APP/config/database. php:

VaR $ codeforestnews = array (

'Datasource '=> 'rss ',

'Feedurl' => 'HTTP: // feeds.feedburner.com/codeforest ',

'Encoding' => 'utf-8 ',

'Cachetime' => '+ 1 Day ',

);
Next, create
/APP/models/codeforest_news.php
Class codeforestnews extends appmodel {

VaR $ usetable = false;

VaR $ usedbconfig = 'codeforestnew ';

}
It is illustrated that datasource is used directly as the data source instead of the tables in the database.
Let's look at the control layer:
Class newscontroller extends appcontroller {

VaR $ name = 'News ';

VaR $ uses = array ('codeforestnew ');

Function Index (){

$ This-> paginate = array (

'Limit' => 10,

'Order' => array (

'Codeforestnews. pubdat' => 'desc ',

),

);

$ This-> set ('News', $ this-> paginate ('codeforestnew '));

}

}
It can be seen, or even sorted.
On the page:

<? PHP foreach ($ news as $ newsitem):?>
<? PHP echo $ HTML-> Link ($ newsitem ['codeforestnew'] ['title'], $ newsitem ['codeforestnews'] ['link']);?> <Br/>

<Em> <? PHP echo $ newsitem ['codeforestnew'] ['pubdate'];?> </Em>
<HR>
<? PHP endforeach;?>
<Div class = "Paging">
<? PHP echo $ paginator-> Prev ('<'. _ ('previous ', true), array (), null, array ('class' => 'Disabled');?>

| <? PHP echo $ paginator-> numbers ();?>
<? PHP echo $ paginator-> next (_ ('Next', true ). '>>', array (), null, array ('class' => 'Disabled');?>

</Div>

Demo effect:
Http://www.codeforest.net/demo/cake_data_sources/news/index/page:2

Check the CakePHP manual. You can see that to write datasources, You can implement one of the following methods:
Create ($ model, $ fields = array (), $ values = array ())
Read ($ model, $ querydata = array ())
Update ($ model, $ fields = array (), $ values = array ())
Delete ($ model, $ id = NULL)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.