First of all, you should consider:
- There is no good general model. Each project needs its own model.
- easy to read, manageable code
- do not repeat the same code (or requests), so if you have a function for a specific task and you want it to be ordered in another way, change it and do not clone it
- Use complex data structures such as arrays or objects to send data to a function, so you don’t always have to change the parameters necessary for the function
- resource usage. The more you want everything around, the overall solution, the more resources it will use.
How badly will performance impact if I select * just so that the calling function might need some information hidden down?
It depends on the loading of your site. Most of the time (unless you pull large drops and text) * is fine, but when resources are scarce, you need to specify columns. This way you can save I / O time.
I feel this makes the model extremely bloated and very attached, creating a lot of duplicate code. Maybe it’s better to have such a model,
Perhaps try the following:
First of all, for complex queries, I use this class, which I did a long time ago for MySQL. This helps to solve the problem.
class sqlAssembler { private $data = array(); var $S = array(); var $F = array(); var $W = array(); var $G = array(); var $H = array(); var $O = array(); var $L = array(); //Clause abbreviations var $clauselist = array ( 'S' => 'SELECT', 'F' => 'FROM', 'W' => 'WHERE', 'G' => 'GROUP BY', 'H' => 'HAVING', 'O' => 'ORDER BY', 'L' => 'LIMIT' ); //Default clause separators var $clausesep = array ( 'S' => ',', 'F' => ',', 'W' => ' AND ', 'G' => ',', 'H' => ' AND ', 'O' => ',', 'L' => '' ); function gen() { $tmp = ''; foreach ( $this->clauselist as $area => $clause ) { if ( count($this->{$area}) ) { $tmp .= ($clause != 'S' ? ' ' : '') . $clause . ' '; for ($i=0; $i < count($this->{$area}); $i++) { //echo $area = (string)$area; $tmp .= $this->{$area}[$i]; } //for } //if } //foreach return $tmp; } //function function genSection($area, $showsection = 0) { $tmp = ''; if ( count($this->{$area}) ) { for ($i=0; $i < count($this->{$area}); $i++) { $tmp .= $this->{$area}[$i]; } //for } //if return $tmp; } //function function clear() { foreach ($this as $area => $v) { //We only care about uppercase variables... do not declare any else variable with ALL UPPERCASE since it will be purged if (ctype_upper($area)) { if ($area == 'L') $this->$area = ''; else $this->$area = array(); } //if } //foreach } //function public function add($area, $str, $criteria = 1, $sep = '#') { if ($criteria) { if ($sep == '#') $sep = $this->clausesep[$area]; //Postgres' OFFSET should be set like: $str = '25 OFFSET 0' //Not very neat I know, but fuck it if ($area == 'L') { $this->{$area} = array(); } //if //$ref = $this->$area; $this->{$area}[] = (count($this->$area) ? $sep : '').$str; return count($this->$area)-1; } //if } //function public function del($area,$index) { if ( isset($this->{$area}[$index]) ) unset($this->{$area}[$index]); else trigger_error("Index nr. {$index} not found in {$area}!",E_USER_ERROR); } //function //-*-* MAGIC CHAIN FUNCTIONS public function S($str,$criteria = 1,$sep = '
Perhaps try the following:
function getShoppingCart($d) { $xx = new sqlAssembler(); $xx->S('*')-> F('items')-> //Notice, that we specified a criteria... if $d['id_item'] exists it will be joined to the WHERE clause, if not it will be left out W("(id_item > '{$d[id_item]}')",$d['id_item'])-> //Same here O("dt DESC",$d['date']) $sql = echo $xx->gen(); //id_item = 11, date = 2009-11-12 //$sql = "SELECT * FROM items WHERE (id_item > '11') ORDER BY dt DESC"; //id_item = null, date = null //$sql = "SELECT * FROM items"; $data = sqlArray($sql); //... handle data }