Welcome Guest, Not a member yet? Register   Sign In
How to Call private method from view?
#1

[eluser]saidbakr[/eluser]
Hello,

I may think that calling controllers functions - methods - from view is not recommended behavior, Really I don't know why! However, I need it as a matter of application performance in restricted or limited situation.

My story is saving some users privileges in the database as letters, for example:
Admin will be ABC, Editor will be BC, User will be C and so on.

I'd like to list users data including there privileges and I want to replace the letters with words regarding each one.

The following private function is created:
Code:
function _echo_privileges($str)
        {
            switch ($str)
            {
                case 'ABC':
                    return 'Admin';
                    break;
                case 'BC':
                    return 'Editor';
                    break;
                case 'C':
                    return 'User';
                    break;
                default :
                    return 'Error';
            }
            
        }
It is possible to loop inside the query result from the controller, but this will add additional loop to the loop in view to display the list. The loop in the view seems like the following
Code:
<?php
    foreach ($query as $row){
    ?>
    <tr>
        <td>&lt;?php echo $row->id;?&gt;</td>
        <td>&lt;?php echo $row->full_name;?&gt;</td>
        <td>&lt;?php echo $row->email;?&gt;</td>
        <td>&lt;?php echo ($row->gender == 'M')? 'Male' : 'Female';?&gt;</td>
//LOOK At The Following Line ***************
        <td>&lt;?php echo _echo_privileges($row->privileges);?&gt;</td>
//***************
        <td>&lt;?php echo $row->join_date;?&gt;</td>
    </tr>
    &lt;?php
    }
    ?&gt;

Please consider the part between **** in the above code. It ofcourse generates call to undefined function - _echo_privileges() - now I need to make this view able to define this function from the controller!
#2

[eluser]n0xie[/eluser]
Why not add a table to the database where you define your 'letters' and do a simple left join?
#3

[eluser]saidbakr[/eluser]
[quote author="n0xie" date="1262841227"]Why not add a table to the database where you define your 'letters' and do a simple left join?[/quote]

It is considerable idea, but the entity I talk about is very simple and does not require another table. Another thing, sql joining is not simple and it adds more complexity which avoided by the private function.

So is there any way call the controller's private function from the view?
#4

[eluser]Eric Barnes[/eluser]
Personally I would think you should use this as a helper instead. Private functions are really only meant to be accessed by the same class.
#5

[eluser]saidbakr[/eluser]
[quote author="suzkaw" date="1262851736"]Personally I would think you should use this as a helper instead. Private functions are really only meant to be accessed by the same class.[/quote]

Yes! It is very good notice. You remembered me with helpers. alright I think this is the best solution available till this moment.
#6

[eluser]Colin Williams[/eluser]
By the time you get to the view, all this sort of stuff should be figured out. It's just bad design to get to a point where you need to display something and go, "Wait, this still needs formatting." Just do it all before the view.

By your current logic, why not just load a view with no params supplied to it and call a bunch of functions?
#7

[eluser]saidbakr[/eluser]
[quote author="Colin Williams" date="1262854586"]By the time you get to the view, all this sort of stuff should be figured out. It's just bad design to get to a point where you need to display something and go, "Wait, this still needs formatting." Just do it all before the view.

By your current logic, why not just load a view with no params supplied to it and call a bunch of functions?[/quote]

My logic is meant by the application performance. The idea is "Why do I make my application to do another loop to retrieve the data where I can avoid it with a simple function?"

Any formatting, regarding the data I talked about above, in the controller side will require a looping through the result set which such loop will be repeated again in the view, so I cancel the loop in the controller and keep the loop in the view.




Theme © iAndrew 2016 - Forum software by © MyBB