Welcome Guest, Not a member yet? Register   Sign In
Variable models/model names and calling methods
#1

[eluser]JayTee[/eluser]
I'm trying to implement an emailing "system" of sorts - basically, I'm abstracting the email library with my own. Here's what I'm doing:
- I'm have a series of about 50 editable email "templates" stored in the database
- Each template has placeholders that are substituted for values when the email is sent
- I want to be able to use a generic means to say:
Code:
$this->emailModel->trigger('template_name',$somedata);

I don't want to bore you with the details, but the gist of the matter is this: The code for *parsing* the email templates is the same - but the placeholders are different for each email. I want to be able to have common code such that:
Code:
$this->emailModel->trigger($template_name,$data);
will be able to handle all the different templates - with only the $data variable (an array) containing different information that's specific to the email template.

(I hope I've made sense in my description.)

There's more than one way to skin a cat, "they" say, so here was my first shot:
Controller:
Code:
$this->load->model('emailmodel');
$this->emailmodel->trigger('email_template_name',$data);

email_template_name model:
Code:
class email_template_name extends Model {
  function __construct() {
    parent::Model();
  }
  function loadEmailData() {
    //do stuff
  }
}

emailmodel:
Code:
function trigger($template_name,$data) {
  $this->load->model($template_name);
  $this->$template_name->loadEmailData($data);
  $this->sendEmailTemplate($this->$template_name);
}

As you can guess, I get an error when I try to execute the code in the email model. It won't let me call the "loadEmailData" method of the object.

The weird thing is if I put a show_error in the constructor of the email_template_name model, it shows up; so I know the model is actually getting loaded.

So.....

Is there any way I can use a variable as the object? ($this->$objectname->method()). I can get this to work in "straight PHP", but it doesn't seem to work in CI; or maybe I'm missing something?
#2

[eluser]Michael Wales[/eluser]
I had to write something recently to parse out variables in a config file - here's the code for that, hopefully it will help.

Code:
function _parse_email($search = array(), $replace = array(), $subject = '') {
        foreach ($search as $id => $token) {
            $subject = str_replace($token, $replace[$id], $subject);
        }
        return $subject;
    }


Usage:
Code:
$this->_parse_email(array('{username}', '{email}', '{password}'), array($username, $email, $password), $email_text);
#3

[eluser]JayTee[/eluser]
That's pretty similar to how I parse out my email text. The trouble I'm running into is that the "search" array is different for every template. I wanted to abstract that from the controller a bit.
#4

[eluser]xwero[/eluser]
[quote author="walesmd" date="1195679307"]
Code:
function _parse_email($search = array(), $replace = array(), $subject = '') {
        foreach ($search as $id => $token) {
            $subject = str_replace($token, $replace[$id], $subject);
        }
        return $subject;
    }
[/quote]
just wondering if following would make more sense
Code:
function _parse_email($replace = array(), $subject = '') {
        foreach ($replace as $id => $token) {
            $subject = str_replace($id, $token, $subject);
        }
        return $subject;
    }
what would change the usage to
Code:
$this->_parse_email(array('{username}'=>$username, '{email}'=>$email, '{password}'=>$password), $email_text);
#5

[eluser]JayTee[/eluser]
[quote author="xwero" date="1195680461"]
just wondering if following would make more sense
Code:
function _parse_email($replace = array(), $subject = '') {
        foreach ($replace as $id => $token) {
            $subject = str_replace($id, $token, $subject);
        }
        return $subject;
    }
what would change the usage to
Code:
$this->_parse_email(array('{username}'=>$username, '{email}'=>$email, '{password}'=>$password), $email_text);
[/quote]

Actually, here's how I have it set up:
Code:
function _parse_email($template_name,$data) {
  $msg = $this->db->getWhere('templates',array('tname'=>$template_name))->row(0)->body;
  foreach($data as $key=>$value) {
    $msg = str_replace('%'.$key.'%',$value,$msg);
  }
  return $msg
}
Usage:
Code:
$data = array(
  'CONTACT_NAME'=>$var1,
  'CONTACT_EMAIL'=>$var2,
  'FAVORITE_COLOR'=>$var3
  .. etc ..
);
$this->_parse_email('favorite_color',$data);
#6

[eluser]Pygon[/eluser]
I think I would go with:

Code:
function _parse_email( $template,$data=array() ) {
return str_replace(array_keys($data), array_values($data), $template);
}

and called it:

Code:
$data = array('{name}' => 'My Name', '{color}' => 'blue');
$template = 'Hello {name}, your favorite color is {color}!';
$this->_parse_email($template,$data);
#7

[eluser]JayTee[/eluser]
These are good suggestions - thank you everyone for your input!

OTOH, I think my original question has been somewhat derailed...

Ignoring the "email parsing"...

the root of my problem is why CI won't let me use variable names as object references:
This generates an error in CI, but is valid in "straight PHP":
Code:
$var_with_name_of_model = 'model_name';
$this->load->model($var_with_name_of_model);
$this->$var_with_name_of_model->model_method();

The weird part is that the model actually gets loaded - but I can't fire off any methods. Any ideas?
#8

[eluser]Pygon[/eluser]
here is your issue:

Code:
$this->$var_with_name_of_model->model_method();

Try:

Code:
$this->{$var_with_name_of_model}->model_method();

or:
Code:
$caller =& $this->{$var_with_name_of_model};
$caller->model_method();




Theme © iAndrew 2016 - Forum software by © MyBB