PHP/Zend 框架 2 - 无法在动态生成的 table 中显示 table 字段值

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

问题

我正在尝试使用 Zend Framework 2 和 PHP/MySQL 将字段值显示到 table 中。鉴于以下 table 是动态生成的,我正在尝试获取如下所示的值。不幸的是,我只得到单元格位置的空值。

问题

如何解决这个问题以将值填充到它们的单元格位置?

代码

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>

编辑:

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

以下returns所有记录如上:

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

您需要使用"foreach"从数据库中获取条目,这是 ResultSet 对象的最佳方式:

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

试试吧。

回答

下面显示的交换数组方法需要为 $data 数组元素设置正确的大小写。我修改了从下面的示例开始的所有行以具有正确的大小写。由于 ActionItemID 是一个字符串,因此它需要符合 ActionItemID 的正确大小写。

答案片段代码(在 exchangeArray 方法中)

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

我是如何找到解决方案的...

步骤 1) 为了检测问题,我使用以下代码查看 Table class 的 fetchAll() 中发生了什么.

/* @var $resultSet ResultSet */

$resultSet->getDataSource();

var_dump($resultSet->getDataSource());

步骤 2) 然后在检查 var_dump 输出后产生

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)
...

步骤 3) 我观察到上面代码的最后一行表示 ActionItemID 的正确大小写,然后导致更改下面的大小写。

步骤 4) 我更改了我的代码 'actionItemID'

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

步骤5)到此(注意'ActionItemID'的大小写)

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

结论

由于字符串区分大小写,因此必须坚持正确的大写,尤其是在本例中的关联数组中使用该字符串时。否则,数组元素将没有值,也不会被设置,这将导致将 null 分配给 actionItemID 和问题中观察到的所有其他字段。