tags:

views:

61

answers:

1

REWROTE: SOLVED

Hi there,

I currently worked on a simple application with a database, a bunch of controllers, views and a model class.

I coded the controllers and inserted the db connections directly

E.g. Each controller method has his own PDO to connect to a specific database+table.

I refactored this because I had too many active PDOs per 1 controller, so I started to code the model class.

A short information: The model class is once accessed by a controller, when the controller is called. Once the model object is constructed it is available through the whole controller, and you can pass custom request to it. E.g. getUserById => Gets the User from the current controller table with the id "xy".

Now that I finally finished the model class and added my PDO class to the model: Everytime I want to access any of my controllers, my FireFox asks me where to safe the empty "test.php" (test.php is my index file).

Restarting Apache2 / PHP / MySQL did not work, if I remove a certain part of my code, there is no error, but this part is essential. ;)

model.php

class Model extends db_pdo_adapter{
    public function __construct($name)
    {
     $name = strtolower($name);
     $this->dbh = parent::connect(Model::ATTR_HOST, Model::ATTR_USR, Model::ATTR_PASSWD, Model::ATTR_DB);
     $this->name = $name.'s';
     //$this->ATTR_TBL = $this->name;

    }

    public function __call($name,$values)
    {
     $string = preg_replace('/^get/','',$name);
     $string = strtolower($string);
     $by = preg_split('/by/',$string);
     $by = strtolower($by[1]);
     return $this->get($string, $by, $values); // when I remove this part no empty file is served.
    }

    public function get($item, $by, $conditions) // single item if is_no_array
    {
     if($item = preg_replace('/$s/','',$this->name))
     {
      $item = '*';
     }
     //if(count($conditions) <= 1)
     //{
      $query = 'SELECT ' . $item . ' FROM ' . $this->name . ' WHERE ' . $by . ' = :' . $by . '';
      $pname = ':'.$by;
     //}

     $this->dbh->getStatement($query);
     $this->dbh->bindParam($pname,$conditions[0]); // ->dbh-> also was missing
     $this->dbh->exec();
     return ($this->dbh->fetchAll());

    }
}

Extract of test.php

 header('Content-Type: text/html;');
$time_start = microtime(true);



 include_once('db/model.php');
 //include_once('village.php');
 //include_once('player.php');
 include_once('building.php');

//$village = Village::getVillage('12');
//$player = Player::getPlayer('423');
//$data = array('name' => 'peter','password' => 'nopasswd','email' => '[email protected]');
//$player->newPlayer($data);
//print_r($village->attr);
//print_r($player->playerObj);
//include('interface.phtml');
//var_dump($_SERVER);
//print_r($village);
//print_r($player);
echo '<br />';
var_dump(Building::getBuilding('321'));

Extract of the building.php (controller)

class Building{
    private function __construct($id,$village = NULL)
    {
     $this->model = new Model(__CLASS__);
     $model = $this->model;

     $this->buildingObj = $model->getBuildingById($id);
    }

    public function getBuilding($id,$village = NULL)
    {
     return (new Building($id));
    }
}
A: 

I found the problem:

I have to extend the Model class with the PDO adapter, I do not know why, but this was the problem.

daemonfire300
Happy to hear you solved the issue. No need to add [solved] to the title. Just accept your own answer :)
Mike B
This requires the question to be 2 days or older, I am already on vacation in 2 days ;)
daemonfire300