Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Understanding EAV
  • In the docs, there is a note saying when a model has EAV container defined, you can no longer use custom data properties for that model as every new property will be seen as a new EAV key.

    I understand that part but how would you add new properties? 
  • Define "new properties"? new in what?
  • You want to add new property in your model which has EAV container, but you don't want to save it in your DB ?
  • Well I was just pulling that from the note in the docs, 'as every new property will be seen as a new EAV key'. 

    Maybe I misunderstood what the statement meant?

    This is what I understand:

    Model_User model would contain all of the basic ORMauth fields, such as following:
    'id',
    'username',
    'password',
    'group_id',
    'email',
    'last_login',
    'previous_login',
    'login_hash',
    'user_id',
    'created_at',
    'updated_at',

    Then there is Model_Users_Meta which has the following:
    'id',
    'parent_id',
    'key',
    'value',
    'user_id',
    'created_at',
    'updated_at',

    Then if I want to add First and Last name to a User and it will go to users_metadata table, 
    I kinda understand how you would be able to add properties from coding end but I don't know how a person would be able to go online and add new properties, such as age or additional email address, to the user through the website application. 


  • @Syntaxlb No I do want to save everything to my database.
  • In a standard ORM Model, if you do $model->property, then "property" is either the name of a column (i.e. it's listed in your $_properties array), or it is a custom property (i.e. it is stored in the model but not part of the record).

    Custom properties are for example used to add temporary data to the object, when you pass the object to a view, to avoid having to pass data as individual variables. For example if the record contains first and last name, you could store fullname: $model->fullname = $model->firstname.' '.$model->lastname;

    When you activate EAV, the model assumes all properties that are not defined in the $_properties array are EAV properties. So the example earlier would create a "fullname" entry in the EAV table.

    So, as to your last question: just assign a value to a new property:

    $model->firstname = "John":
    $model->lastame = "Doe";
    $model->save();

    This will create two Model_Users_Meta (your EAV table) records, one with a key of "firstname" and one with a key of "lastname". And with a "parent_id" linking it to the user (it's the FK).

    Under the hood, and EAV table is a plain one-to-many relation.
  • When adding new property, where would you define that? In Controller_Users or in the Model_Users_Meta?
  • What do you mean by "property"?

    In EAV, data is stored as a key-value pair, so in the above example, you get two records in your EAV table, one with key="firstname", value="John", and one with key="lastname" and value="Doe".

    There is no need to define anything, this works automatically.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion