doujia4619 2014-07-29 13:14 采纳率: 0%
浏览 40
已采纳

Kohana / PHP-在将记录传递给视图之前向记录添加更多信息

Scenario: I have a view to which i pass a kohana record-set object, and view will go through each record and display its values in appropriate format. In the view i have to alert user with a message. This alert will only get activated if certain criterion is met, for example, view have to do PHP datediff using the date property of each record and see whether it is the best time to display the alert to user.

What i am thinking is instead of the view do the calculation, i would do it inside the controller. However, I believe it is a bad idea inside the controller to have a loop which iterates through each record and do the calculation and wraps the record inside a wrapper object along with additional properties specific for view. What i am looking for something like a callback in the model which gets called every time, a record is fetched. I can do the my calculations there and return the object. Is such an approach possible with Kohana? If not, please tell me what is the best solution to fit in this requirement?

Thanks for your time and attention.

  • 写回答

1条回答 默认 最新

  • dongweiben5229 2014-07-29 15:03
    关注

    Yes indeed, your suggestion is a good one and I use it all the time.

    I assume you have a model which extends ORM. As an example, you could do something like this.

    class Model_Example extends ORM {
        protected $_primary_key = 'id';
        protected $_table_name = 'example_table';
        // possibly relations, filters etc.
    
        function should_message_be_shown() {
            if( !$this->_loaded ) {
                throw new Kohana_Exception('Should only be called on loaded objects');
            }
    
            if( $this->date_created > ( time() - 3600 ) ) {
                return true;
            }
            return false;
        }
    
        function get_user_message() {
            if( !$this->_loaded ) {
                throw new Kohana_Exception('Should only be called on loaded objects');
            }
    
            return 'Hi ' . $this->user_name . '! This is your personal message';
        }
    }
    

    The method should_message_be_shown() makes use of the class variable Model_Example::date_created. This assumes you have a table column called date_created which holds an UNIX timestamp. So in this particular example it returns true if the record was created within the last hour.

    I have added a check to see if the record is actually loaded and throw an exception otherwise.

    To complete the example, you can fetch and use the model in your controller and view like this:

    class Controller_Example extends Controller {
        function action_index() {
            $records = ORM::factory('Example')->where('something', '=', true)->find_all();
            $this->response->body(View::factory('example')
                    ->set('examples', $records));
        }
    }
    

    And the view file:

    <div class="example-list">
        <?php foreach($examples as $example): ?>
            <div class="example-item">
                <h2><?php echo $example->title; ?></h2>
                <?php if($example->should_message_be_shown()): ?>
                    <div class="message">
                        <?php echo $example->get_user_message(); ?>
                    </div>
                <?php endif; ?>
            </div>
        <?php endforeach; ?>
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