0

I have those two tables:

Addresses:

id | member_id | label | ... | start | end | ...

and

Members:

id | firstname | ... | created | modified

Every member has many addresses during his life, and I need to track when he lived where. That's why I have a start and an end field in the address.

I'm working in Members Scope, select a few of them by other data and want to access their current address. This is the address with the highest start value, so i decided to add another association in the MembersTable.php called CurrentAddress.

This is my current Version:

    $this->hasOne('CurrentAddress', [
        'foreignKey' => 'member_id',
        'className' => 'Addresses',
        'conditions' => [
            
        ]
    ])
      ->setProperty('current_address')
      ->setDependent(true);

I played around and checked the cookbook, but i can't get a condition to find the highest start date of the Address to work.

Could anyone show me the correct definition for "Highest start date of this users addresses"

Thanks in advance!

molarum
  • 13
  • 4
  • Sounds like **https://stackoverflow.com/questions/30241975/how-to-limit-contained-associations-per-record-group**. In your case another option could possibly be to add an additional column to mark the current address. – ndm Aug 15 '22 at 16:13
  • i've seen this one and this is my absolut last option. The guys entering the data are young students, mostly drunk and really unreliable most of the time (its a membership software for a students and alumni club). I can't guarantee they always set the right marks and i thought this would be the most simple and elegant solution in terms of coding... – molarum Aug 15 '22 at 19:50
  • Not sure if could then even rely on them providing the correct dates ;) The linked question's answer should however work for the date based lookups, it's not very straightforward, but overall pretty solid. – ndm Aug 15 '22 at 20:30

0 Answers0