2013-02-14 166 views
0

我使用Kohana 3.2和Bootstrap 2.3.0開發迷你博客,這是它的數據庫結構表。我要評論評論(Kohana 3.2)

CREATE TABLE IF NOT EXISTS `entries` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `entry_title` varchar(255) NOT NULL, 
    `entry_content` text NOT NULL, 
    `entry_created_date` datetime NOT NULL, 
    `entry_edited_date` datetime NOT NULL, 
    `entry_created_ip` varchar(40) NOT NULL, 
    `entry_edited_ip` varchar(40) NOT NULL, 
    `entry_published` tinyint(1) NOT NULL, 
    `entry_published_comment` tinyint(1) NOT NULL, 
    `entry_hits` int(11) NOT NULL, 
    `catentry_id` tinyint(2) NOT NULL, 
    `user_id` int(11) NOT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8; 

CREATE TABLE IF NOT EXISTS `comments` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `comment_message` text NOT NULL, 
    `comment_date` datetime NOT NULL, 
    `comment_ip` varchar(40) NOT NULL, 
    `entry_id` int(11) NOT NULL, 
    `user_id` int(11) NOT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8; 

這是我的項目位置。 C:\ wamp \ www \ iblog

創建模型條目。 C:\ WAMP \ WWW \ iblog \程序\類\型號\ entry.php

<?php 

defined('SYSPATH') or die('No direct script access.'); 

class Model_Entry extends ORM { 
    protected $_belongs_to = array(
     'catentry' => array(), 
     'user' => array() 
    ); 

    protected $_has_many = array(
     'comments' => array() 
    ); 
} 

創建模型評論。 C:\ WAMP \ WWW \ iblog \程序\類\型號\ comment.php

<?php 

defined('SYSPATH') or die('No direct script access.'); 

class Model_Comment extends ORM { 
    protected $_belongs_to = array(
     'entry' => array(), 
     'user' => array() 
    ); 
} 

創建控制器函數入口管理。 C:\ WAMP \ WWW \ iblog \程序\類\控制器\ manage.php

function action_entry() { 
     if (Session::instance()->get('user') == null && Session::instance()->get('user')->usertype_id != 1) { 
      $this->template->content = View::factory('user/login'); 
     } else { 

      // Get the total count of records in the enry table 
      $count = ORM::factory('entry')->find_all()->count(); 

      // Create an instance of Pagination class and set values 
      $pagination = Pagination::factory(array(
         'total_items' => $count, 
         'items_per_page' => 10, 
        )); 
      $pagination->route_params(array('controller' => $this->request->controller(), 'action' => $this->request->action())); 

      // Load specific results for current page 
      $entries = ORM::factory('entry') 
        ->order_by('id') 
        ->limit($pagination->items_per_page) 
        ->offset($pagination->offset) 
        ->find_all(); 

      // Render the pagination links 
      $page_links = $pagination->render(); 

      // Define our template view and bind to variables 
      $this->template->content = View::factory('manage/entry') 
        ->bind('count', $count) 
        ->bind('entries', $entries) 
        ->bind('page_links', $page_links); 
     } 
    } 

創建視圖項。 C:\ WAMP \ WWW \ iblog \程序\意見\管理\ entry.php

<ul class="breadcrumb"> 
    <li> 
     <?php echo HTML::anchor('', '<i class="icon-home icon-white"></i>&nbspHome'); ?> 
     <span class="divider">/</span> 
    </li> 
    <li> 
     <?php echo HTML::anchor('manage/index', 'manage'); ?> 
     <span class="divider">/</span> 
    </li> 
    <li class="active">entry</li> 
</ul> 

<div class="page-header"> 
    <span class="pull-right"> 
     <?php echo HTML::anchor('manage/index', '<strong>กลับไปหน้าตั้งค่า</strong> <i class="icon-signout"></i>', array('class' => 'btn btn-danger')); ?> 
    </span> 
    <h2><i class="icon-tasks"></i> จัดการบล็อก</h2> 
</div> 

<table class="table table-striped table-condensed table-hover"> 
    <thead> 
     <tr> 
      <th>#</th> 
      <th>Name</th> 
      <th>Category</th> 
      <th>Comment Count</th> 
      <th>publish?</th> 
      <th>comment?</th> 
      <th>Hits</th> 
      <th>Owner</th> 
      <th>Created</th> 
      <th>Modified</th> 
      <th style="width: 36px;"></th> 
     </tr> 
    </thead> 
    <tbody> 
     <?php foreach ($entries as $entry): ?> 
      <tr class="tooltip-demo"> 
       <td><?php echo $entry->id; ?></td> 
       <td> 
        <?php echo HTML::anchor('', '<i class="icon-eye-open"></i>', array('rel' => 'tooltip', 'title' => $entry->entry_title)); ?> 
       </td> 
       <td><?php echo $entry->catentry->cat_entry_name; ?></td> 
       <td><?php echo 'no'; ?></td> 
       <td> 
        <?php 
         switch($entry->entry_published) { 
          case 0: echo '<span class="label label-warning">publish</span>';break; 
          case 1: echo '<span class="label label-success">waited</span>';break; 
         } 
        ?> 
       </td> 
       <td> 
        <?php 
         switch($entry->entry_published_comment) { 
          case 0: echo '<span class="label label-important">On</span>';break; 
          case 1: echo '<span class="label label-success">Off</span>';break; 
         } 
        ?> 
       </td> 
       <td><?php echo HTML::anchor('entry/viewEntry/'. $entry->id, @$entry->entry_hits); ?></td> 
       <td><?php echo $entry->user->username; ?></td> 
       <td><?php echo $entry->entry_created_date; ?></td> 
       <td> 
        <?php 
         if ($entry->entry_edited_date == '0000-00-00 00:00:00') { 
          echo 'Not Modified.'; 
         } else { 
          echo $entry->entry_edited_date; 
         } 
        ?> 
       </td> 
       <td> 
        <?php echo HTML::anchor('manage/editEntry/' . $entry->id, '<i class="icon-edit"></i>', array('rel' => 'tooltip', 'title' => 'แก้ไข')); ?> 
        <?php echo HTML::anchor('manage/deleteEntry/' . $entry->id, '<i class="icon-minus-sign"></i>', array('rel' => 'tooltip', 'data-placement' => 'right', 'title' => 'ลบบล็อกนี้', 'onclick' => 'return confirm(\'ต้องการลบบล็อกนี้ใช่หรือไม่?\')')); ?> 

    <!--<a href="<?php //echo url::base(); ?>admin/delete/<?php //echo $catentry->id; ?>" onclick="return confirm('ต้องการลบหมวดนี้ใช่หรือไม่?')" role="button" data-toggle="modal"><i class="icon-minus-sign"></i></a>--> 
       </td> 
      </tr> 
     <?php endforeach; ?> 
    </tbody> 
</table> 

<hr> 

<div class="pagination"> 
    <?php echo @$page_links; ?> 
    <span class="pull-right"><?php echo Form::label('', '<h4><i class="icon-book"></i> ' . $count . ' Entries</h4>'); ?></span> 
</div> 

Ploblem。我如何評論評論計數值?

View sample entries

回答

0

在您看來,裏面的for循環,嘗試插入這樣的:

<?php echo $entry->comments->find_all()->count(); ?> 

,既然你標誌着進入HAS_MANY意見應該工作。

1

鑑於上 「的foreach」 身體,你可以這樣寫:

<?php echo $entry->comments->count_all(); ?>