如何从动态数据库记录集创建新的php对象

时间:2013-07-02 17:16:48

标签: php oop

我正在开发一个php job calendar项目。这就是我要做的事情:查询数据库并获取预定信息(我得到的那部分),获取该信息并根据每个记录集创建一个作业对象,然后我需要按部门排序对象并开始日期/ time然后根据部门和时间在动态表格中显示它们。我已经走到了这一步:

<?php 
session_start();
include_once("functions.php");
class jobs
{
    //a job has a:
    private $eachJob;
    private $group;
    private $jID;
    private $num;
    private $jStatus;
    private $description;
    private $startDate;
    private $endDate;
    private $startTime;
    private $endTime;
    private $rSpan;
    private $department;


    public function __construct()
    {
        $this->buildJob($_SESSION['dept']);
    }

    public function buildJob($depatment)
    {
        $captionHeading = 'Traditional Print';
        $conn = connect();

        $tsql = "select + 'Job# ' + CAST (JobNum AS VARCHAR(10)) as JobNum, Description, datediff(MI,StartTime, EndTime) / 15 as 'RowSpan', AssetName, 
        AssetID, Status.ColorCode as tdCC, JobID, StartTime, EndTime, datediff(day,getDate(),StartTime)*24*60/15 as 'Blank', getdate() as now
        from Calendar_View, Departments, Status, Assets
        where Departments.DepartmentName = '$depatment' and Calendar_View.Department = Departments.DepartmentID and AssetStatus = Status.StatusID and 
        Calendar_View.Asset = Assets.AssetID
        order by AssetID asc";

        $stmt = sqlsrv_query($conn, $tsql);
        if ($stmt)
        {
            while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) 
            {
            /*******************************
            $this->jobDetails = array(
                $this->group    = $row{'AssetName'},
                $this->jID          = $row['JobID'],
                $this->num          = $row['JobNum'],
                $this->jStatus      = $row['tdCC'],
                $this->description  = "now =" . date_format($row['now'],'Y-m-d'),
                $this->startDate    = date_format($row['StartTime'],'Y-m-d'),
                $this->endDate      = date_format($row['EndTime'],'Y-m-d'),
                $this->startTime    = date_format($row['StartTime'],'g:i'),
                $this->endTime      = date_format($row['EndTime'],'g:i'),
                $this->rspan        = "Time needed: " . $row['RowSpan']);
                $this->jobInfo($this->jobDetails);
            **********************************/
                $this->group        = $row{'AssetName'};
                $this->jID          = $row['JobID'];
                $this->num          = $row['JobNum'];
                $this->jStatus      = $row['tdCC'];
                $this->description  = "now =" . date_format($row['now'],'Y-m-d');
                $this->startDate    = date_format($row['StartTime'],'Y-m-d');
                $this->endDate      = date_format($row['EndTime'],'Y-m-d');
                $this->startTime    = date_format($row['StartTime'],'g:i');
                $this->endTime      = date_format($row['EndTime'],'g:i');
                $this->rspan        = "Time needed: " . $row['RowSpan'];
                //$this->jobInfo();
            }
        }
        else
        {
            die( print_r(sqlsrv_errors(), true));
        }
    }
    public function jobInfo()
    {

    }
}
/******************************
class job
{

}
******************************/
?>

我可以通过以下网址访问该作业并执行此操作:

include_once("../php/job_class.php");
$obj=new  job("Traditional Print");
echo "<pre>";
echo print_r($obj);
echo "</pre>";

这只给了我从sql调用创建的对象。我已经尝试将它放入一个数组并将其传递给jobinfo函数,但这给了我一个数组数组并且弄得一团糟。我怎样才能完成我想做的事情?

2 个答案:

答案 0 :(得分:1)

您的所有属性都是私有的。另外,对象名称是作业,您正在启动一个名为job的对象。另外,构造函数不会传入传入变量,而是尝试向其发送字符串。同样适用于jobInfo()它没有任何传入参数,你试图向它发送一堆值。

要执行您要执行的操作,您可以创建一个返回作业对象数组的静态函数。

答案 1 :(得分:1)

您的排序问题

最简单的排序方法是在SQL查询中执行此操作。因此可以将订单更改为ORDER BY DepartmentName,StartTime。

您的对象问题

您应该创建一个作业类,而不是拥有包含所有数据的作业类,而作业类有许多作业实例。也许是这样的:

class job
{
    //a job has a:
    private $eachJob;
    private $group;
    private $jID;
    private $num;
    private $jStatus;
    private $description;
    private $startDate;
    private $endDate;
    private $startTime;
    private $endTime;
    private $rSpan;
    private $department;

    public __construct($row) {
        $this->group        = $row{'AssetName'};
        $this->jID          = $row['JobID'];
        $this->num          = $row['JobNum'];
        $this->jStatus      = $row['tdCC'];
        $this->description  = "now =" . date_format($row['now'],'Y-m-d');
        $this->startDate    = date_format($row['StartTime'],'Y-m-d');
        $this->endDate      = date_format($row['EndTime'],'Y-m-d');
        $this->startTime    = date_format($row['StartTime'],'g:i');
        $this->endTime      = date_format($row['EndTime'],'g:i');
        $this->rspan        = "Time needed: " . $row['RowSpan'];
    }
}

class jobs
{
    private $jobs = array();


    public function __construct()
    {
        $this->buildJob($_SESSION['dept']);
    }

    public function buildJob($depatment)
    {
        $captionHeading = 'Traditional Print';
        $conn = connect();

        $tsql = "select + 'Job# ' + CAST (JobNum AS VARCHAR(10)) as JobNum, Description, datediff(MI,StartTime, EndTime) / 15 as 'RowSpan', AssetName, 
    AssetID, Status.ColorCode as tdCC, JobID, StartTime, EndTime, datediff(day,getDate(),StartTime)*24*60/15 as 'Blank', getdate() as now
    from Calendar_View, Departments, Status, Assets
    where Departments.DepartmentName = '$depatment' and Calendar_View.Department = Departments.DepartmentID and AssetStatus = Status.StatusID and 
    Calendar_View.Asset = Assets.AssetID
    order by AssetID asc";

        $stmt = sqlsrv_query($conn, $tsql);
        if ($stmt)
        {
            while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) 
            {

            $this->jobs[] = new job($row);
            }
        }
        else
        {
            die( print_r(sqlsrv_errors(), true));
        }
    }

    public function jobInfo()
    {
        $jobArray = array();
        echo "<pre>";
        echo print_r("$jobArray[1]");
        echo "</pre>";
    }
}