A page has several elements, and they are linked using the page_to_elements table. Each element has several element_fields and is linked using element_to_element_fields . Each element_field has a type and is linked using the element_to_element_fields table. The values of each element_field inside an element have a value (eitehr in value_char, value_text or value_num), which is stored in the element_values table.
The following is the structure of the database:
pages: id|name elements: id|name element_fields_types (sql_type can be char, text or num): id|name|sql_type element_fields (names can be title, intro, content, link, number, etc etc): id:element_field_type_id|name element_to_element_fields: id|element_id|element_field_id page_to_elements: id|page_id|element_id element_values: id|page_id|element_id|page_to_element_id|element_field_id|value_char|value_text|value_num
What I'm looking for is a good hasManyToMany solution to get all values when I request the page id. Now I have several loops and creating arrays to get such a structure (where the values are specified from the correct column name based on what was set in element_fields ):
$page = array( 'elements' => array( [0] => array( 'element_name_here' => array( 'fields' => array( [0] => array( 'field_name_here' => 'Field value', 'field_name_here' => 'Field value', 'field_name_here' => 'Field value', 'field_name_here' => 'Field value' ), [1] => array( 'field_name_here' => 'Field value', 'field_name_here' => 'Field value', 'field_name_here' => 'Field value', 'field_name_here' => 'Field value' ), [2] => array( 'field_name_here' => 'Field value', 'field_name_here' => 'Field value', 'field_name_here' => 'Field value', 'field_name_here' => 'Field value' ), ) ) ), [1] => array( 'element_name_here' => array( 'fields' => array( [0] => array( 'field_name_here' => 'Field value', 'field_name_here' => 'Field value', 'field_name_here' => 'Field value', 'field_name_here' => 'Field value' ), [1] => array( 'field_name_here' => 'Field value', 'field_name_here' => 'Field value', 'field_name_here' => 'Field value', 'field_name_here' => 'Field value' ), [2] => array( 'field_name_here' => 'Field value', 'field_name_here' => 'Field value', 'field_name_here' => 'Field value', 'field_name_here' => 'Field value' ), ) ) ), ) );
I need something like below to create the above array:
$page = Page::find($id); print_r($page->getValues->toArray());
I have some experience using ownToMany or hasManyToMany, but this never happens.
Any help would be greatly appreciated.