[EXTENTION] ArrayModel

Hey everyone,

While developing an online role playing game, I just needed a model for managing static array data like cities, ranks, possessions and configuration files. First, I made something simple which was mostly spelialized for the game. After a while I was thinking about sharing it with you all, because you never know if you will need it ;). In my opinion it is also a great addition to Yii. I extended it with some more functions to let it work like CActiveRecord, and made it more developer-friendly.

For more information, check http://www.yiiframework.com/extension/arraymodel/

Feedback will be very welcome :D I hope I helped people with this.

Instinct

Oops @ typo in the topic title :D to moderators: can [extention] be replaced by [extension]? Would be nice!

I tried your extension. There is something I don’t understand much.

First of all, the Data file must be create manually. Why not check for it right when the model is init or created?

Second, I cannot understand about the dataStructure() method. For example, I have the following data Structure:




public function arrayStructure()

    {

        return array(

        	'name'	 => 'NoOp',

        	'description'	=>	'Do Nothing (No Operation)',

                'detail' => '',

        );

    }



Then if I use




setAttributes(array("name" => "test", "description" => "sample", "detail" => "test me if you can."));



Then it will raise error complain that there is no such attribute name.

But if I change the model to:




public function arrayStructure()

    {

        return array(

           'name' => array(

                'name'	 => 'NoOp',

        	'description'	=>	'Do Nothing (No Operation)',

                'detail' => '',

            ),

        );

    }



Then it’s working. Strange. What is the meaning of the name wrapper?

Hi Dinhtrung,

In the latest version of this extension it is not necessarily needed anymore to manually create the data file. The data file will be automatically created when some data is getting saved to the model. In the very first version it was indeed needed to manually create the data file, but I haven’t changed the “manual” yet.

And to answer your second question, the array structure must be there to define how the data file is structured. The array structure must be just like that the keys of arrayStructure() are the actual keys in the data file, and the values of array_structure() are the unique corresponding attribute names of the model.

So if your data file is something like this:




array(

    'name' => 'Aasdd asd ads',

    'description' => 'Sample bla',

    'detail' => '',

);



Then your arrayStructure() should be like this:




public function arrayStructure()

{

    array(

        'name' => 'name',

        'description' => 'description',

        'detail' => 'detail',

    );

}



In this case, the element ‘name’ inside the data file can be accessed by calling $model->name. In your case you can just use the same attribute names as the key names inside the data file.

To make it something more clear, if your arrayStructure() is like this:




public function arrayStructure()

{

    array(

        'name' => 'name_new',

        'description' => 'description_new',

        'detail' => 'detail_new',

    );

}



Then you can for example access the element ‘name’ inside the data file by calling $model->name_new.

I made the model work like this, because then the developer can use associative array data structures, like this:




public function arrayStructure()

{

    array(

        'name' => array(

            'old' => 'name_old',

            'new' => 'name_new',

        ),

        'description' => 'description',

        'detail' => 'detail',

    );

}



In this example, you can access array[‘name’][‘old’] by calling $model->name_old and array[‘name’][‘new’] by calling $model->name_new.

I understand that the arrayStructure() can be very confusing and I hope this explaination helps you a bit. Feel free to ask more questions.

Instinct

Hi Instinct,

I’m new comer in Yii. I have a project that have to store my data in Array before they have to inserted in database, and I found your extension, this arraymodel. because I still new here in Yii, I follow your instruction, but it failed. I do really need to know how to implement or install it. I got error that

include(ArrayModel.php): failed to open stream: No such file or directory”. seems like Yii can’t find ArrayModel.php, My questions are where should i put that extensions and how to use it…

thanks

I’m sorry for questioning old document… I think I made mistake when I using EArrayModel as an extend model… because there is no file using name EArrayModel.php… And It solved when I renamed ArrayModel.php… :)

My deeply apologize…

Thx