php - Where should I put my defaults? -


i'm working on web application uses php it's code,and mysql it's storage engine. when working on data model, realized have small issue handling of 'default' data.

i designed mysql schema include defaults, made sense @ time because manipulating data 'by hand' -- it's year later got around adding control panel let others change data.

the issue becomes how handle 'default' values needed objects. technically, leaving things 'null' default, wasn't 1 particularly concerned me before now.

if use mysql defaults, when insert new value have turn around , query 'real' data out of database. if, on other hand, set defaults in php, start violating dry having defaults in 2 places. , if ever 'quick' fix , changes them in 1 place, wind pretty 'interesting' bugs debug. @ same time, can't remove defaults mysql, because part of data schema, , need left in it.

i'm willing bet i'm either overlooking something, or making invalid assumption somewhere, don't know is. have advice on how handle this?

edit:

for clarity, php code might along lines of:

$foo = new foo(); //add in instance-specific data $foo -> save; 

foo's save method run query:

insert foo (bar1, bar2) values (:bar1, :bar2) 

but inserts 2 of (many) variables foo uses. set me in 'default' state. @ moment, have run:

$foo= foo::getfoowithid($foo->id); //i know should use dba; legacy code 

and $foo populated data that's been set in database. e. g. bar3 , bar4 have defaults aren't set when first creating object -- users can change them later if want (and will), aren't part of 'default' object.

should move defaults php? leave them in mysql , pull out of it? violate dry putting them in both places? or missing fourth, better option?

you didn't show example query, i'll assume what's wrong.

if write insert queries in php in such manner omit elements have default values, mysql take on , add them you.

so, have table columns id, user , comment. let's comment has default value. i'm saying can make insert query you'd specify id , user (or user, if id primary, auto-incrementing index), , comment automatically receive default value set in table definition.

edit

whatever works best app design, really... see used , keep them there.

but single rule i'd follow is: don't have defaults in more 1 place, if they're overlapping (as opposed extending).


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -