Welcome Guest, Not a member yet? Register   Sign In
Generic Models & Views vs Bespoke Models and Views?
#1

[eluser]Mikeboy[/eluser]
Hello

1 - Generic Model vs Multiple Bespoke Models

For the particular application I am creating, all database queries are completed via stored procedures.

So could you therefore have one generic model class, and pass to it from the controller, the name of the procedure to execute, and the ID or names of the fields to return (if a select).

i.e.
Code:
$this->generic_model->add(sp_xxx_ins, $v1, $v2, $v3);
(= 'exec sp_xxx_ins $v1, $v2, $v3')

$this->generic_model->add(sp_xxx_del, $id);
(= 'exec sp_xxx_del $id')

$this->generic_model->add(sp_xxx_sel, $id, $name, $desc, $etc);
(= 'exec sp_xxx_sel $id' and then return the requested fields.)

Syntax may not be correct or the best way for passing the parameters, I haven't checked, but the idea is the same..

Is this a good way? It will cut out a lot of duplicated code. But is it generally best to have a 'model per table'?



2 - Generic View Template vs Multiple Bespoke Views

Different views - what is better:

1) Create multiple bespoke views with info such as forms, table headings, titles etc hard coded in them.

or

2) A single generic view template. Build up the dynamic data in the controller, i.e. build a form with the form helper, or create an array of table headings - and then pass this data into a generic view template?


What I am thinking is with the above ways you could have a few generic View templates (i.e. a generic table view, a generic form view, etc) and a single/or few generic Model. But then of course the controller may get quite large? Maybe the controller is doing some of the work that should be handled by the View ???


Please can someone advise the best direction to take? I want to make sure I am following the MVC structure as conventionally as possible.

Thanks Smile

Mike
#2

[eluser]Jim OHalloran[/eluser]
[quote author="Mikeboy" date="1185232146"]Hello

1 - Generic Model vs Multiple Bespoke Models

Is this a good way? It will cut out a lot of duplicated code. But is it generally best to have a 'model per table'?
[/quote]

I usually use for one per table, but in special circumstances I use a model which might access multiple tables. e.g. in my last project I had to produce some complex reports which pulled data from several tables. Rather than coding the reports using the existing model classes I was able to do most of the heavy lifting in SQL, and had a "reports" model which executed the queries, munged the results and returned them.

That said, models are an an optional feature in CI, you're not required to use them, and there doesn't seem to be any one "right way". If you watch {url=http://video.derekallard.com/video/video]Derek Allard's Video[/url], Derek returns HTML fragments from his models. I prefer to return arrays of data from my Models, then rely on views to transform that into HTML, but either approach is valid.

In your app, you could use a single model, or you could avoid them completely, whatever works best for you!

[quote author="Mikeboy" date="1185232146"]
2 - Generic View Template vs Multiple Bespoke Views

Different views - what is better:
[/quote]

I tend to favor Bespoke views. I've found that eventually you need something slightly different for one view that the generic view doesn't cope with, and the generic view either becomes extremely complex, or you end up the odd bespoke view anyway. The downside of bespoke views is that if you later need to change something in your views you tend to end up having to change a lot of views. I do use generic header/footer views though.

Hope that helps.

Jim.
#3

[eluser]Mikeboy[/eluser]
Perfect, great help

Thanks very much Jim
#4

[eluser]Crafter[/eluser]
If you are using stored procs, with a nice database library like ActiveRecord/ORM, then you could eliminate the model layer without batting an eyelid.

I agree with Jim's stance that views are very specific. Generic views will only work if your data dictionary is stored and retrievable by your application at run time, so that view requirements for specific fields can be acted upon.




Theme © iAndrew 2016 - Forum software by © MyBB