0

I got an error while running my code, it says call to a member function getBallparkDetailsStartDate() on a non-object.

 if($projectStatusId == ProjectStatusKeys::BALLPARK_ACTIVE) {
    $ballpark = $this->ballparkDetailsHandler->getBallparkDetailsByProjectId($projectId);
    $projectDetails["startdate"] = $ballpark->getBallparkDetailsStartDate();
    $projectDetails["enddate"] = $ballpark->getBallparkDetailsEndDate();
    $projectDetails["projectid"] = $projectId;
    $projectDetails["name"] = $ballpark->getBallparkDetailsBookingRef();
    $projectDetails["status"] = ProjectStatusKeys::BALLPARK_ACTIVE;          
 } 

I got the error in this line: $projectDetails["startdate"] = $ballpark->getBallparkDetailsStartDate();

Here is my other code:

<?php
class BallparkDetails {
private $ballparkDetailsId;
private $project;
private $ballparkDetailsBookingRef;
private $ballparkDetailsStartDate;
private $ballparkDetailsEndDate;
private $ballparkDetailsExpiryDate;
private $ballparkDetailsDescription;
private $ballparkDetailsNotes;

public function __construct($ballparkDetailsId, $project, 
        $ballparkDetailsBookingRef, 
        $ballparkDetailsStartDate, $ballparkDetailsEndDate, 
        $ballparkDetailsExpiryDate, $ballparkDetailsDescription, 
        $ballparkDetailsNotes) {
    $this->ballparkDetailsId = $ballparkDetailsId;
    $this->project = $project;
    $this->ballparkDetailsBookingRef = $ballparkDetailsBookingRef;
    $this->ballparkDetailsStartDate = $ballparkDetailsStartDate;
    $this->ballparkDetailsEndDate = $ballparkDetailsEndDate;
    $this->ballparkDetailsExpiryDate = $ballparkDetailsExpiryDate;
    $this->ballparkDetailsDescription = $ballparkDetailsDescription;
    $this->ballparkDetailsNotes = $ballparkDetailsNotes;
}

public function getBallparkDetailsId() {
    return $this->ballparkDetailsId;
}

public function getProject() {
    return $this->project;
}

public function getBankName() {
    return $this->getProject()->getBankName();
}

public function getBankRef() {
    return $this->getProject()->getBankRef();
}

public function getRegionName() {
    return $this->getProject()->getRegionName();
}

public function getProjectStatusName() {
    return $this->getProject()->getProjectStatusName();
}

public function getBallparkDetailsBookingRef() {
    return $this->ballparkDetailsBookingRef;
}

public function getBallparkDetailsStartDate() {
    return $this->ballparkDetailsStartDate;
}

public function getBallparkDetailsEndDate() {
    return $this->ballparkDetailsEndDate;
}

public function getBallparkDetailsExpiryDate() {
    return $this->ballparkDetailsExpiryDate;
}

public function getBallparkDetailsDescription() {
    return $this->ballparkDetailsDescription;
}

public function getBallparkDetailsNotes() {
    return $this->ballparkDetailsNotes;
}

public function getProjectId() {
    return $this->getProject()->getProjectId();
}

public function getProjectStatusId() {
    return $this->getProject()->getProjectStatusId();
}

}
?>

The last time I check this it ran well. But now I don't know what's wrong with this? Please help me find the error. Thanks.

Edited by pritaeas: Fixed code.

2
Contributors
13
Replies
15
Views
4 Years
Discussion Span
Last Post by bettybarnes
0

That means that this line:

$ballpark = $this->ballparkDetailsHandler->getBallparkDetailsByProjectId($projectId);

returns null to $ballpark, then on the next line it is not possible to call the function because there's no valid object.

0

For your reference this is the whole code of ViewWorkloadController:

<?php


