Welcome Guest, Not a member yet? Register   Sign In
Dynamically referencing CI Model Class properties?
#1

[eluser]Myles Wakeham[/eluser]
I'm hoping some kind soul can shed some light on how I can do this... Here's the background:

I am developing an 'email template' system. Basically its like a CI View or Smarty type templating engine, but its for email content. The user can create a template for the body of an email, something like this (this is a 'Forgot password' email example):

"We received your request. Your password is {user->password}"

I need to replace the stuff in the { } tags. Since this will be used for hundreds of different purposes, the models that are referenced will be many and varied. My current application has about 30 models in it, and I'll expose certain properties for this as needed.

I have written some functions to extract all the tags from the template source. That's working fine. I have an array of these tags ready for processing.

The problem is now how I can replace the tags with the current instance of the class/property that the tag is referring to. In my case, I'm using a direct reference to the model/property that is loaded in the controller that calls the class, and I have an object created from that controller and loaded with the data that I want to use for the template. The function gains access to the object created in the controller through:
Code:
$contr = & get_instance();
$the_user = $contr->user;

(this is just one example for a user model)

I have a property in this example class called 'password' that I can generally reference as $the_user->password. Works fine in regular functions. But in my case I want the reference to it to be variable (based on the tags found in the template).

So how do you reference an object (model) property this way?

I've tried the following which don't work:

Code:
$source_data = 'user->password';
echo ${$source_data};

or

Code:
echo & ${$source_data};

but that doesn't work. I've also tried doing this with an eval statement, but I get the exact same problem.

What's the best way to reference a model's object and its properties from a plug-in function?

Myles
#2

[eluser]xwero[/eluser]
A bit in a partying mood here but the first thing i can think of is to create a function for each property with the model_property syntax. These functions call a helper function that loads the model and the method that is needed to get the property.
#3

[eluser]Myles Wakeham[/eluser]
[quote author="xwero" date="1240026757"]A bit in a partying mood here but the first thing i can think of is to create a function for each property with the model_property syntax. These functions call a helper function that loads the model and the method that is needed to get the property.[/quote]

Thanks, but I have about 30 models, each each has 20 or so properties I'd like to expose for this. That's a hell of a lot of functions to write. Is there any other way to directly access the properties of a class from a variable (ie. a pointer or something like that?)

Myles
#4

[eluser]xwero[/eluser]
I didn't believe you were going to do it but that was my first idea. i think you need something like

Code:
function get_property($var)
{
    list($model,$property) = explode('_',$var);
    
    $this->load->model($model,'',true);

    return $this->$model->$property;
}
The var is extracted from the template
#5

[eluser]Myles Wakeham[/eluser]
[quote author="xwero" date="1240030955"]I didn't believe you were going to do it but that was my first idea. i think you need something like

Code:
function get_property($var)
{
    list($model,$property) = explode('_',$var);
    
    $this->load->model($model,'',true);

    return $this->$model->$property;
}
The var is extracted from the template[/quote]

Thanks again for your thoughts on this. If the model has already been loaded in a controller, and I want to have this function resident in a plug-in that is called from that controller, how would I reference the parent instance with this?

Myles
#6

[eluser]xwero[/eluser]
i wouldn't, if the model is already loaded the load->model call will be void.
#7

[eluser]Myles Wakeham[/eluser]
[quote author="xwero" date="1240032143"]i wouldn't, if the model is already loaded the load->model call will be void.[/quote]

That's a good point. I realized that in looking at the CI code for how a library is loaded. OK, I'll give that a try. Again, many thanks for your thoughts.

Myles
#8

[eluser]helmutbjorg[/eluser]
This may help if I am correct in what you are wanting...

Code:
class Object {
    var $password = 'hello';
}

$user = new Object();

$controller = 'user';
$property = 'password';

echo $$controller->$property;

So you need to split your source data

Code:
$source_data = 'user->password';

list($controller, $property) = explode('->', $source_data);

echo $$controller->$property;
#9

[eluser]Myles Wakeham[/eluser]
[quote author="helmutbjorg" date="1240046913"]This may help if I am correct in what you are wanting...

Code:
class Object {
    var $password = 'hello';
}

$user = new Object();

$controller = 'user';
$property = 'password';

echo $$controller->$property;

So you need to split your source data

Code:
$source_data = 'user->password';

list($controller, $property) = explode('->', $source_data);

echo $$controller->$property;
[/quote]

Yes, that is exactly what I was trying to do. I can definitely split the object from the property reference. I just needed to know the syntax in order to get the data back from it.

Now would there be any issues if the object is instantiated in the controller, and I'm using a $contr = & get_instance() to get access to it in my function?

Myles
#10

[eluser]helmutbjorg[/eluser]
I'm guessing you are trying to get an instance of the CodeIgnier super object. I don't see any issues even with the following...

Code:
$source_data = 'form_validation->run';

$CI =& get_instance();
list($library, $method) = explode('->', $source_data);
$CI->load->library($library);
$CI->$library->$method();

Just give it a go. You'll work it out.




Theme © iAndrew 2016 - Forum software by © MyBB