简体   繁体   English

Cakephp连接:从两个表中检索数据

[英]Cakephp join: retrieving data from two tables

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... 我正在CakePHP 2.x上工作..我在数据库名称中有两个表, 图像音频 ..都有一个userid字段...首先,我要检索数据并按日期字段排序在两个表中..在我的查看页面上,我想按日期显示图像和视频...

for example image1,video2,video3,image2 例如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 问题是我必须手动编写现在显示图像并现在显示音频的代码...我想显示有关日期的文件..因此,如果在两个音频文件之后显示图像,则显示图像,然后依此类推

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 ) 您可以定义相关图像/音频文件的排序方式(请参阅CakePHP文档: 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‘]
}
?>

I had the same situation. 我有同样的情况。 {With little success, I tried using CakePHP's Hash class to merge the two models' arrays in order to combine the models into a single recordset. {收效甚微,我尝试使用CakePHP的Hash类合并两个模型的数组,以将模型合并为一个记录集。 Maybe another person can chime in and explain how to do this with just CakePHP.} 也许其他人可以只使用CakePHP就能解释这一点。}

What worked for me, was to consolidate the two models (tables) within my MySQL database through a view. 对我有用的是通过视图在MySQL数据库中合并这两个模型(表)。 Continue to use your existing models for Image and Audio CrUD operations. 继续将现有模型用于图像和音频CrUD操作。 For this page, use a new model based on a view. 对于此页面,请使用基于视图的新模型。 Here is example view code: 这是示例视图代码:

CREATE VIEW vw_combined_audio_images AS
SELECT user_id, created, modified, foo, null as bar, 'image' as type
FROM images
UNION
SELECT user_id, created, modified, null, bar, 'audio'
FROM audio;

For me, I needed fields that were in one table and not the other. 对我来说,我需要一个表中的字段,而不是另一个表中的字段。 In the code example, you can see that the images table has "foo" which is not in audio and audio has "bar" not in images. 在代码示例中,您可以看到images表中没有音频中的“ foo”,而image中没有音频中的“ bar”。 (Just be careful of data type casting.) I also used a field to determine which type of record it was (see "type" above.) (请注意数据类型转换。)我还使用一个字段来确定它是哪种记录类型(请参见上面的“类型”。)

Then create the model 然后创建模型

class Asset extends AppModel{
public $useTable = 'vw_combined_audio_images';
public $belongsTo = array('User' => array(####));
}

Then add the model to the $hasMany array of the User model 然后将模型添加到User模型的$ hasMany数组中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM