Yii Framework Forum: [EXTENTION] ArrayModel - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

[EXTENTION] ArrayModel Allows to access, validate and manipulate array data located on the fi Rate Topic: -----

#1 User is offline   Instinct 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 23
  • Joined: 21-July 10

Posted 30 June 2011 - 06:10 PM

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.yiiframew...ion/arraymodel/

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

Instinct
1

#2 User is offline   Instinct 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 23
  • Joined: 21-July 10

Posted 30 June 2011 - 07:11 PM

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

#3 User is offline   dinhtrung 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 23-May 11
  • Location:Hanoi, Vietnam

Posted 15 July 2011 - 01:54 AM

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?
0

#4 User is offline   Instinct 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 23
  • Joined: 21-July 10

Posted 15 July 2011 - 05:08 AM

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
0

#5 User is offline   ucupmania 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 15-April 13
  • Location:Indonesia

Posted 13 June 2013 - 09:57 PM

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
0

#6 User is offline   ucupmania 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 15-April 13
  • Location:Indonesia

Posted 17 June 2013 - 02:26 AM

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
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users