donglankui1263 2011-06-12 14:17
浏览 39
已采纳

cakePHP - 为模型或控制器添加新功能

I'm new to cakePHP and MVC development and trying to create something with cakePHP but can't figure out how to do this :

I'm creating a simple CRUD application which takes in Albums and Songs through simple data entry forms. I created the DB and used the Cake console app to create all the models / controllers etc and it works well. I can CRUD both albums and songs no problem, and the song table in the DB is connected to the album table with a foreign key so all the links and associations are there in the model.

What I want to do is be able to click on an album and see the songs associated with that album, ,but I'm not sure how to go about it. Do I need to add a query in the model, or does that functionality go into the Controller ?

My take is : in the album list make the album names links, which call a |viewAlbum| function in the Songs Controller with the albumID. Not sure where to go from here though ......

Can anyone point me in the right direction ?

Cheers, Colm


@JohnP Thank you for your reply. How do you create a link to call that function in the controller though ? I have :

echo $html->link(__($album['Album']['title'], true), array('controller'=>'Songs', 'action'=>'viewAlbum', $album['Album']['id']));

Where viewAlbum is the name of the function in the songs controller. Any ideas on why this doesn't work ?

  • 写回答

2条回答 默认 最新

  • dousi4257 2011-06-13 19:06
    关注

    Protos -

    If I understand correctly -- you're using John's example, and you need to fix the link in your view that calls his controller?

    <?
    echo $this->Html->link(__($album['Album']['title'], true), array('controller'=>'Album', 'action'=>'viewSongs', $id));
    
    ?>
    

    John's example explained how to create a method in the Albums controller, suggested hitting a method in the Songs model that returned the desired results.

    So your link would target the Album controller, and its action should be the controller method.

    This method makes less sense in the Songs controller, because it requires an Album id. You just want the Album controller to pull associated data from the Songs model / table. John's answer is exactly correct but maybe too complicated if you're just getting started with Cake. John split the needed functionality by putting a method in the Song model, called by a method in the Albums controller, which pulls results for your view to display.

    I'm switching this to "fat controller," which is easier to follow for short code but less MVC.

    You need a hasMany relationship from Albums to Songs - each Album hasMany Songs:

    // ../models/album.php
    
    class Album extends AppModel {
    
        var $name = 'Album';
    
        var $hasMany = array(
            'Song' => array(
                'className' => 'Song',
                'foreignKey' => 'album_id'
            )
        );
    

    Your controller action will look like this:

    // ../controllers/albums_controller.php
    
    function viewSongs($id = null) {
        if(isset($id) && $id != null) {
            $albums = $this->Album->find('first',       
                array('conditions'=>array('Album.id'=>$id));
            $songs = $this->Album->Song->find('all', 
                array('conditions'=>array('Song.album_id'=>$id)));
            // This returns variables to the view to use
            $this->set(compact('albums', 'songs'));
        }
    }
    

    Your view will be called viewSongs.ctp, and it'll look something like this:

    // ../views/albums/viewSongs.ctp
    
    <?php 
    foreach($albums as $album) {
        echo "<h2>{$album['name']}</h2>";
        echo "<ul>";
        foreach ($songs as $song) {
           echo "<li>{$song['Song']['name']}</li>"
        }
        echo "</ul>";
    }
    

    And your link in ../views/albums/view.ctp would be like:

    <?php
    echo $this->Html->link('View Songs', array('controller'=>'albums',
        'action'=>'viewSongs', $id));
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里