Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Storing Related Records when using multiple modules (using namespaces)

In: https://docs.phalconphp.com/en/latest/reference/models.html#storing-related-records

<?php

// Create an artist
$artist          = new Artists();
$artist->name    = 'Shinichi Osawa';
$artist->country = 'Japan';

// Create an album
$album         = new Albums();
$album->name   = 'The One';
$album->artist = $artist; // Assign the artist
$album->year   = 2008;

// Save both records
$album->save();

But when you have multiple modules and newArtists() is a new \Common\Models\Artists() the $album->artist no longer works. I have also tried $album->commonModelsArtists()

I have looked for examples but cannot find any. Any idea's?



22.8k
Accepted
answer

In the relationship I needed to defined an alias so in Albums:

    $this->belongsTo("artist_id", "Common\Models\Artists", "id", array(
        "foreignKey" => array(
            "message" => "You cannot save the album as the artist no longer exists!"
        ),
        "alias" => "Artists"
    ));