質問

I will be working on a system that will be using an EAV structure to store object data. The model itself will have some pre-defined attributes (coordinates and date/time stamps), namely:

  • X
  • Y
  • Z
  • start date/time
  • end date/time

The rest of the attributes will be dynamically created by the user, and added as rows in the attributes table. These rows will be pivoted and used as columns to align with the values table (wasn't sure if this required an explanation, but figured better safe than sorry).

The object (or entity) to attributes relationship is one-to-many.

The attribute to values relationship is one-to-many.

I am new to Eloquent/Laravel but I have read through the relationships documentation and see that there is functionality for one-to-many relationships, however, I am unclear how I can reference all the data in an EAV (seems more geared toward RDBMS). Would it be something like this (please keep in mind this is a crude example)?

class UserObject extends Eloquent {

    public function object_data()
    {
        return $this->hasMany(attributes_data);
    }

    public function attributes_data()
    {
        return $this->hasMany('Values');
    }

}

Basically, my question is:

Is there an accepted (or proper) approach for designing/writing a model when retrieving all data associated with an EAV? If so, can you point me towards some documentation or tutorials for Eloquent/EAV approaches?

Some after thoughts:

My thinking was that I would need to create 3 classes for each component of the EAV (i.e. one for the entity component data, one for the attributes data, and one for the values data).

Thank you.

役に立ちましたか?

解決

There are a few approaches that can be done to go about this (as I have discovered through other forum posts):

Create a model for each EAV component, e.g. pseudo code:

class Object extends Eloquent{
    public function objects(){
        return $this->hasMany('Attribute');
    }
}

class Attribute extends Eloquent{
    public function attributes(){
        return $this->hasMany('Value');
    }
}

class Value extends Eloquent{
    public function values(){
        return /* etc. */;
    }
}

This will cause a performance hit as there will be 3 queries executed just to retrieve value data.

Use Query Builder

select()->join()->where("attribute", "=", "something")

This will also cause a performance hit.

DB features of Laravel

Basically running the SQL queries directly will probably cause the least amount of overhead, example from Laravel site:

 $results = DB::select('select * from users where id = ?', array(1));

Side note:

For this kind of system (where attributes are dynamic and unknown), it would be best to avoid EAV entirely and stick to a No-SQL solution, where working with Laravel would be more intuitive. I have considered this option and will go with this on the long-term.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top