カテゴリ一覧表示→チャンネル一覧2
それでは、カテゴリの画面で、子カテゴリの数がゼロになったら、チャンネル一覧を表示させるようにします。
まず category_newのアクションを編集します。
/var/www/podtv/apps/frontend/modules/category_new/actions/actions.class.php
public function executeShow() { $this->category_new = CategoryNewPeer::retrieveByPk($this->getRequestParameter('id')); // parent_category_idが同じものの一覧を表示。つまりそれが子になる。 $c = new Criteria(); $c->add( CategoryNewPeer::PARENT_CATEGORY_ID, $this->getRequestParameter('id'), Criter ia::EQUAL); $this->child_categorys = CategoryNewPeer::doSelect( $c); // ついでにそれが何個あるかも表示させる。 $this->child_categorys_count = CategoryNewPeer::doCount( $c); // 子カテゴリの数がゼロだったら、チャンネル一覧を表示 if ( $this->child_categorys_count == 0){ $c2 = new Criteria(); $c2->add( ChannelPeer::CATEGORY_NEW_ID, $this->getRequestParameter('id'), Criter ia::EQUAL); $this->channel_lists = ChannelPeer::doSelect( $c2); } $this->forward404Unless($this->category_new); }
次に viewです。そろそろソースが汚くなってきました(笑)
/var/www/podtv/apps/frontend/modules/category_new/templates/showSuccess.php
<?php // auto-generated by sfPropelCrud // date: 2008/05/11 16:44:48 ?> <table> <tbody> <tr> <th>Id: </th> <td><?php echo $category_new->getId() ?></td> </tr> <tr> <th>Name: </th> <td><?php echo $category_new->getName() ?></td> </tr> <tr> <th>Parent category: </th> <td><?php echo $category_new->getParentCategoryId() ?></td> </tr> <tr> <th>Created at: </th> <td><?php echo $category_new->getCreatedAt() ?></td> </tr> <tr> <th>Updated at: </th> <td><?php echo $category_new->getUpdatedAt() ?></td> </tr> </tbody> </table> <br /> <br /> <?php echo '子カテゴリは' .$child_categorys_count .'個あります'?><br /> <?php foreach ($child_categorys as $child_category): ?> <?php echo link_to( $child_category->getName(), 'category_new/show?id='.$child_category->g etId()); ?><br /> <?php endforeach; ?> <?php if ( $child_categorys_count == 0) { echo "<hr />"; echo "チャンネル一覧を表示します<br /><br />"; ?> <?php foreach ( $channel_lists as $channel_list): ?> <?php echo link_to( $channel_list->getName(), 'channel/show?id=' .$channel_list->getId()) ."<br />"; ?> <?php endforeach; ?> <?php } ?> <hr /> <?php echo link_to('edit', 'category_new/edit?id='.$category_new->getId()) ?> <?php echo link_to('list', 'category_new/list') ?>
ここでちょっと疑問なのが、viewの中で、子カテゴリの数を調べてゼロだったら、チャンネル一覧を表示するという分岐をしているところです。viewの中でこういう分岐を入れてしまって良いのかな〜と。でも、他にどこに入れれば良いのか分からないです。では、表示させてみます。
http://podtvlabo.com/frontend_dev.php/category_new/show/id/14
ちゃんとできました。ほっ。そして、サッカーダイジェストをクリックした画面がこちら。
チャンネルの詳細画面が表示されました。まぁ、ここに来るまで何回かつかえているのですが、その辺、自分でも書ききれないので省略。
実際にやってみるまで、category_newのアクションの中で ChannelPeerとか呼べるのかどうか分からなかったのですが、ちゃんと呼べるみたいです。
という事は、TOP画面を表示させる defaultモジュールから CategoryNewPeerとかでデータを呼び出す事ができるという事ですね。でも、似たような実装が2つできちゃうから、美しくないかな?
次は、backendコントローラーを作って、そちらから frontendで作ったデータにアクセスできるかテスト。