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 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档
  • ¥50 C++五子棋AI程序编写
  • ¥30 求安卓设备利用一个typeC接口,同时实现向pc一边投屏一边上传数据的解决方案。