PHP/Zend Framework 2 - Unable to display table field values within dynamically generated table

349 views Asked by At

Problem

I am trying to display field values into a table using Zend Framework 2 and PHP/MySQL. Given the following table which is dynamically generated, I am trying to get the values shown below. Unfortunately, I only get empty values for the cell locations.

Question

How can I solve this issue to populate values into their cell locations?

enter image description here

Code

Controller.php

   public function summaryAction()
    {
        return new ViewModel(array(
            'actionitems' => $this->getActionItemTable()->fetchAll(),
        ));
    }

Table.php

    public function fetchAll()
    {
        $select = new Select();
        $select->from('actionitems', array('*'))
               ->join('users', 'actionitems.OwnerID = users.UserID', array('OwnerLastName' => new Expression('users.lastName'),  'OwnerFirstName' =>  new Expression('users.firstName')));   
        $resultSet = $this->tableGateway->selectWith($select);
        return $resultSet;
    }

index.phtml

        <table class="actionitemsummary">
                <tr>
                    <th>
                        Team
                    </th>
                    <th>
                        Item #
                    </th>
                    <th>
                        Action Item Title
                    </th>
                    <th>
                        Criticality
                    </th>
                    <th>
                        Status
                    </th>
                    <th>
                        Assigned Date
                    </th>
                    <th>
                        Original Due Date
                    </th>
                    <th>
                        ECD
                    </th>
                    <th>
                        Closed Date
                    </th>
                    <th>
                        Owner
                    </th>
                    <th>
                        Actions
                    </th>
                </tr>
                <?php 
                    use Application\Model\ActionItem;

                    /* @var $actionItem ActionItem */
                ?>

                <?php foreach($actionitems as $actionItem): ?>
                <tr>
                    <td class="team">
                        <?php echo $actionItem->team; ?>
                    </td>
                    <td class="itemnum">
                        <?php echo $actionItem->actionItemID; ?>
                    </td>
                    <td class="actionitemtitle">
                        <?php echo $actionItem->actionItemTitle; ?>
                    </td>
                    <td class="criticality">
                        <?php echo $actionItem->criticality; ?>
                    </td>
                    <td class="status <?php echo $actionItem->status; ?> onschedule">
                        <?php echo $actionItem->status; ?>
                    </td>
                    <td class="assigneddate">
                        <?php echo $actionItem->assignedDate; ?>
                    </td>
                    <td class="originalduedate">
                        <?php echo $actionItem->dueDate; ?>
                    </td>
                    <td class="ecd">
                        <?php echo $actionItem->ecd; ?>
                    </td>
                    <td class="closeddate">
                        <?php echo $actionItem->closedDate; ?>
                    </td>
                    <td class="owner">
                        <?php echo $actionItem->ownerID; ?>
                    </td>
                    <td class="actions">
                        <a href="">View</a>
                    </td>
                </tr>
                <?php endforeach; ?>
            </table>

EDIT:

var_dump($resultSet);