class ViewWorkloadController extends Controller implements ControllerInterface {
private $weekHandler;
private $employeeHandler;
private $ballparkDetailsHandler;
private $projectDetailsHandler;
private $employeeHolidayHandler;

private $projectEmployeeWorkloadHandler;
private $projectEmployeeRoleHandler;
private $projectWeeklyWorkloadHandler;
private $projectHandler;

private $employeeId;
private $projectEmployeeRoles;
private $projectInfo;   # Row data
private $availability;  # Availability row
private $holidayLoad;   # Holiday (miscellanoues) row
private $finishedLoad;  # Finished load row

private $earliestDate;  # Earliest start date in all projects
private $latestDate;    # Earliest end date in all projects

private $startDate;     # Start date based on page input, store earliestDate if applicable
private $endDate;       # Start date based on page input, store latestDate if applicable

private $errors;

private $workloadWeekStartAndEndDate;

public function init() {
    $this->weekHandler = new WeekHandler($this->mysqli);
    $this->employeeHandler = new EmployeeHandler($this->mysqli);
    $this->ballparkDetailsHandler = new BallparkDetailsHandler($this->mysqli);
    $this->projectDetailsHandler = new projectDetailsHandler($this->mysqli);
    $this->projectEmployeeRoleHandler = new ProjectEmployeeRoleHandler($this->mysqli);
    $this->projectWeeklyWorkloadHandler = new ProjectWeeklyWorkloadHandler($this->mysqli);
    $this->employeeHolidayHandler = new EmployeeHoliday($this->mysqli);
    $this->projectHandler = new ProjectHandler($this->mysqli);
}

public function setEmployeeId($employeeId) {
    $this->employeeId = $employeeId;
}

public function getErrors() {
    return $this->errors;
}

private function setError($message) {
    $this->errors[] = $message; 
}

public function getProjectManager() {
    return $this->projectManager;
}

public function getEmployeeIdByEmployeeName($employeeName) {
    return $this->employeeHandler->getEmployeeIdByEmployeeName($employeeName);
}


public function processInput() {
    if(isset($_POST["refresh"]) || 
            (isset($_POST["startdate"]) && isset($_POST["enddate"])) ||
            (isset($_GET["startdate"]) && isset($_GET["enddate"]))) {
        $this->validateDateInput();
    }
    $projectStatusIds[] = ProjectStatusKeys::BALLPARK_ACTIVE;
    $projectStatusIds[] = ProjectStatusKeys::PROJECT_ACTIVE;
    $employeeId = $this->employeeId;

    $projectEmployeeRoles = $this->projectEmployeeRoleHandler->
        getProjectEmployeeRolesByEmployeeIdAndProjectStatusId($employeeId, $projectStatusIds);

    $this->projectEmployeeRoles = $projectEmployeeRoles;        
    $this->processProjectEmployeeRoles();

    # If  active project present
    if($this->projectInfo) {
        $this->processEarliestDate();
        $this->processLatestDate();
    }
    $this->processWeekNumbers();
    $this->processProjectEmployeeWorkload();
    $this->processFinishedWorkload();
    $this->processHolidayLoad();
    $this->processAvailability();
}

private function validateDateInput() {      
    if((isset($_GET["startdate"]) && isset($_GET["enddate"]))) {
        $startDate = $_GET["startdate"];
        $endDate = $_GET["enddate"];    
    }
    else {
        $startDate = $_POST["startdate"];
        $endDate = $_POST["enddate"];
    }

    if(strlen($startDate) < 1 && strlen($endDate) < 1 ) {
        $startDate = date('Y/m/d');
        $endDate = date('Y/m/d');
    }
    if(strlen($startDate) < 1 && !(strlen($endDate) < 1)) {
        $startDate = $endDate;
    }
    else if (strlen($startDate < 1)) {
        $startDate = date('Y/m/d');
    }

    $startDateS = strtotime($startDate);
    $endDateS = strtotime($endDate);
    $dateDiff = $endDateS - $startDateS;
    $fullDays = floor($dateDiff/(60*60*24));

    if($startDateS > $endDateS) {
        $this->setError("Start date must be earlier than end date");
    }
    else if ($fullDays > 365) {
        $this->setError("Total days for comparison must not exceed one year.");
    }

    $this->startDate = $startDate;
    $this->endDate = $endDate;
}

private function processProjectEmployeeRoles() {
    $projectEmployeeRoles = $this->projectEmployeeRoles;
    $projectInfo = array();

    if(!$projectEmployeeRoles) {
        $projectDetails["startdate"] = date('Y-m-d');
        $projectDetails["enddate"] = date('Y-m-d');
        $projectDetails["projectid"] = null;
        $projectDetails["name"] = null;
        $projectDetails["status"] = null;
        $projectInfo[] = $projectDetails;
        $this->projectInfo = $projectInfo;
        return;     
    }

    foreach($projectEmployeeRoles as $projectEmployeeRole) {
        $projectStatusId = $projectEmployeeRole->getProjectStatusId();
        $projectId = $projectEmployeeRole->getProjectId();
        $projectEmployeeRoleId = $projectEmployeeRole->getProjectEmployeeRoleId();
        $projectDetails = array();

        $projectDetails["employeeroleid"] = $projectEmployeeRoleId;
        $projectDetails["role"] = $projectEmployeeRole->getRoleName();

        # Get the project manager from database
        $projectManager = $this->projectHandler->getProjectManagerEmployeeByProjectId($projectId);
        $projectDetails["managername"] = $projectManager->getEmployeeName();
        $projectDetails["managerid"] = $projectManager->getEmployeeId();

        if($projectStatusId == ProjectStatusKeys::BALLPARK_ACTIVE) {
            //var_dump($ballpark);
            $ballpark = $this->ballparkDetailsHandler->getBallparkDetailsByProjectId($projectId);               
            //$projectDetails["startdate"] = $ballpark->getBallparkDetailsStartDate(); 
            //$projectDetails["enddate"] = $ballpark->getBallparkDetailsEndDate();
            $projectDetails["projectid"] = $projectId;
            //$projectDetails["name"] = $ballpark->getBallparkDetailsBookingRef();
            $projectDetails["status"] = ProjectStatusKeys::BALLPARK_ACTIVE; 
        }
        else if($projectStatusId == ProjectStatusKeys::PROJECT_ACTIVE) {
            $project = $this->projectDetailsHandler->getProjectDetailsByProjectId($projectId);
            $projectDetails["startdate"] = $project->getProjectDetailsStartDate(); 
            $projectDetails["enddate"] = $project->getProjectDetailsEndDate();
            $projectDetails["projectid"] = $projectId;
            $projectDetails["name"] = $project->getProjectDetailsName();
            $projectDetails["status"] = ProjectStatusKeys::PROJECT_ACTIVE;
        }
        $projectInfo[] = $projectDetails;
    }       
    $this->projectInfo = $projectInfo;
}

private function processEarliestDate() {
    $projectInfo = $this->projectInfo;
    $startDate = null;
    foreach($projectInfo as $info) {
        $infoDate = $info["startdate"];
        if(!isset($startDate)) {
            $startDate = $infoDate;
            continue;
        }           
        if(strtotime($startDate) > strtotime($infoDate)) {
            $startDate = $infoDate;
        }
    }
    $this->earliestDate = $startDate;
}

private function processLatestDate() {
    $projectInfo = $this->projectInfo;
    $endDate = null;
    foreach($projectInfo as $info) {
        $infoDate = $info["enddate"];
        if(!isset($endDate)) {
            $endDate = $infoDate;
            continue;
        }           
        if(strtotime($endDate) < strtotime($infoDate)) {
            $endDate = $infoDate;
        }
    }
    $this->latestDate = $endDate;
}

private function processWeekNumbers() {
    if(isset($_POST["refresh"]) || isset($_POST["ajax"]) || isset($_POST["hasrefresh"]) ||
        (isset($_GET["startdate"]) && isset($_GET["enddate"]) && !$this->errors)) {
        $delimiter = "/";   # Input date format is yyyy/mm/dd
        $startDate = $this->startDate;
        $endDate = $this->endDate;
    }
    else {
        $delimiter = "-";   # SQL date format is yyyy-mm-dd
        $startDate = $this->earliestDate;
        $endDate = $this->latestDate;
        # If earliest and latest end date exceeds one year, force a shorter span
        if($this->isGreaterThanAYear($startDate, $endDate)) {
            $endDate = date('Y/m/d', strtotime($startDate ." + 11 months"));
        }

        # Store information
        $this->startDate = $startDate;
        $this->endDate = $endDate;
    }

    $projectWorkloadWeekNumbers = 
        WeekNumberUtility::computeAndStoreWeekNumbers($startDate, $endDate, $delimiter);

    $workloadWeekStartAndEndDate = array();
    foreach($projectWorkloadWeekNumbers as $weekNumbers) {
        $weekYearArray = explode("-", $weekNumbers);
        $weekNumber = $weekYearArray[0];

        $year = $weekYearArray[1];
        $startDate = WeekNumberUtility::convertWeekNumberToMonday($weekNumber, $year, 'Y/m/d');
        $endDate = WeekNumberUtility::getFridayFromMondayDate($startDate, 'Y/m/d');             

        $startAndEndDate["weeknumber"] = $weekNumber;
        $startAndEndDate["year"] = $year;
        $startAndEndDate["startdate"] = $startDate;
        $startAndEndDate["enddate"] = $endDate;
        $workloadWeekStartAndEndDate[] = $startAndEndDate;
    }

    # Store Workload week numbers and dates
    $this->workloadWeekStartAndEndDate = $workloadWeekStartAndEndDate;          
}

/**
 * Gets the week start and end date, which is generated at method generateData()
 * @return <b>mixed[]</b> - Array where: <br/>
 *      - array[]["weeknumber"] <br/>         
 *      - array[]["year"] <br/>
 *      - array[]["startdate"] <br/>
 *      - array[]["enddate"] <br/>
 */ 
public function getWorkloadWeekStartAndEndDate() {
    return $this->workloadWeekStartAndEndDate;
}

/**
 * Processes the data for individual project workloads 
 */
private function processProjectEmployeeWorkload() {
    $weekStartAndEnd = $this->workloadWeekStartAndEndDate;
    $projectInfo = $this->projectInfo;

    $maxProjects = count($projectInfo);     
    for($i = 0; $i < $maxProjects; $i++) {
        $workload = array();
        $isProject = array();
        $projectEmployeeRoleId = $projectInfo[$i]["employeeroleid"];

        $projectWeeklyWorkloads = $this->projectWeeklyWorkloadHandler->
            getProjectWeeklyWorkloadByProjectEmployeeRoleId($projectEmployeeRoleId);
        # Loop for each week in the total duration of the timeline 
        foreach($weekStartAndEnd as $weekDates) {
            /**
             * @todo Currently loops through the entire project_weekly_workload to compare.
             * Very unoptimized.
             */ 
            $load = 0;
            $isProj = false;
            foreach($projectWeeklyWorkloads as $projectWeeklyWorkload) {
                if($projectWeeklyWorkload->getWeekNumber() == $weekDates["weeknumber"]
                        && $projectWeeklyWorkload->getWeekYear() == $weekDates["year"]) {
                    $load += $projectWeeklyWorkload->getProjectWeeklyWorkloadLoad();
                    $isProj = true;
                    break;
                }
            }
            $isProject[] = $isProj;
            $workload[] = $load;
        }
        # Appends "Workload" array to project
        $projectInfo[$i]["isproject"] = $isProject;
        $projectInfo[$i]["workload"] = $workload;
    }
    $this->projectInfo = $projectInfo;
    /*
    foreach($projectInfo as $x) {
        echo "<hr>";
        echo $x["name"] . "-";
        echo $x["role"] . "-";
        echo $x["projectid"] . "-";
        echo count($x["workload"]) . "-";
    }
    */
}

private function processFinishedWorkload () {
    $finishedLoad = array();
    $projectWeeklyWorkloadHandler = $this->projectWeeklyWorkloadHandler;
    $employeeId = $this->employeeId;
    $weekStartAndEnd = $this->workloadWeekStartAndEndDate;
    $projectStatusIds[] = ProjectStatusKeys::BALLPARK_APPROVED;
    $projectStatusIds[] = ProjectStatusKeys::BALLPARK_COMPLETED;
    $projectStatusIds[] = ProjectStatusKeys::BALLPARK_EXPIRED;
    $projectStatusIds[] = ProjectStatusKeys::BALLPARK_CANCELLED;
    $projectStatusIds[] = ProjectStatusKeys::PROJECT_CANCELLED;
    $projectStatusIds[] = ProjectStatusKeys::PROJECT_COMPLETED;

    $startWeekNumber =  $weekStartAndEnd[0]["weeknumber"];
    $startYear = $weekStartAndEnd[0]["year"];
    $max = count($weekStartAndEnd);
    $endWeekNumber = $weekStartAndEnd[$max-1]["weeknumber"];
    $endYear = $weekStartAndEnd[$max-1]["year"];

    $weeklyWorkloads = $projectWeeklyWorkloadHandler->
        getProjectWeeklyWorkloadByEmployeeIdAndWeekNumberAndYearRangeAndProjectStatusIds(
            $employeeId, $startWeekNumber, $startYear, $endWeekNumber, $endYear, $projectStatusIds);

    /** 
     * Currently unoptimized, but meh
     */     
    foreach($weekStartAndEnd as $weekDate) {
        $origWeekNum = $weekDate["weeknumber"];
        $origWeekYear = $weekDate["year"];
        $totalLoad = 0;
        if($weeklyWorkloads) {
            foreach($weeklyWorkloads as $workload) {
                $weekYear = $workload->getWeekYear();
                $weekNumber = $workload->getWeekNumber();
                $load = $workload->getProjectWeeklyWorkloadLoad();
                if($origWeekNum == $weekNumber && $origWeekYear == $weekYear) {
                    $totalLoad += $load;
                }
            }
        }
        $totalLoad = round($totalLoad, 2);
        $finishedLoad[] = $totalLoad;
    }
    $this->finishedLoad = $finishedLoad;
}

private function processAvailability() {
    $availability = array();
    $projectInfo = $this->projectInfo;
    $finishedLoad = $this->finishedLoad;
    $holidayLoad = $this->holidayLoad;

    $maxRow = count($projectInfo);
    $maxColumn = count($projectInfo[0]["workload"]);

    for($i = 0; $i < $maxColumn; $i++) {
        $load = 5;
        for($j = 0; $j < $maxRow; $j++) {
            $load -= $projectInfo[$j]["workload"][$i];
        }
        $load -= $finishedLoad[$i];
        $load -= $holidayLoad[$i];
        $availability[] = round($load,2);
    }
    $this->availability = $availability;        
}

private function processHolidayLoad() {
    $holidayLoad = array();
    $weekStartAndEnd = $this->workloadWeekStartAndEndDate;
    $employeeId = $this->employeeId;
    foreach($weekStartAndEnd as $weekDates) {
        $load = 0;
        $isProj = false;
        $startDate = WeekNumberUtility::convertWeekNumberToMonday(
            $weekDates["weeknumber"], $weekDates["year"], 'Y/m/d');
        $endDate = WeekNumberUtility::getFridayFromMondayDate($startDate, 'Y/m/d');
        $load += $this->employeeHolidayHandler->getHolidayLoad($employeeId, $startDate, $endDate);
        $holidayLoad[] = $load;
    }

    $this->holidayLoad = $holidayLoad;
}

private function isGreaterThanAYear($startDate, $endDate) {
    $startDateS = strtotime($startDate);
    $endDateS = strtotime($endDate);
    $dateDiff = $endDateS - $startDateS;
    $fullDays = floor($dateDiff/(60*60*24));

    if(fullDays > 365)
        return true;
    return false;
}

public function getProjectInfo() {
    return $this->projectInfo;
}

public function getEarliestDate() {
    return $this->earliestDate;
}

public function getLatestDate() {
    return $this->latestDate;
}

public function getStartDate(){
    return $this->startDate;
}

public function getEndDate() {
    return $this->endDate;
}

public function getAvailability() {
    return $this->availability;
}

public function getFinishedLoad() {
    return $this->finishedLoad;
}
public function getHolidayLoad() {
    return $this->holidayLoad;
}

public function getEmployee() {
    $employeeId = $this->employeeId;
    return $this->employeeHandler->getEmployeeByEmployeeId($employeeId);
}
}
?>
0

