【问题标题】:PHP/Zend Framework 2 - Unable to display table field values within dynamically generated tablePHP/Zend Framework 2 - 无法在动态生成的表中显示表字段值
【发布时间】:2025-12-05 05:25:04
【问题描述】:

问题

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

问题

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

代码

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

以下返回所有记录如上:

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

【问题讨论】:

  • 你从“$resultSet”得到什么?
  • 当我在 for 循环或 foreach 循环中遍历结果集时,我得到数组每个元素的每个字段的空值。

标签: php mysql model-view-controller zend-framework2


【解决方案1】:

您需要使用“foreach”从 DB 中获取条目,这是 ResulSet 对象的最佳方式:

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

试试看。

【讨论】:

  • 我得到了适量的结果,但此时结果都显示为空。这一定是问题所在。我会再试一次看看发生了什么。
【解决方案2】:

回答

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

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

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

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

第 1 步)为了检测问题,我使用以下代码查看 Table 类的 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'])) ? $data['ActionItemID'] : null;

结论

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

【讨论】:

    最近更新 更多