object(Zend\Db\ResultSet\ResultSet)[287]
  protected 'allowedReturnTypes' => 
    array (size=2)
      0 => string 'arrayobject' (length=11)
      1 => string 'array' (length=5)
  protected 'arrayObjectPrototype' => 
    object(Application\Model\ActionItem)[258]
      public 'actionItemID' => null
      public 'status' => null
      public 'actionItemTitle' => null
      public 'railName' => null
      public 'team' => null
      public 'criticality' => null
      public 'assignedDate' => null
      public 'ecd' => null
      public 'dueDate' => null
      public 'closedDate' => null
      public 'completionDate' => null
      public 'closureCriteria' => null
      public 'notes' => null
      public 'assignorID' => null
      public 'ownerID' => null
      public 'altOwnerID' => null
      public 'approverID' => null
      public 'rejectionJustification' => null
      public 'approvalStatement' => null
      public 'closureStatement' => null
      public 'actionItemStatement' => null
  protected 'returnType' => string 'arrayobject' (length=11)
  protected 'buffer' => 
    array (size=0)
      empty
  protected 'count' => int 15
  protected 'dataSource' => 
    object(Zend\Db\Adapter\Driver\Pdo\Result)[264]
      protected 'statementMode' => string 'forward' (length=7)
      protected 'fetchMode' => int 2
      protected 'resource' => 
        object(PDOStatement)[265]
          public 'queryString' => string 'SELECT `actionitems`.*, users.lastName AS `OwnerLastName`, users.firstName AS `OwnerFirstName` FROM `actionitems` INNER JOIN `users` ON `actionitems`.`OwnerID` = `users`.`UserID`' (length=178)
      protected 'options' => null
      protected 'currentComplete' => boolean true
      protected 'currentData' => 
        array (size=22)
          'ActionItemID' => string '1' (length=1)
          'Status' => null
          'Team' => null
          'Criticality' => string '1 - High' (length=8)
          'AssignedDate' => string '2015-06-11' (length=10)
          'DueDate' => string '2015-06-02' (length=10)
          'CompletionDate' => null
          'ECD' => string '2015-06-07' (length=10)
          'ClosedDate' => null
          'ActionItemTitle' => string 'test' (length=4)
          'ActionItemStatement' => string 'test' (length=4)
          'AssignorID' => string '1' (length=1)
          'OwnerID' => string '1' (length=1)
          'AltOwnerID' => string '1' (length=1)
          'ApproverID' => null
          'RejectionJustification' => null
          'ApprovalStatement' => null
          'ClosureCriteria' => string 'test' (length=4)
          'ClosureStatement' => null
          'Notes' => string 'test' (length=4)
          'OwnerLastName' => string 'TEST' (length=13)
          'OwnerFirstName' => string 'TEST' (length=4)
      protected 'position' => int 0
      protected 'generatedValue' => string '0' (length=1)
      protected 'rowCount' => int 15
  protected 'fieldCount' => int 22
  protected 'position' => int 0

The following returns all records as above:

for ($i = 0; $i <  $resultSet->count(COUNT_NORMAL); $i++)
        {
            var_dump($resultSet);
            $resultSet->next();
         }
2

There are 2 answers

0
Vahe On BEST ANSWER

Answer

The exchange array method shown below needs to have proper case for the $data array elements. I revised all lines starting with the sample one below to have the proper case. Since ActionItemID is a string it needs to have to the proper case of the ActionItemID.

Answer Snippet Code (within exchangeArray method)

$this->actionItemID = (isset($data['ActionItemID'])) ? $data['ActionItemID'] : null;

How I arrived at the solution...

Step 1) In order to detect the issue I used the following code to see what was happening within fetchAll() of my Table class.

/* @var $resultSet ResultSet */

$resultSet->getDataSource();

var_dump($resultSet->getDataSource());

Step 2) Then after inspecting the var_dump output produced

object(Zend\Db\Adapter\Driver\Pdo\Result)[264]
  protected 'statementMode' => string 'forward' (length=7)
  protected 'fetchMode' => int 2
  protected 'resource' => 
    object(PDOStatement)[265]
      public 'queryString' => string 'SELECT `actionitems`.*, users.lastName AS `OwnerLastName`, users.firstName AS `OwnerFirstName` FROM `actionitems` INNER JOIN `users` ON `actionitems`.`OwnerID` = `users`.`UserID`' (length=178)
  protected 'options' => null
  protected 'currentComplete' => boolean true
  protected 'currentData' => 
    array (size=22)
      'ActionItemID' => string '1' (length=1)
...

Step 3) I observed the last line from the code above signaled the proper case for ActionItemID which then led to changing the case below.

Step 4) I changed my code from having 'actionItemID'

$this->actionItemID = (isset($data['actionItemID'])) ? $data['actionItemID'] : null;

Step 5) To this (Note Capitalization of 'ActionItemID')

$this->actionItemID = (isset($data['ActionItemID'])) ? $data['ActionItemID'] : null;

Conclusion

Since strings are Case Sensitive, one must adhere to the proper capitalization especially when using that string within an associative array as in this case. Otherwise the array element will not have a value and will not be set and it would lead to null being assigned to the actionItemID and all other fields as was observed in the question.

1
Conti On

You need to use "foreach" to get the entries form DB, which is the best way for ResulSet objects:

foreach($resultSet $result){
    var_dump($result);
}

Try it.