QueryBuilder限制只适用于cakephp 3.2中的第一行 [英] QueryBuilder limit is working for only first row in cakephp 3.2

查看:197
本文介绍了QueryBuilder限制只适用于cakephp 3.2中的第一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hii我是新的cakephp 3.2 v。
这里我使用模型关联(hasMany)。
在绑定部分(campaign_videos),我想只提取一个记录,
所以为此,我已经把下面的代码来管理它。
我在db中的实际数据。

  [
{
id:1,
user_id:95,

campaign_videos:[
{
id:1,
campaign_id:1,

},
{
id:3,
campaign_id:1,

}
]
},
{
id:2,
user_id:95,

campaign_videos:[
{
id:2,
campaign_id:2,

}
]
},



  $ fetchCampaignFirst = $ this-> Campaigns-> find() - > contains(['CampaignVideos'=> ['queryBuilder'=> function($ q){
return $ q-> limit(1);
}]] );

我得到此限制仅适用于第一个数据,不适用于其他人(其他人甚至不显示获取的数据)数据)。



下面我写了输出



这里我想得到一个输出

  [
{
id:1,
user_id :95,
campaign_videos:[
{
id:1,
campaign_id:1,

},
]
},
{
id:2,
user_id:95,

campaign_videos:[
{
id:2,
campaign_id:2,

}
]
}]

只需要campaign_videos的第一条记录。
这里在使用queryBuilder查询后,我得到了像。

  
{
id:1,
user_id:95,
campaign_videos:[
{
id:1,
campaign_id:1,

},

]
},
{
id:2,
user_id:95,

campaign_videos:[

]
}]
pre>

我没有获得第二个ID的任何数据,而数据是存在的。
请建议我。
提前谢谢。

解决方案

也许我错了(所以请自由地downvote我的答案)认为使用简单的查询是不可行的



事实上,当加载关联数据时,在关联表上执行单个查询,之后将行与相应的行匹配在主表中。



所以你需要做一个mysql查询,找到每个类别的第一个记录。即类似于 article



我认为唯一的(也许更简单的)解决方案是循环你的记录:

  $ campaigns = $ this-> Campaigns-> find(); 

foreach($ campaigns as $ campaign)
{
$ campaign-> campaign_videos = $ this-> Campaigns-> CampaignVideos-find()
- > where(['campaign_id'=> $ campaign-> id]
- > order(['id'=>'asc'])
- > limit );
}


Hii i am new to cakephp 3.2 v. Here i have used model association (hasMany). Here in bind section (campaign_videos) ,i want to fetch only one record , so for this ,i have put below code to manage it. my actual data in db.

[
    {
        "id": 1,
        "user_id": 95,

        "campaign_videos": [
            {
                "id": 1,
                "campaign_id": 1,

            },
            {
                "id": 3,
                "campaign_id": 1,

            }
        ]
    },
    {
        "id": 2,
        "user_id": 95,

        "campaign_videos": [
            {
                "id": 2,
                "campaign_id": 2,

            }
        ]
    },

  $fetchCampaignFirst = $this->Campaigns->find()->contain(['CampaignVideos' => ['queryBuilder' => function ($q) {
                            return $q->limit(1);
                        }]]);

I am getting this limit working for first data only ,not for others (others even not showing the fetched data).

Below i have written the output

Here i want to get an output like

[
    {
        "id": 1,
        "user_id": 95,
         "campaign_videos": [
            {
                "id": 1,
                "campaign_id": 1,

            },
         ]
    },
    {
        "id": 2,
        "user_id": 95,

        "campaign_videos": [
            {
                "id": 2,
                "campaign_id": 2,

            }
        ]
    }]

Only want the first record of campaign_videos. Here after using the queryBuilder query , i am getting out put like.

[
    {
        "id": 1,
        "user_id": 95,
         "campaign_videos": [
            {
                "id": 1,
                "campaign_id": 1,

            },

        ]
    },
    {
        "id": 2,
        "user_id": 95,

        "campaign_videos": [

        ]
    }]

I am not getting any data for second id ,while data is present for it. Please suggest me. Thank you in advance.

解决方案

Maybe I'm wrong (so feel free to downvote my answer) but I think it's not feasible using a simple query

in fact cake, when loading associated data, does a single query on the associated table and after that matches the rows with the corresponding ones in the main table.

So you would need to do a mysql query that find the first record of every category. i.e. something like what is described in this article

I think that the only (or maybe the simpler) solution is to loop through your records:

$campaigns= $this->Campaigns->find();

foreach($campaigns as $campaign)
{
    $campaign->campaign_videos = $this->Campaigns->CampaignVideos-find()
        ->where(['campaign_id' => $campaign->id]
        ->order(['id' => 'asc'])
        ->limit(1);
}

这篇关于QueryBuilder限制只适用于cakephp 3.2中的第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