0

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.

sradha
  • 2,216
  • 1
  • 28
  • 48
  • Whoops, I didn't noticed my newly gained cakephp tag powers of being able to close questions with a single vote. I wasn't 100% sure, but I think this is essentially what you are trying to do - sorry if that isn't the case! If your problem is different, please elaborate on it, and I'll vote to reopen it in case necessary. – ndm May 18 '16 at 11:33
  • You can use func()->min() whci it was working on cakephp3.1 but not cakephp3.2+ http://book.cakephp.org/3.0/en/orm/query-builder.html – Fury Aug 22 '16 at 14:09

1 Answers1

0

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);
}
arilia
  • 9,373
  • 2
  • 20
  • 44