Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

iannazzi's avatar

Using a table to define the model AND the view

After writing about 50 similar forms for db entry, update, edit, I had written a workflow that used a "table definition" to define my data and what view we were going to present. This table definition would have the database fields that would correspond the keys in an array returned from a query. It would also have what type of element we needed for the view: a select, an input, tree select, textarea, checkbox, etc. Some expected input/validation code would also be in there. I could pass the table definition to a function to extract the data from the database, then I would pass the same table along with the data to a function that created the html for the view.

For example, a discounts table definition {return array( array( 'db_field' => 'pos_discount_id', 'type' => 'input', 'tags' => ' readonly = "readonly" ', 'caption' => 'Discount ID', 'value' => $pos_discount_id, 'validate' => 'none'

                            ),
                    array('db_field' =>  'discount_code',
                            'type' => 'input',
                            'db_table' => 'pos_discounts',
                            'caption' => 'Discount Code',
                            'validate' => $unique_validate),    
                    array('db_field' =>  'discount_name',
                            'type' => 'input',
                            'caption' => 'Discount Name',
                            'validate' => 'none'),
                
                    
                    array('db_field' =>  'discount_amount',
                            'type' => 'input',
                            'caption' => 'Discount Amount',
                            'validate' => 'number'),
                    array('db_field' =>  'percent_or_dollars',
                            'type' => 'select',
                            'caption' => '$ or %',
                            'html' => createEnumSelect('percent_or_dollars','pos_discounts', 'percent_or_dollars', 'false',  'off')),       
                            
                    
                    array('db_field' =>  'comments',
                            'type' => 'input',
                            'caption' => 'Comments'),
                    array('db_field' =>  'active',
                            'type' => 'checkbox',
                            'caption' => 'Active',
                            'value' => '1')
                    );  </code>}

we would get the data: $data= selectSingleTableDataFromID($db_table, $key_val_id, $data_table_def);

then create the view: first create the html for the table $big_html_table = createHTMLTableForMYSQLInsert($data_table_def); //add whatever other form fields you need to that sting... //then wrap the html in a form $html .= createFormForMYSQLInsert($data_table_def, $big_html_table, $form_handler, $complete_location, $cancel_location);

As I have about 100 tables this reusable code get used quite a bit.

After watching all the laravel 5 basics and fundamentals and intermediate videos I didn't see a section where this would be considered a good idea, in fact view presenters video mentions combining any information in the model that the view would need is a bad idea.

Can you guys maybe point me in the right direction here.... is there an accepted method to define how we will represent the data in the model on the view... I.e. if we expect an element to be a checkbox, can I just put that into the model? If not where, and is there a video tutorial on this?

0 likes
0 replies

Please or to participate in this conversation.