0

我正在使用 Kohana 3.2 和 Bootstrap 2.3.0 开发迷你博客,这是一个数据库结构表。

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\application\classes\model\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\application\classes\model\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\application\classes\controller\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\application\views\manage\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>กลับไปหน้าตั้งค่า&lt;/strong> <i class="icon-signout"></i>', array('class' => 'btn btn-danger')); ?>
    </span>
    <h2><i class="icon-tasks"></i> จัดการบล็อก&lt;/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>

问题。我如何计算评论计数值的评论?

查看示例条目

4

2 回答 2

1

鉴于“foreach”正文,您可以这样写:

<?php echo $entry->comments->count_all(); ?>
于 2013-02-17T00:41:54.203 回答
0

在您看来,在 for 循环中,尝试插入:

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

这应该可以工作,因为您将条目标记为 has_many 评论。

于 2013-02-15T07:26:40.770 回答