- Region is performing a piece of code (logic+queries) from the save() method in Country and then some more
- City is performing a piece of code from the save() method in region and then some more
If I were to also have a class for, say neighborhoods, the nesting and repetition of code would be even worse.
The problem is, in this example I use transactions. These are important because I may make several updates to different tables and I want to rollback in case anything goes wrong. And since nested transactions are not possible (yet?) I cannot just reuse the save methods of one class in the other. So I can not let the City's save() method use the Region's save() method instead of - as it is now - duplicate all the logic and queries.
What direction would you take to solve this problem?
p.s. the code is incomplete to make it more readable, just enough to show the problem hopefully
Code: Select all
class Country {
protected $country;
function __construct(){
$conn = new PDO('odbc:*', '*', '*', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
}
function save(){
$conn->beginTransaction();
try {
// Check if country already exists
// if not INSERT COUNTRY
$this->conn->execute("INSERT INTO countries (country) VALUES ($this->country)");
$conn->commit();
}
catch (Exception $e) {
$conn->rollBack();
}
}
}
class Region {
protected $region;
protected $country;
function __construct(){
$conn = new PDO('odbc:*', '*', '*', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
}
function save(){
$conn->beginTransaction();
try {
// Check if country exists
// if not first INSERT country
$this->conn->execute("INSERT INTO countries (country) VALUES ($this->country)";
// then get lastInsertId as $countryId and use that in following query
$this->conn->execute("INSERT INTO regions (region, countryid) VALUES ($this->region, $countryId)");
$conn->commit();
}
catch (Exception $e) {
$conn->rollBack();
}
}
}
class City {
protected $country;
protected $region;
protected $city;
function __construct(){
$conn = new PDO('odbc:*', '*', '*', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
}
function save(){
$conn->beginTransaction();
try {
// Check if region exists
// if not first INSERT region
// but maybe country doesn't yet exists, so first check if country exist
// if not INSERT country
$this->conn->execute("INSERT INTO countries (country) VALUES ($this->country)";
// then get lastInsertId as $countryId and use that in following query
$this->conn->execute("INSERT INTO regions (region, countryid) VALUES ($this->region, $countryId)");
// then get lastInsertId as $regionId and use that in following
$this->conn->execute("INSERT INTO cities (city, regionid, countryid) VALUES ($city, $regionId, $countryId)");
$conn->commit();
}
catch (Exception $e) {
$conn->rollBack();
}
}
}