2017-01-14 40 views
0

繼開發者文檔here是否有可能通過對象的屬性進一步篩選結果?具體5.8快速輸入塊 - 按屬性篩選結果

例如,假設您在碼頭擁有多個船隻並擁有相同的所有者,並且您只希望在碼頭中找到所有者的船隻是否存在進一步過濾數據的方式(即按屬性BoatOwner過濾) 。

經過大量閱讀Doctrine2文檔,我可以理解,這可以做到,但我不能解決如何擴展C5代碼或我可以調用什麼方法來做到這一點。

<?php defined('C5_EXECUTE') or die(_("Access Denied.")); ?> 
<?php 

if (isset($entry) && is_object($entry)) { 

$boats = $entry->getBoats(); 

?> 

<table class="table"> 
    <thead> 
    <tr> 
     <th>Name</th> 
     <th>Year</th> 
     <th>Owner</th> 
     <th>Classification</th> 
    </tr> 
    </thead> 
<tbody> 
<?php if (count($boats)) { 
    foreach($boats as $boat) { ?> 
     <tr> 
      <td><?=$boat->getBoatName()?></td> 
      <td><?=$boat->getBoatYear()?></td> 
      <td><?=$boat->getBoatOwner()?></td> 
      <td><?=$boat->getBoatClass()?></td> 
     </tr> 
    <?php } ?> 
<?php } else { ?> 
    <tr> 
     <td colspan="4">No boats found.</td> 
    </tr> 
<?php } ?> 
</tbody> 
</table> 

<?php } ?> 

以上是C5文檔中的代碼。魔法「get」方法可以以某種方式擴展嗎?還是有更簡單的解決方案來處理$ boats數組(我認爲它是一個數組),以便只選擇具有特定屬性值的小船?

回答

0

答案是放置一個if語句與foreach循環。

if (count($boats)) { 
    foreach($boats as $boat) { 
if($boat->getBoatOwner() == "boat owner's name here") { 
?> 
<tr> 
     <td><?=$boat->getBoatName()?></td> 
     <td><?=$boat->getBoatYear()?></td> 
     <td><?=$boat->getBoatOwner()?></td> 
     <td><?=$boat->getBoatClass()?></td> 
    </tr> 

<?php; 
      } else { 
       ?> 

    <?php; 
      } 



     ?> 



     <?php } ?> 
    <?php } else { ?> 
     <tr> 
      <td colspan="4">No boats found.</td> 
     </tr> 
    <?php } ?> 
    </tbody> 
</table> 
<?php } ?> 

我想我會通過船主的名字作爲變量可能從頁面屬性爲這是任何使用。