Model That Stores In File Instead Of Database

I have to install this application in many place’s intranet, so creating a table to store this variables was a waste, also, I need to access easily this variables anywhere in the app.

So i created this model that stores its data in the config file instead of the database, take a look, enjoy, gimme advice… stuff

<?php

/**

  • This is the model class for "something".

*/

class Config extends CFormModel

{

//En vez de usar activeRecord se puede usar CFormModel, Yii lo tiene para crear modelos que generan formularios

//pero que no se relaciona con una tabla

function  __construct(&#036;scenario=''){


  //aca traigo los datos anteriores del archivo donde los guarde


    parent::__construct(&#036;scenario);


    &#036;c = include(  Yii::app()-&gt;basePath .DIRECTORY_SEPARATOR . 'config' .DIRECTORY_SEPARATOR. 'local.php'  );        


    &#036;this-&gt;setAttributes( &#036;c['params'], false);


    //utilizo la llave params porque este arreglo está junto al arreglo de config de yii, así puedo acceder a las 


    //variables en toda la aplicación, pero es caprico


    //hay que usar false, no estoy 100% seguro de porque


}





public &#036;razon_social;


public &#036;nit;


public &#036;pais;


public &#036;departamento;


public &#036;ciudad;


public &#036;direccion;


public &#036;telefono;


public &#036;email;


public &#036;corredor_gtm;





//las propiedades y atributos hay que declararlas manual para poder acceder a ellas











/**


 * @return array validation rules for model attributes.


 */


public function rules()


{


    // NOTE: you should only define rules for those attributes that


    // will receive user inputs.


    return array(


        array('status', 'numerical', 'integerOnly'=&gt;true),


        //igual que en cualqueir modelo


    );


}








/**


 * @return array customized attribute labels (name=&gt;label)


 */


public function attributeLabels()


{


    return array (


        'razon_social'  =&gt;  'Razon Social',


        'nit' =&gt; 'Nit o Rut',


        'pais' =&gt; 'Páis',


        'departamento' =&gt; 'Departamento',


        'ciudad' =&gt; 'Ciudad',


        //igual que en cualqueir modelo


    );


}








public function save(){


  // esta es la parte pro, hay que sobreescribir la función save para que guarde en un archivo en 


  //vez de guardar en la tabla.


  // en mi caso lo que hice fue usar var_export para guardar el php de la declaracion del arreglo


  //meterlo en el archivo, le pongo antes que return y asi lo puedo leer despues usando include(&#036;archivo) 


  //y cargarlo en una variable


    &#036;l = Yii::app()-&gt;basePath .DIRECTORY_SEPARATOR . 'config' .DIRECTORY_SEPARATOR. 'local.php' ;


    &#036;c = var_export(&#036;this-&gt;attributes, true);


    &#036;f = fopen(&#036;l,'w');


    if(&#036;f === false){


        fclose(&#036;f);


        return false;


    }


    &#036;c = &quot;&lt;?php

return array(

'params'=&gt; &quot;.&#036;c. &quot;,

);

";

    if( fwrite(&#036;f,&#036;c) === false) {


        fclose(&#036;f);


        return false;


    }


    fclose(&#036;f);


    return true;


}

}

gist. github. com /dvidsilva/ 5930338

Using SQLITE could do the trick too, not?