Storing Related Records Question

I have a Property model that has a "hasOne->addressId" relationship to an Address model. The Address model has a "belongsTo->addressId" relationship to the Property. I create a Property instance, then create an Addess instance and add it to the Property instance. I then try to "save()" the Property instance where I get a "PresenceOf - addressId is required" error. It looks like the framework is trying to save the Property before the Address, hence thr Address id is missing. Can someone tell me the sequence that the framework uses when inserting parent and child objects?

Could you please provide models and steps to reproduce the issue?

edited 13d ago
namespace Library\Models\Properties\Base;

class Property 
{

    const TABLE_NAME = 'properties';

    public $loanId; 
    public $addressId;
    public $propertyTypeId;
    public $propertyUseId;

    /**
     * Initialize method for model
     */
    public function initialize()
    {
        parent::initialize();
        $this->hasOne(
            'addressId',
            'Library\Models\Addresses\Base\Address',
            'id', ['alias' => 'Address']);
        $this->hasOne(
            'propertyTypeId',
            'Library\Models\Types\PropertyType',
            'id', ['alias' => 'PropertyType']);
        $this->hasOne(
            'propertyUseId',
            'Library\Models\Types\PropertyUseType',
            'id', ['alias' => 'PropertyUseType']);
    }

    /**
     * Independent Column Mapping.
     * Keys are the real names in the table and the values their names in the application
     *
     * @return array
     */
    public function columnMap()
    {
        return
            [
                'loan_id' => 'loandId',
                'address_id' => 'addressId',
                'property_type_id' => 'propertyTypeId',
                'property_use_id' => 'propertyUseId'
            ];
    }
}
namespace Library\Models\Addresses\Base;

class Address
{

    const TABLE_NAME = 'addresses';
    const BLANK_FIELD = "''";

    public $loanId; 
    public $address1;
    public $address2 = self::BLANK_FIELD;
    public $city;
    public $state;
    public $zip;
    public $country;

    /**
     * Initialize method for model
     */
    public function initialize()
    {
        parent::initialize();
        $this->belongsTo(
            'id',
            'Library\Models\Properties\Base\Property',
            'addressId', ['alias' => 'Property']);
    }

    /**
     * Independent Column Mapping.
     * Keys are the real names in the table and the values their names in the application
     *
     * @return array
     */
    public function columnMap()
    {
        return
            [
                'loan_id' => 'loandId',
                'address1' => 'address1',
                'address2' => 'address2',
                'city'     => 'city',
                'state'    => 'state',
                'zip'      => 'zip',
                'country'  => 'country'
            ];
    }
}
{
        $property = new \Library\Models\Properties\Base\Property();
        $property
            ->setLoanId('96')
            ->setPropertyUseId('1')
            ->setPropertyTypeId('1');

        $address = new Address();
        $address
            ->setLoanId('96')
            ->setAddress1('3456 Some Street')
            ->setCity('Yuck')
            ->setState('MO')
            ->setZip('12345')
            ->setCountry('USA');

        $property->address = $address;
        if ($property->save() == false) {
            foreach ($property->getMessages() as $message) {
                echo $message;
            }
        }
}

Not sure how to add code segment to this forum.

```php

new stdClass();

```



35.2k
$address = new Address();
...
...

$property = new Property();
$property->Address = $address;