I have a function that assembles an English sentence about a model object. For example, “Joe created a forum topic 3 days ago”. I would like to use this function/method in views for that model that are imported elsewhere. It works fine in my views as $this->sentence(), but I’m not sure how to reference it when it is imported into another controller. In other words, it works fine in the ContentController, where it is defined, but if I import the model in the SiteController, it can no longer be referenced as $this->sentence.
This is how I’ve done it so far, in the SiteController.php file. It works, but I don’t think it is the proper way.
public function sentence( $content_item ) {
include_once( 'ContentController.php' );
$ctlr = new ContentController('content'); // TBD: Not sure if this is the proper way to do this.
return $ctlr->sentence( $content_item );
}
IMO this method should be put somewhere into the Model domain as it’s coupled to your model objects. So you could again create a base model and extend your models from that:
class BaseRecord extends CActiveRecord {
public function sentence()
{
// your sentence logic here. No need to supply the content_item, as you can use $this instead
}
}