Reputation: 765
Background :
I am trying to create a flat file key value store. When i use the code below the array that has been written to the file gets deleted and i end up with an empty file. On the next request the file gets populated with the array. This process loops itself on every page request.
What i have tried :
Nothing seems to work! I end up with the same loop of an empty file -> file with array and it continues
Question :
Can someone more experienced tell me what i am doing wrong?
code :
/**
* Class Orm
*/
class Orm
{
/**
* @var string The Root database directory with a trailing slash. default is "./database/".
*/
static $dir = "./database/";
/**
* @var array
*/
protected $data;
/**
* @var string
*/
protected $file = "";
/**
* @param $file
* @return string
*/
public function load_table($file)
{
try {
if (file_exists(self::$dir . $file)) {
return $this->file = $file;
} else {
throw new Exception("The file " . $file . " cannot be created, because it already exists");
}
} catch (Exception $error) {
return $error->getMessage();
}
}
/**
* @param String
* @param array $values An associative array of values to store.
* @return array
*/
public function set($key, $values = array())
{
try{
if (!empty($key) && !empty($values)){
return $this->data[$key] = $values;
} else {
throw new Exception();
}
} catch (Exception $error){
return $error->getMessage();
}
}
public function save()
{
try{
if (file_exists(self::$dir . $this->file)) {
if (filesize(self::$dir . $this->file) == 0)
{
file_put_contents(self::$dir . $this->file, print_r($this->data, TRUE));
}else{
$tmp = file_get_contents(self::$dir . $this->file);
$content = array_merge($tmp, $this->data);
file_put_contents(self::$dir . $this->file, print_r($content, TRUE));
}
} else {
throw new Exception();
}
} catch(Exception $error){
return $error->getMessage();
}
}
}
$user = new Orm();
$user->load_table("users");
$user->set("Tito",array("age" => "32", "occupation" => "cont"));
$user->save();
PS : I thought this would be a nice project to familiarize myself with Php. So please do not advise to use SQL as this is for learning and understanding Php only.
Upvotes: 2
Views: 141
Reputation: 4996
I can not say why your code fails to do that or not. However I would create another object as well that takes care of loading and saving a string to disk. Nothing more and nothing less:
class StringStore
{
private $path;
public function __construct($path) {
$this->path = $path;
}
/**
* @return string
*/
public function load() {
... move your load code in here
return $buffer;
}
/**
* @param string $buffer
*/
public function save($buffer) {
... move your save code in here
}
}
That might look a bit less, however you can move a larger part of code out of the ORM class. If the problem is with storing to disk (e.g. maybe some mistake made?) you can fix it in the store class then.
If the mistake has been made in the string processing, then you know you need to look in the ORM class instead to continue fixing.
Upvotes: 3