Check this class: BallparkDetailsHandler. One option is that the mysqli is not connected correctly (a guess). Show this function: getBallparkDetailsByProjectId

0

This one:

<?php

class BallparkDetailsHandler extends Handler implements HandlerInterface {

public function init(){}

/**
 * Gets booking_ref, bank_ref, region_name, status_name, description, notes, dates 
 * @param unknown_type $projectId
 */
public function getBallparkDetailsByProjectId($projectId) {
    $stmt = $this->stmt;
    $query = 
            "SELECT ballpark_details.ballpark_details_booking_ref, 
                bank.bank_ref, 
                region.region_name, 
                project.project_status_id,
                project_status.project_status_name, 
                ballpark_details.ballpark_details_description, 
                ballpark_details.ballpark_details_notes,
                ballpark_details.ballpark_details_start_date, 
                ballpark_details.ballpark_details_end_date,
                ballpark_details.ballpark_details_expiry_date
            FROM `ballpark_details` 
            INNER JOIN `project` 
                ON project.project_id = ballpark_details.project_id
            INNER JOIN `bank` 
                ON project.bank_id = bank.bank_id
            INNER JOIN `region` 
                ON project.region_id = region.region_id
            INNER JOIN `project_status` 
                ON project_status.project_status_id = project.project_status_id
            WHERE ballpark_details.project_id = ?
            ORDER BY ballpark_details.ballpark_details_booking_ref";
    if($stmt = $this->mysqli->prepare($query)){
        $stmt->bind_param("i", $projectId);
        $stmt->execute();
        $stmt->store_result();
        $stmt->bind_result($ballparkDetailsBookingRef, $bankRef, $regionName, $projectStatusId, 
            $projectStatusName, $ballparkDetailsDescription, $ballparkDetailsNotes, 
            $ballparkDetailsStartDate, $ballparkDetailsEndDate, $ballparkDetailsExpiryDate);
        $stmt->fetch();

        if(!$stmt->num_rows) {
            return null;
        }
        $bank = new Bank("", "", $bankRef, "");
        $region = new Region("", $regionName, "");
        $projectStatus = new ProjectStatus($projectStatusId, $projectStatusName);
        $project = new Project($projectId, $bank, $region, $projectStatus);
        return new BallparkDetails("", $project, 
            $ballparkDetailsBookingRef, $ballparkDetailsStartDate, 
            $ballparkDetailsEndDate, $ballparkDetailsExpiryDate, 
            $ballparkDetailsDescription, $ballparkDetailsNotes);            
    }
    return null;
}

public function addBallparkDetails($projectId, $ballparkDetailsBookingRef, 
        $ballparkDetailsStartDate, $ballparkDetailsEndDate, 
        $ballparkDetailsExpiryDate, $ballparkDetailsDescription, 
        $ballparkDetailsNotes) {
    $stmt = $this->stmt;
    $query = 
        "INSERT INTO `ballpark_details` 
            (`project_id`, 
            `ballpark_details_booking_ref`, 
            `ballpark_details_start_date`, 
            `ballpark_details_end_date`,
            `ballpark_details_expiry_date`, 
            `ballpark_details_description`,
            `ballpark_details_notes`) 
        VALUES (?, ?, ?, ?, ?, ?, ?);";
    if($stmt = $this->mysqli->prepare($query)) {
        $stmt->bind_param("issssss", $projectId, $ballparkDetailsBookingRef, 
            $ballparkDetailsStartDate, $ballparkDetailsEndDate,
            $ballparkDetailsExpiryDate, $ballparkDetailsDescription,
            $ballparkDetailsNotes);
        $stmt->execute();
        return Status::INSERT_SUCCESS;
    }       
    return Status::INSERT_FAIL;
}

public function updateBallparkDetails($ballparkDetailsId, $projectId, 
        $ballparkDetailsBookingRef, $ballparkDetailsStartDate, 
        $ballparkDetailsEndDate, $ballparkDetailsExpiryDate,
        $ballparkDetailsDescription, $ballparkDetailsNotes) {
    $stmt = $this->stmt;
    $query =
        "UPDATE `ballpark_details` 
        SET `project_id` = ?,
            `ballpark_details_booking_ref` = ?,
            `ballpark_details_start_date` = ?,
            `ballpark_details_end_date` = ?,
            `ballpark_details_expiry_date` = ?,
            `ballpark_details_description` = ?,
            `ballpark_details_notes` = ? 
        WHERE `ballpark_details_id` = ?;";
    if($stmt = $this->mysqli->prepare($query)) {
        $stmt->bind_param("issssssi", $projectId, $ballparkDetailsBookingRef, 
            $ballparkDetailsStartDate, $ballparkDetailsEndDate,
            $ballparkDetailsExpiryDate, $ballparkDetailsDescription,
            $ballparkDetailsNotes, $ballparkDetailsId);
        $stmt->execute();
        return $ballparkDetailsId;
    }
    return null;
}

public function updateBallparkDetailsByProjectId($projectId, 
        $ballparkDetailsBookingRef, $ballparkDetailsStartDate, 
        $ballparkDetailsEndDate, $ballparkDetailsExpiryDate,
        $ballparkDetailsDescription, $ballparkDetailsNotes) {
    $stmt = $this->stmt;
    $query =
        "UPDATE `ballpark_details` 
        SET `ballpark_details_booking_ref` = ?,
            `ballpark_details_start_date` = ?,
            `ballpark_details_end_date` = ?,
            `ballpark_details_expiry_date` = ?,
            `ballpark_details_description` = ?,
            `ballpark_details_notes` = ? 
        WHERE `project_id` = ?;";
    if($stmt = $this->mysqli->prepare($query)) {
        $stmt->bind_param("ssssssi", $ballparkDetailsBookingRef, 
            $ballparkDetailsStartDate, $ballparkDetailsEndDate,
            $ballparkDetailsExpiryDate, $ballparkDetailsDescription,
            $ballparkDetailsNotes, $projectId);
        $stmt->execute();
        return $projectId;
    }
    return null;
}

/**
 * Gets the ballpark_details primary key using the unique Booking reference supplied
 * @param String $ballparkDetailsBookingRef - The unique booking ref
 * @return <b>int</b> - ballpark_details_id
 */
public function getBallParkDetailsIdByBallparkDetailsBookingRef($ballparkDetailsBookingRef) {
    $stmt = $this->stmt;
    $query = 
        "SELECT `ballpark_details_id` 
        FROM `ballpark_details` 
        WHERE `ballpark_details_booking_ref` = ?";
    if($stmt = $this->mysqli->prepare($query)) {
        $stmt->bind_param("s", $ballparkDetailsBookingRef);
        $stmt->execute();
        $stmt->store_result();
        $stmt->bind_result($ballparkDetailsId);
        $stmt->fetch();
        if(!$stmt->num_rows)
            return null;
        return $ballparkDetailsId;
    }
    return null;
}

/**
 * Gets the Project ID and Ballpark Details' Booking Ref using Project Status ID, between two dates
 * @param date $startDate
 * @param date $endDate
 * @param int $projectStatusId project.project_status_id 
 */
public function getProjectIdAndBallparkDetailsBookingRefByProjectStatusIdAndBetweenDates(
        $projectStatusId, $startDate, $endDate) {
    $stmt = $this->stmt;
    $query = 
        "SELECT project.project_id,
            ballpark_details.ballpark_details_booking_ref
        FROM `ballpark_details`
        INNER JOIN `project`
            ON project.project_id = ballpark_details.project_id
        WHERE project.project_status_id = ?
            AND ballpark_details.ballpark_details_start_date <= ? 
            AND ballpark_details.ballpark_details_end_date >= ?";

    #echo "=========================<br/>";
    #echo $startDate .'<br/>';
    #echo $endDate. '<br/>';
    #echo "=========================<br/>";
    if($stmt = $this->mysqli->prepare($query)) {
        # Start1 <= End2 AND Start2 <= End1
        $stmt->bind_param("iss", $projectStatusId, $endDate, $startDate);
        $stmt->execute();
        $stmt->store_result();
        $stmt->bind_result($projectId, $ballparkDetailsBookingRef);
        $ballparkDetails = array();
        while($stmt->fetch()) {
        #   echo 'projectid-'.$projectId.'<br/>';
            #echo $ballparkDetailsBookingRef . '<br/>';
            $projectStatus = new ProjectStatus($projectStatusId, null);
            $project = new Project($projectId, null, null, $projectStatus);
            $ballparkDetails[] = new BallparkDetails(
                null, $project, $ballparkDetailsBookingRef, 
                null, null, null, null, null);
        }               
        return $ballparkDetails;
    }
    #echo $query;
    return null;    
}

public function searchBallparkDetailsByEmployeeIdAndBookingRefAndProjectStatusIdAndBetweenDates(
        $employeeId, $bookingRef, $projectStatusId, $startDate, $endDate) {
    $stmt = $this->stmt;
    $query = 
        "SELECT DISTINCT
                project.project_id,
                ballpark_details.ballpark_details_booking_ref, 
                bank.bank_ref, 
                region.region_name, 
                project.project_status_id,
                project_status.project_status_name, 
                ballpark_details.ballpark_details_description, 
                ballpark_details.ballpark_details_notes,
                ballpark_details.ballpark_details_start_date, 
                ballpark_details.ballpark_details_end_date,
                ballpark_details.ballpark_details_expiry_date
        FROM `ballpark_details` 
        INNER JOIN `project` 
            ON project.project_id = ballpark_details.project_id
        INNER JOIN `bank` 
            ON project.bank_id = bank.bank_id
        INNER JOIN `region` 
            ON project.region_id = region.region_id
        INNER JOIN `project_status` 
            ON project_status.project_status_id = project.project_status_id
        INNER JOIN `project_employee_role`
            ON project_employee_role.project_id = project.project_id 
        WHERE project.project_status_id = ?
            AND ballpark_details.ballpark_details_start_date <= ? 
            AND ballpark_details.ballpark_details_end_date >= ? 
            AND ballpark_details.ballpark_details_booking_ref LIKE CONCAT('%', ?, '%')
            AND project_employee_role.employee_id = ?";
    if($stmt = $this->mysqli->prepare($query)) {
        # Start1 <= End2 AND Start2 <= End1
        $stmt->bind_param("isssi", $projectStatusId, $endDate, $startDate, $bookingRef, $employeeId);
        $stmt->execute();
        $stmt->store_result();
        $stmt->bind_result($projectId, $ballparkDetailsBookingRef, $bankRef, $regionName, $projectStatusId,
            $projectStatusName, $ballparkDetailsDescription, $ballparkDetailsNotes, 
            $ballparkDetailsStartDate, $ballparkDetailsEndDate, $ballparkDetailsExpiryDate);
        $ballparkDetails = array();
        while($stmt->fetch()) {
            $bank = new Bank("", "", $bankRef, "");
            $region = new Region("", $regionName, "");
            $projectStatus = new ProjectStatus($projectStatusId, $projectStatusName);
            $project = new Project($projectId, $bank, $region, $projectStatus);
            $ballparkDetails[] =  new BallparkDetails("", $project, 
                $ballparkDetailsBookingRef, $ballparkDetailsStartDate, 
                $ballparkDetailsEndDate, $ballparkDetailsExpiryDate, 
                $ballparkDetailsDescription, $ballparkDetailsNotes);    
        }               
        return $ballparkDetails;
    }
    #echo $query;
    return null;    
}

public function searchBallparkDetailsByBookingRefAndProjectStatusIdAndBetweenDates(
        $bookingRef, $projectStatusId, $startDate, $endDate) {
    $stmt = $this->stmt;
    $query = 
        "SELECT ballpark_details.ballpark_details_booking_ref, 
                bank.bank_ref, 
                region.region_name, 
                project.project_status_id,
                project_status.project_status_name, 
                ballpark_details.ballpark_details_description, 
                ballpark_details.ballpark_details_notes,
                ballpark_details.ballpark_details_start_date, 
                ballpark_details.ballpark_details_end_date,
                ballpark_details.ballpark_details_expiry_date
        FROM `ballpark_details` 
        INNER JOIN `project` 
            ON project.project_id = ballpark_details.project_id
        INNER JOIN `bank` 
            ON project.bank_id = bank.bank_id
        INNER JOIN `region` 
            ON project.region_id = region.region_id
        INNER JOIN `project_status` 
            ON project_status.project_status_id = project.project_status_id
        WHERE project.project_status_id = ?
            AND ballpark_details.ballpark_details_start_date <= ? 
            AND ballpark_details.ballpark_details_end_date >= ? 
            AND ballpark_details.ballpark_details_booking_ref LIKE CONCAT('%', ?, '%')";
    if($stmt = $this->mysqli->prepare($query)) {
        # Start1 <= End2 AND Start2 <= End1
        $stmt->bind_param("isss", $projectStatusId, $endDate, $startDate, $bookingRef);
        $stmt->execute();
        $stmt->store_result();
        $stmt->bind_result($ballparkDetailsBookingRef, $bankRef, $regionName, $projectStatusId,
            $projectStatusName, $ballparkDetailsDescription, $ballparkDetailsNotes, 
            $ballparkDetailsStartDate, $ballparkDetailsEndDate, $ballparkDetailsExpiryDate);
        $ballparkDetails = array();
        while($stmt->fetch()) {
            $bank = new Bank("", "", $bankRef, "");
            $region = new Region("", $regionName, "");
            $projectStatus = new ProjectStatus($projectStatusId, $projectStatusName);
            $project = new Project($projectId, $bank, $region, $projectStatus);
            $ballparkDetails[] =  new BallparkDetails("", $project, 
                $ballparkDetailsBookingRef, $ballparkDetailsStartDate, 
                $ballparkDetailsEndDate, $ballparkDetailsExpiryDate, 
                $ballparkDetailsDescription, $ballparkDetailsNotes);    
        }               
        return $ballparkDetails;
    }
    #echo $query;
    return null;    
}

/**
 * A general ballpark search method which dynamically generates query based on parameters.
 * Start date and end date are required parameters.
 * @param String $startDate - Required
 * @param String $endDate - Required
 * @param int $employeeId
 * @param String $bookingRef
 * @param int[] $regionIds
 * @param String $bankRef
 * @param int[] $projectStatusIds
 * @param boolean $hasUnassigned - Has a null employee
 */
public function generalBallparkDetailsSearch($startDate, $endDate, $employeeId, 
        $bookingRef, $regionIds, $bankRef,
        $projectStatusIds, $hasUnassigned = false) {
    # Number of conditions based on parameter; Meh parameter
    $numConditions = 0;

    $params = array(null);
    $bind_param = '';

    $stmt = $this->stmt;
    $sql = "SELECT DISTINCT 
                ballpark_details.ballpark_details_booking_ref, 
                bank.bank_ref, 
                region.region_name, 
                project.project_status_id,
                project.project_id,
                project_status.project_status_name, 
                ballpark_details.ballpark_details_description, 
                ballpark_details.ballpark_details_notes,
                ballpark_details.ballpark_details_start_date, 
                ballpark_details.ballpark_details_end_date,
                ballpark_details.ballpark_details_expiry_date
            FROM `ballpark_details` 
                INNER JOIN `project` 
                    ON project.project_id = ballpark_details.project_id
                INNER JOIN `bank` 
                    ON project.bank_id = bank.bank_id
                INNER JOIN `region` 
                    ON project.region_id = region.region_id
                INNER JOIN `project_status` 
                    ON project_status.project_status_id = project.project_status_id
                INNER JOIN `project_employee_role`
                    ON project_employee_role.project_id = project.project_id ";

    $sql .= "WHERE ";

    if(!is_null($employeeId)) {
        $numConditions++;
        $sql .= 'project_employee_role.employee_id   = ? AND ';
        $bind_param .= 'i';
        $params[] = $employeeId;
    }

    if(!is_null($bookingRef)) {
        $numConditions++;
        $sql .= "ballpark_details.ballpark_details_booking_ref LIKE CONCAT('%', ?, '%') AND ";
        $bind_param .= 's';
        $params[] = $bookingRef;
    }

    if(!is_null($regionIds)) {
        $maxRegionIds = count($regionIds);
        $sql .= "(";    
        #If RegionIds is a single key
        if(is_numeric($regionIds)) {
            $numConditions++;
            $sql .= "region.region_id = ?) AND ";
            $params[] = $regionIds;
            $bind_param .= "i"; 
        }
        #If RegionIds is an array of keys
        else {          
            for($i = 0; $i < $maxRegionIds; $i++) {
                $numConditions++;
                $bind_param .= "i";                     # Add integer to bind_param
                $params[] = $regionIds[$i];     # Add parameter for execution
                $sql .= " region.region_id = ? ";
                if($i != $maxRegionIds-1)
                    $sql .= " OR ";
            }
            $sql .= ") AND ";
        }       
    }

    if(!is_null($bankRef)) {
        $numConditions++;
        $sql .= "bank.bank_ref LIKE CONCAT('%', ?, '%') AND ";
        $bind_param .= 's';
        $params[] = $bankRef;
    }

    if(!is_null($projectStatusIds)) {
        $maxProjectStatus = count($projectStatusIds);
        $sql .= "(";        
        #If ProjectStatusIds is a single key
        if(is_numeric($projectStatusIds)) {
            $numConditions++;
            $sql .= "project.project_status_id = ?) AND ";
            $params[] = $projectStatusIds;
            $bind_param .= "i"; 
        }
        #If ProjectStatusIds is an array of keys
        else {          
            for($i = 0; $i < $maxProjectStatus; $i++) {
                $numConditions++;
                $bind_param .= "i";                     # Add integer to bind_param
                $params[] = $projectStatusIds[$i];      # Add parameter for execution
                $sql .= " project.project_status_id = ? ";
                if($i != $maxProjectStatus-1)
                    $sql .= " OR ";
            }
            $sql .= ") AND ";
        }
    }

    # Start date and end date
    $sql .= "ballpark_details.ballpark_details_start_date <= ? 
            AND ballpark_details.ballpark_details_end_date >= ? ";
    $bind_param .= "ss";
    $params[] = $endDate;
    $params[] = $startDate;

    if($hasUnassigned) {
        $sql .= ' AND (SELECT COUNT(*) FROM project_employee_role 
                    WHERE project_employee_role.project_id = project.project_id AND
                    project_employee_role.employee_id = ?) > 0';
        $bind_param .= 'i';
        $params[] = EmployeeKeys::NULL_EMPLOYEE;
    }

