# Create a HasOne association for the most recent record

**URL:** <https://discourse.cakephp.org/t/create-a-hasone-association-for-the-most-recent-record/10060>\
**Category:** Uncategorized\
**Created:** [January 8, 2022, 9:51am UTC](https://discourse.cakephp.org/t/create-a-hasone-association-for-the-most-recent-record/10060 "2022-01-08T09:51:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bgrinter](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/bgrinter/32/1381_2.png) [@bgrinter](https://discourse.cakephp.org/u/bgrinter)\
**Post date:** [January 8, 2022, 9:51am UTC](https://discourse.cakephp.org/t/create-a-hasone-association-for-the-most-recent-record/10060/1 "2022-01-08T09:51:29Z")

</div>

Is is possible to create a HasOne association to get the lastest record only?

I have the following association - a Member may have several warrants

```auto
$this->hasMany('MemberWarrants', [
    'foreignKey' => 'member_id'
]);

```

I want to be able to select a list of members, and also return the latest MemberWarrant for each Member.

I had tried limits on the query but it just returned 1 MemberWarrant for the whole selection.

I’ve also created a hasOne association

```auto
$this->hasOne('CurrentMemberWarrant', [
    'className' => 'MemberWarrants',
    'conditions' => function ($e, $query) {
        $query
            ->order(['CurrentMemberWarrant.date_issued' => 'DESC'])
        ->limit(1);
        return $e;
    }
]);

```

But it always just returns each Member Warrant and duplicate entries for the Member.

I’ve looked through the SQL Log and I’ve also tried to create what I want in SQL but can’t get it ☹

What I’d done previously in CakePHP 2.x was to create a binding key in the Member record and then in the afterSave for the associated record, update the member binding key with the last record.

This is an example we have for the Member Units - a Member might move to a different Unit, we have all their history and also a simple relationship to get the current Unit. It worked - although I was hoping with the ORM where might be a better way to handle this. Apparently not?

```auto
$this->hasMany('MemberUnits', [
    'foreignKey' => 'member_id'
]);

...

$this->hasOne('CurrentMemberUnits', [
    'className' => 'MemberUnits',
    'foreignKey' => 'id',
    'bindingKey' => 'current_member_unit_id'
]);

```

---

<div class="post-metadata">

**Author:** ![ndm](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/ndm/32/2234_2.png) [@ndm](https://discourse.cakephp.org/u/ndm)\
**Post date:** [January 8, 2022, 12:52pm UTC](https://discourse.cakephp.org/t/create-a-hasone-association-for-the-most-recent-record/10060/2 "2022-01-08T12:52:33Z")

</div>

I was never here…

> [@Limit contained record - not working!](https://discourse.cakephp.org/t/limit-contained-record-not-working/9872/3):
>
> That’s due to how the ORM works, associated data is either retrieved in a separate query (hasMany/belongsToMany), or in a join (hasOne/belongsTo, this can also be changed to use a separate query). For the former if you apply a limit, it will limit the overall number of associated records, not the records “per parent”. You could give [icings/partitionable](https://github.com/icings/partitionable) a try. Also check [cakephp - How to limit contained associations per record/group? - Stack Overflow](https://stackoverflow.com/questions/30241975/how-to-limit-contained-associations-per-record-group/30270675#30270675) for some additional workarounds.

---

<div class="post-metadata">

**Author:** ![bgrinter](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/bgrinter/32/1381_2.png) [@bgrinter](https://discourse.cakephp.org/u/bgrinter)\
**Post date:** [January 8, 2022, 8:59pm UTC](https://discourse.cakephp.org/t/create-a-hasone-association-for-the-most-recent-record/10060/3 "2022-01-08T20:59:18Z")

</div>

🙂  
Thanks - we’re on MariaDB so appears there might be issue with partitionable but will check it out  
I also found that Stack Overflow page - tried some of the workarounds but didn’t help alas.

Once again thanks - no need now to keep banging my head on the keyboard trying to figure it out!
