What is the data pattern in the Segmentfault framework?
In the http://www.infoq.com/cn/news/2012/09/... Mentioned in the.
Reply content:
What is the data pattern in the Segmentfault framework?
In the http://www.infoq.com/cn/news/2012/09/... Mentioned in the.
In the Segmentfault framework, we Data
define a data management object, which is mainly convenient to display in the template. It can represent a single data, or can show multiple data, which is mainly through Iterator
the inheritance interface to implement the iterator function. For example, we show the effect of extracting data from the database to the page display.
We're going to write that in action.
Remove data from model $questions = $this->questionmodel->listquestions ();//put array in iterator $this->questions = new Data_ Question ($questions);//Presentation template $this->view->render (' questions.php ');
We can manipulate the object in the view Data_Question
.
URL ();?> ">
title ();?>
Summary (' ... ');?>
It can be seen that its usage is greatly simplified and intuitive, you do not need to echo
, because we in the Data
base class to help you with the function of the __call
field name as the method name of the direct output of the work, of course you can echo $q->title
, because we also have to deal with the __get
function, You can call the current field directly.
More importantly, with the use of caching technology, we can achieve a very fantastic method, such as the problem author $q->user
, it can directly return to an Data_User
object, then we can use the time to query, what data we want to write what data, such as we output the author of each problem related information
User->url ();?> ">
user->name ();?>.
User->rank ();?>
This is just the most basic usage, and there are many more complex usages that are not introduced, such as the output field can take parameters and so on