ここの情報は古いです。ご理解頂いた上でお取り扱いください。

Changeset 5066


Ignore:
Timestamp:
Jan 28, 2008, 11:18:45 AM (12 years ago)
Author:
ebihara
Message:

#1843:DiaryモデルにDiaryCommentとMemberを関連づけた。リレーション(とnull地獄)のサンプル

Location:
OpenPNE/branches/prj/framework/cakephp/app
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • OpenPNE/branches/prj/framework/cakephp/app/config/sql/convert_openpne2cakepne.sql

    r5061 r5066  
    3939) ENGINE=MyISAM DEFAULT CHARSET=utf8;
    4040
     41DROP TABLE IF EXISTS `diary_comments`;
     42CREATE TABLE `diary_comments` (
     43  `id` int(11) NOT NULL auto_increment,
     44  `diary_id` int(11) NOT NULL,
     45  `member_id` int(11) NOT NULL,
     46  `body` text NOT NULL,
     47  `created` datetime NOT NULL,
     48  PRIMARY KEY  (`id`)
     49) ENGINE=MyISAM DEFAULT CHARSET=utf8;
     50
    4151INSERT INTO `members` (`id`, `nickname`, `birth_year`, `birth_month`, `birth_day`,
    4252`image_filename`, `access_date`, `created`, `modified`)
     
    5767     FROM `c_diary`
    5868     ORDER BY `c_diary_id`);
     69
     70INSERT INTO `diary_comments` (`id`, `diary_id`, `member_id`, `body`, `created`)
     71  (SELECT `c_diary_comment_id`, `c_diary_id`, `c_member_id`, `body`, `r_datetime`
     72     FROM `c_diary_comment`
     73     ORDER BY `c_diary_id`);
  • OpenPNE/branches/prj/framework/cakephp/app/controllers/diaries_controller.php

    r5061 r5066  
    33{
    44    var $name = 'Diaries';
    5     var $uses = array('Diary');
     5    var $uses = array('Diary', 'DiaryComment');
    66
    77    function view($id)
    88    {
    9         $diary = $this->Diary->findById($id);
     9        $diary = $this->Diary->findById($id, null, null, 2);
     10        $comment_count = $this->DiaryComment->findCount(array('diary_id' => $id));
    1011        $this->set('diary', $diary);
     12        $this->set('comment_count', $comment_count);
    1113        $this->pageTitle = $diary['Diary']['subject'];
    1214    }
  • OpenPNE/branches/prj/framework/cakephp/app/models/diary.php

    r5061 r5066  
    33{
    44    var $name = 'Diary';
     5    var $hasMany = array(
     6        'DiaryComment' => array(
     7            'className' => 'DiaryComment',
     8            'order' => '',
     9            'conditions' => '',
     10            'dependent' => '',
     11            'foreignKey' => 'diary_id',
     12        ),
     13    );
     14    var $belongsTo = array(
     15        'Member' => array(
     16            'className' => 'Member',
     17            'fields' => array('id', 'nickname'),
     18            'order' => '',
     19            'conditions' => '',
     20            'dependent' => '',
     21            'foreignKey' => 'member_id',
     22        ),
     23    );
    524
    625    var $validate = array (
  • OpenPNE/branches/prj/framework/cakephp/app/views/diaries/view.tpl

    r5061 r5066  
    1 <h2>({$diary.Diary.subject})</h2>
     1<h2>({$diary.Member.nickname})さんの日記</h2>
     2<h3>({$diary.Diary.subject})</h3>
    23<div class="diary_info" style="border-bottom:1px solid #000">
    34({$diary.Diary.created})
    45</div>
    56<div class="diary_body" style="border-bottom:1px solid #000">
    6 ({$diary.Diary.body|nl2br})
     7({$diary.Diary.body|escape|nl2br})
    78</div>
     9({if count($diary.DiaryComment)})
     10<hr>
     11<h2>コメント</h2>
     12<ul>
     13({foreach from=$diary.DiaryComment item=value})
     14    <li>({$value.created|date_format:"%Y/%m/%d"})&nbsp;…&nbsp;({$value.body|escape})(<a href="({$html->url('/members/home/')})({$value.Member.id})">({$value.Member.nickname})</a>)</li>
     15({/foreach})
     16</ul>
     17({/if})
Note: See TracChangeset for help on using the changeset viewer.