I'm currently refactoring my DLL and BLL and I need some advice on what is the best way to work with DAL and BLL.
My current solution is like this:
// DAL class only consists of SQL queries and returns SQL result
class DAL {
public function isBrandInList($brand)
{
$sql ="SELECT id FROM sl_label WHERE name = '$brand'";
$this->query($sql);
return $this->query_result;
}
}
// BLL will process data from DAL and return data to whatever function called the BLL function.
class BLL extends DAL {
public function isBrandInList($name)
{
$result = parent::isBrandInList($name);
if(mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result)) {
return $row['status'];
}
} else {
return false;
}
}
}
I don't think this is the best way to work and my DAL file is getting over 1000 lines of code and has over 50 different classes. It's getting messy.
My plan is to improve this and start using MySQLi / PDO and prepared statements.
Can anyone tell me how I can improve the way I write my DAL and BLL? Should I create sub class in the DAL, e.g. one subclass for Person and one sub class for Brand?
I've googled and looked at diffrent design patterns. Most of them are for .NET and they don't have any good examples.
And maybe som links to some good articles? The ones I've found hasn't been the best.
UPDATE
This is an example on how I do it today. The code below is just snippets and not the entire code.
//search.php
require_once('classes/search.class.php');
$search = new Search($_GET);
$result = $search->search();
echo $result;
//search.class.php
include('classes/bll.class.php');
class Search extends BLL {
private $store_term;
public function __construct($term) {
parent::__construct();
$this->get_search_term($term);
}
private function get_search_term($term) {}
public function search() {
if(!empty($this->store_term))
$result = $this->search_store();
return $result;
}
private function search_store() {
$result = parent::getStoresBySearch($this->store_term);
$html = 'Code for building html search result list here';
return $html;
}
//bll.class.php
class BLL extends DAL
{
public function __construct() {
parent::__construct();
}
public function getBrandsBySearch($term) {
$result = parent::getBrandsBySearch(mysql_real_escape_string($term));
return $result;
}
}
// dal.class.php
class DAL extends pdo {
public function __construct($cfg_file = 'sl.config') {
parent::__construct($this->dsn, $this->user, $this->pass);
}
public function getStoresBySearch($term)
{
$term = $term.'%';
$sql = "SELECT bla bla bla";
$sth = parent::prepare($sql);
$sth->setFetchMode(PDO::FETCH_ASSOC);
$sth->execute(array(':term'=>$term));
$result = $sth->fetchAll();
return $result;
}
}
I have a fealing that this might not be the best way to code?
class Store {}be considered a BL class? – Steven Mar 25 '11 at 16:32