空文件问题
重写:已解决
大家好,
我目前正在开发一个带有数据库、一堆控制器、视图和模型类的简单应用程序。
我对控制器进行了编码并直接插入数据库
连接 每个控制器方法都有自己的 PDO 来连接到特定的数据库+表。
我重构了它,因为每个控制器有太多活动 PDO,所以我开始编写模型类。
简短信息:当控制器被调用时,模型类被控制器访问一次。 一旦构建了模型对象,它就可以通过整个控制器使用,并且您可以将自定义请求传递给它。 例如 getUserById =>从当前控制器表中获取 ID 为“xy”的用户。
现在我终于完成了模型类并将我的 PDO 类添加到模型中: 每次我想要访问我的任何控制器时,我的 FireFox 都会询问我在哪里保存空的“test.php”(test.php 是我的索引文件)。
重新启动 Apache2 / PHP / MySQL 不起作用,如果我删除代码的某一部分,则不会出现错误,但这部分是必不可少的。 ;)
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());
}
}
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'));
摘录building.php(控制器)
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));
}
}
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));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现问题:
我必须使用 PDO 适配器扩展 Model 类,我不知道为什么,但这就是问题所在。
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.