改写:已解决

嗨,您好,

我目前在一个简单的应用程序上工作,该应用程序具有数据库,一堆控制器,视图和一个模型类。

我对控制器进行了编码,并直接插入了数据库连接

例如。
每个控制器方法都有自己的PDO以连接到特定的数据库+表。

我之所以进行重构,是因为每个1个控制器有太多活动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' => 'peter@hasnomail.org');
//$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));
    }
}

最佳答案

我发现了问题:

我必须使用PDO适配器扩展Model类,我不知道为什么,但这是问题所在。

关于php - 空文件问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1941791/

10-09 06:53
查看更多