Welcome Guest, Not a member yet? Register   Sign In
Move code from controller to helpers/models
#1

(This post was last modified: 01-26-2016, 01:44 AM by sebastianvirlan.)

Hi, I am building on My_Controller.php a object based on some config variables:

PHP Code:
protected $config_columns                 = array('product_name', array('category' => 'category_name''relation' => 'belongs_to'), 'slug_produs''price''old_price''short_description''recomandat');
protected 
$config_columns_labels          = array('product_name' => 'Product Name''category' => array('category_name' => 'Category Name'), 'recomandat' => 'Recomandat');
.............................................

foreach(
$this->config_columns as $key => $column):
    if(
is_string($column)):
        
$config_columns['Fields'][$column] = new stdClass();
        
$config_columns['Fields'][$column]->Field $column;
        if(
$this->config_columns_labels[$column]):
            
$config_columns['Fields'][$column]->Label $this->config_columns_labels[$column];
        else:
            
$config_columns['Fields'][$column]->Label ucwords(str_replace('_'' '$column));
        endif;
    elseif(
is_array($column)):
        
$config_columns['Fields'][reset($column)] = new stdClass();
        
$config_columns['Fields'][reset($column)]->Field reset($column);
        
$config_columns['Fields'][reset($column)]->Table key($column);
        
$config_columns['Fields'][reset($column)]->Key $this->{$this->config_model}->belongs_to[key($column)]['primary_key'];
        if(
$this->config_columns_labels[key($column)]):
            
$config_columns['Fields'][reset($column)]->Label $this->config_columns_labels[key($column)][reset($column)];
        else:
            
$config_columns['Fields'][reset($column)]->Label ucwords(str_replace('_'' 'reset($column)));
        endif;
    endif;
endforeach; 


So the foreach build this array:

PHP Code:
Array
(
    [
pk] => id_produs
    
[Fields] => Array
        (
            [
product_name] => stdClass Object
                
(
                    [
Field] => product_name
                    
[Label] => Product Name
                
)

            [
category_name] => stdClass Object
                
(
                    [
Field] => category_name
                    
[Table] => category
                    
[Key] => id_categorie
                    
[Label] => Category Name
                
)

            [
slug_produs] => stdClass Object
                
(
                    [
Field] => slug_produs
                    
[Label] => Slug Produs
                
)

            [
pret] => stdClass Object
                
(
                    [
Field] => price
                    
[Label] => Price
                
)

            [
pret_vechi] => stdClass Object
                
(
                    [
Field] => old_price
                    
[Label] => Old Price
                
)

            [
scurta_descriere] => stdClass Object
                
(
                    [
Field] => short_description
                    
[Label] => Short Description
                
)

            [
recomandat] => stdClass Object
                
(
                    [
Field] => recomandat
                    
[Label] => Recomandat
                
)

        )




I really don't like my practice, how can I split my code into helpers or models? Based on the final object array I build the view. I wanna organize the code in classes. Like

PHP Code:
class Person {
    var 
$name;

    function 
get_name ( ) {
        return 
$this->name;
    }

    function 
set_name ($new_name) {
        
$this->name $new_name;
    }

Reply
#2

(This post was last modified: 01-27-2016, 12:47 PM by cartalot.)

i don't fully understand what you are trying to do - but the first thing you probably want to do is "separate the concerns".
in other words if you are showing products, then you have the concern of getting the products from a db table or some other data source.
which products, what products, what fields to return, etc etc So - Get the Products - that is one concern, and its very appropriate
for that to happen in a Model. Very Important - if no products come back - then that is dealt with in the Controller.
In other words you are not checking if there are products in your View files.

The second concern - which should be completely separate - is how the Products will be displayed in the View or
Show the Products. the key there is to create a data structure where its then very easy to show the products in the View with
the minimum amount of logic in the View file. Where that stage is done is a personal choice but my suggestion is to use a Model
because its the easiest.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB