2015-06-10 81 views
1

問題PHP/Zend框架2 - 無法動態地生成表

我試圖顯示字段值插入利用Zend框架2和PHP/MySQL的一個表中顯示錶的字段值。鑑於下面的表是動態生成的,我試圖得到如下所示的值。不幸的是,我只能得到單元位置的空值。

問題

我怎樣才能解決這個問題來填充值到他們的小區位置?

enter image description here

代碼

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(); 
     } 
+1

你從「$ resultSet」中得到了什麼? – Suraj

+0

當我迭代for循環或foreach循環中的結果集時,我會爲數組中每個元素的每個字段獲得空值。 – Vahe

回答

0

回答

下面所示的交換陣列的方法需要具有用於將$數據數組元素適當情況下。我修改了從下面的示例開始的所有行,以具有適當的情況。由於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')

$這 - >的大寫actionItemID =(isset($ data ['ActionItemID']))? $ data ['ActionItemID']:null;

結論

由於字符串是大小寫敏感的,必須使用字符串關聯數組內,在這種情況下,特別是在堅持正確的資本。否則,數組元素將不會有值並且不會被設置,並且會導致將null分配給actionItemID和所有其他字段,如問題中所觀察到的。

0

你需要使用 「的foreach」 來獲得項目訂單DB,這是ResulSet對象的最佳方式:

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

試試吧。

+0

我得到了適量的結果,但結果全部在此處顯示爲空。這一定是問題。我會再次嘗試看看發生了什麼。 – Vahe