dongying6179 2013-07-24 11:23
浏览 63

Cakephp join:从两个表中检索数据

i am working on a Cakephp 2.x.. i have two tables into my db name Images and Audios .. both have a userid field ... first what i want is i want to retrive the data and sort by date field which is in both the tables.. on my view page i want to show the images and videos by date...

for example image1,video2,video3,image2

right now i am doing this in Images model

   function getImagesAndAudio($userid){
       $this->bindModel(array(
        'belongsTo' => array(
            'Audio' => array(
                'className' => 'Audio',
                'foreignKey' => false,
                'conditions' => array(

                    'Image.user_id = Audio.user_id',




                ),
                'type' => 'LEFT',

                'order'=>'Image.date',//error


            )
        )
    ), false);

    return $this->find('all', array('conditions' => array('Image.User_id' => $userid),
        'contain' => array('Audio' ),
        ));

the problem is right now it gives me error if i do this

      'order'=>'date',

and the other thing i dont know how can i show the images and videos in mixup order... i mean when i do this

   foreach ($datas as $data){
     echo $data['Image']['filename'];
     echo $data['Audio']['filename'];

      }

the problem is i have to manually write the code that show image now and now show the audio... i want to show the file with respect to date .. so if there is an image after the two audios file show an image and then so on

  • 写回答

2条回答 默认 最新

  • dongmo9996 2013-07-25 14:59
    关注

    If I understand you correct i think this approach should work:

    Define the proper Relations between Image/Audio and User (User hasMany Audio/Image and Audio/Image belongsTo User)

    You can define how the related Images/Audio Files should be ordered (see CakePHP Documentation: http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasmany)

    Now in your controller just set the user for your view and you can retrieve your data with

    <?php
    //Same goes for Image
    foreach($userVariable[‘Audio‘] as $audio) {
        echo $audio['filename‘]
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题