    #echo "<hr>".$sql;
    #echo "<br/><br/>Parameters:";
    #foreach($params as $param) {
    #   echo "<br/>" . $param;
    #}
    #echo "<br/>Bind_Param: ".$bind_param;
    #echo "<hr>";

    $params[0] = $bind_param;       
    $results = $this->execSQL($sql, $params, false);    

    if($results) {
        $ballparkDetails = array();
        foreach($results as $result) {
            $bank = new Bank("", "", $result["bank_ref"], "");
            $region = new Region("", $result["region_name"], "");
            $projectStatus = new ProjectStatus(null, $result["project_status_name"]);
            $project = new Project($result["project_id"], $bank, $region, $projectStatus);
            $ballparkDetails[] =  new BallparkDetails("", $project, 
                $result["ballpark_details_booking_ref"], $result["ballpark_details_start_date"], 
                $result["ballpark_details_end_date"], $result["ballpark_details_expiry_date"], 
                $result["ballpark_details_description"], $result["ballpark_details_notes"]);    
        }
        return $ballparkDetails;
    }
    return null;
}
}

?>
0

I think it's not that there's an error in the connection. I don't know what happened. Last week it was working fine but now when I checked it it's giving me this error.

0

Line 44-46:

if (!$stmt->num_rows) {
    return null;
}

