17488 Posts in 4473 Topics by 1978 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1576 Views |
-
How to reference Model methods from Controler?

29 February 2008 at 6:27am
I have found myself duplicating methods in a Model and its related Controler.
E.g.Blog extends Page() {
function Posts($numPerPage) {
return DataObject::get("Post","BlogID=$this->ID","","",(int)$_GET['start'] . ", $numPerPage"");
}
}Blog_Controller extends Page_Controller() {
function Posts() {
return DataObject::get("Post","BlogID=$this->ID","","",(int)$_GET['start'] . ", 8");
}
}I was looking for a way to just use the function in the Model class and noticed that model class functions are inherited automatically,
E.g. just remove the Post function from the controler and the $this->Post() will call the function in the Model. However, parent does not seem to work like it does with other functions such as init().
E.g. the below call to parent::Posts() does not work, but parent::init() does work.
Blog_Controller extends Page_Controller() {
function init() {
parent::init();
}
function Posts($id) {
parent::Posts(8);
}
}Any ideas on how to refernce the model function when you have a function with the same name in the controller?
-
Re: How to reference Model methods from Controler?

1 March 2008 at 3:26pm
Good question, I do know that if you have a method on the controller, and if you reference a DB field, or relation by calling $this->Field() (replace Field with your DB field name) then it works. However, I'm not sure if you can call a method on the model the same way (not a DB field).
Does this example work, perhaps?
class Page extends SiteTree {
function Posts() {
return 'Lots of posts!';
}}
class Page_Controller extends ContentController {
function Posts() {
return $this->Posts();
}}
Cheers,
Sean
| 1576 Views | ||
|
Page:
1
|
Go to Top |



