适当的OOP PHP类结构和数据库交互

时间:2014-04-24 16:31:06

标签: php mysql class oop pdo

我对OOP和PHP相当新。我一直在通过Kevin Yank的书,并希望继续推进一些OOP概念。但我有点失落,我需要帮助。

我正在尝试使用供应商类从数据库生成供应商列表。 城市和国家等字段使用ID(使用城市和国家/地区表的关系数据库),因此在我在html输出文件中发布完整列表之前,我还需要能够获取这些名称值。

我可以在程序上完成所有这些工作,但在搬到OOP时努力寻找合适的结构。

我按照Kevin的说明为我的PDO数据库连接创建了一个db.inc.php文件:

<?php 

//db connection using PDO class object

try {

    //create new PDO class object
    $pdo = new   
    PDO('mysql:host=122.2.2.2;port=3307;dbname=mydbname','dbuser','password');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->exec('SET NAMES "utf8"');

}
catch (PDOException $e)
{
    //if unable to connect, tell us why
    $output = 'Unable to connect to the database server 2.'.$e->getMessage();
    include 'output.html.php';
    exit();
}

我创建了一个供应商类。这个类将包含操纵供应商的所有方法:getVendors(),getVendor($ id),deleteVendor($ id),updateVendor($ id)等。

<?php


class Vendor {

    protected $name;
    protected $address;
    protected $city;
    protected $state;
    protected $postal;
    protected $country;
    protected $tel;
    protected $email;
    protected $web;


    public function __construct(){

        //typically first function to appear in a class
        // Constructor using MagicMethod
        $this->name = $name;
        $this->address = $address;
        $this->city = $city;
        $this->state = $state;
        $this->postal = $postal;
        $this->country = $country;
        $this->tel= $tel;
        $this->email = $email;
        $this->web = $web;

    }

    public  function getVendors($pdo){

        //get all vendor data from db
        try
        {
            //prepares a statement for execution and returns a statement object
            $query = $pdo->prepare("SELECT * FROM vendor ORDER BY name");
            $query->execute();

            $vendors = array();

            foreach ($query->fetchAll() as $row){

            //create array of member objects
            $vendors[] = new Vendor($row);
        }

        //return member objects
        //not sure this is the right thing to do here
        return $vendors;

        }
        catch (PDOException $e)
        {
             //handle any errors
             $output = 'Error fetching vendors: ' . $e->getMessage();
             include 'output.html.php';
             exit();

        }//end try catch
    }//end function


    //setters

    public function setName( $name ){
        $this->name = $name;
    }

    public function setAddress( $address ){
        $this->address = $address;
    }

    public function setCity( $city ){
        $this->city = $city;
    }

    public function setState( $state ){
        $this->state = $state;
    }

    public function setPostal( $postal ){
        $this->postal = $postal;
    }

    public function setCountry( $country ){
        $this->country = $country;
    }

    public function setTel( $tel ){
        $this->tel = $tel;
    }

    public function setEmail( $email ){
        $this->email = $email;
    }

    public function setWeb( $web ){
        $this->web = $web;
    }


    //getters

    public function getName(){
        return $this->name;
    }

    public function getAddress(){
        return $this->address;
    }

    public function getCity(){
        return $this->city;
    }

    public function getState(){
        return $this->state;
    }

    public function getPostal(){
        return $this->postal;
    }

    public function getCountry(){
        return $this->country;
    }

    public function getTel(){
        return $this->tel;
    }

    public function getEmail(){
        return $this->email;
    }

    public function getWeb(){
        return $this->web;
    }

}

我有一个调用供应商列表的index.php文件

<?php 

//get all vendors

include_once $_SERVER['DOCUMENT_ROOT'] .'/db.inc.php'; 
include_once $_SERVER['DOCUMENT_ROOT'] .'/includes/Vendor.Class.php'; 

//instantiate class
$vendor = new Vendor();

//calling my function to get a list of all vendors
//not sure if passing in $pdo object is a good idea/practice

$vendors = $vendor->getVendors($pdo);

//test print out vendors
echo $vendors;
print_r($vendors);

//output to template
include 'output.html.php';

然后想法使用模板/输出html文件输出数据:

<!DOCTYPE html>
<html lang="en">
    <head>

    </head>

    <body>

    <?php foreach ($vendors as $vendor){

        //if my city is an id value, at what point do I call the City Class to look up   
        that city name? I should not be calling DB from my view. And is it OK to have  
        html code in my controller?

        $name = $vendor->getName();
        $address2 = $vendor->getAddress2();
        $city = $vendor->getCity();
        $zip = $vendor->getZip();
        $web = $vendor->getWeb();
        $phone = $vendor->getTel();


    ?>

    </body>

</html>

我的问题:

  1. 在我的供应商类中,我正在创建一个供应商对象数组 - 这是从DB返回我的供应商行的正确OOP方式吗?
  2. 如果我的国家和城市是ID,并且每个都有自己的类,则何时何地调用这些类并使用getCity($ id)和getCountry($ id)获取名称值而不是ID,并将它们纳入我的foreach声明中?
  3. 非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我不认为你应该制作单独的类来获取城市名称和国家名称,只需在数据库中创建一个视图,选择供应商的详细信息,用名称替换id并从中选择* ... 只是为了简单起见,将所有属性放入数组中是一个更好的选择 只需将返回的数组从sql保存到数组中......