I am looking for a good way to organize some files in a way that accords with good organizational practices.
I have a class that contains several SQL queries on a database table. A second table was created that is identical to the first except that now to look for an individual row one more value has to be provided to the WHERE clause of a query.
Some example code:
class Table1
{
public function getRowByValue($value1) {
$query = "SELECT name, age, address FROM table1 WHERE value1 = :value1";
$db->prepare($query);
$db->bindValue(':value1', $value1);
$db->execute();
return $db->fetch();
}
}
class Table2
{
public function getRowByValue($value1, $value2) {
$query = "SELECT name, age, address FROM table2 WHERE value1 = :value1 AND value2 = :value2";
$db->prepare($query);
$db->bindValue(':value1', $value1);
$db->bindValue(':value2', $value2);
$db->execute();
return $db->fetch();
}
}
All of the columns are identical in each table (15+), but table2’s primary key is different, so in order to find a unique row, an additional value is needed. (This is by design and cannot be changed.)
I thought of two ways to do this. The first was to make two different classes and just have a lot of duplicate code, as above. (The functions are more complicated and longer than the example.) But any change in class1 would have to be replicated in class2, something that can lead to errors overtime since things are not all in one place. The second was to check if value2 was given and execute a different query if it was:
class Table1
{
public function getRowByValue($value1, $value2 = null) {
if ($value2 === null) {
$query = "SELECT name, age, address FROM table1 WHERE value1 = :value1";
$db->prepare($query);
$db->bindValue(':value1', $value1);
$db->execute();
}
else {
$query = "SELECT name, age, address FROM table2 WHERE value1 = :value1 AND value2 = :value2";
$db->prepare($query);
$db->bindValue(':value1', $value1);
$db->bindValue(':value2', $value2);
$db->execute();
}
return $db->fetch();
}
}
Are there any better solutions to this? Or which one of these should I use?