If the project id you're trying to retrieve does not exist in the database, null is returned. So your code needs to do a null check on the retrieved $ballpark, before using it's functions. It may not exist.

0

How do I check null on the retrieved $ballpark? Sorry I know nothing about PHP. Please help me solve this problem. thanks

0

Know nothing about PHP, and then changing such code... pretty strange.

Anyway:

 if ($projectStatusId == ProjectStatusKeys::BALLPARK_ACTIVE) {
    $ballpark = $this->ballparkDetailsHandler->getBallparkDetailsByProjectId($projectId);
    if ($ballpark == null) {
        // here should be error handling to indicate that the project was not found
    }
    else {
        $projectDetails["startdate"] = $ballpark->getBallparkDetailsStartDate();
        $projectDetails["enddate"] = $ballpark->getBallparkDetailsEndDate();
        $projectDetails["projectid"] = $projectId;
        $projectDetails["name"] = $ballpark->getBallparkDetailsBookingRef();
        $projectDetails["status"] = ProjectStatusKeys::BALLPARK_ACTIVE;          
    }
 } 
0

It gives me this error message: Fatal error: Error: bad project ID: '17'. What is the meaning of this?

0

Like I said before, id 17 is not in the database, or there is something wrong with the query (which I doubt).

0

Thanks priteas! My problem is solved by your help in giving me tips on how to handle errors.! this is solved. What I did is delete the projectIds that don't belong to ballparks. Thank you!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.