Congrats on the release of Quick 5!
I have been experimenting with different relationship types, and I like that Quick allows setting relationships via setter methods like set
& relationshipName
. However, I noticed something unexpected where some relationship types persist changes to the database immediately, whereas others do not and wait for the save()
method to be called. I am curious if there is some type of reasoning or logic behind why Quick behaves that way, as it feels inconsistent.
Instinctively, I assumed the relationships would be set in the entity first and reside in memory (_relationshipData
) until such time that the entity as a whole is saved/persisted, thus separating the persistence layer from the domain layer. However, this is not the case with some relationship types.
Here’s some sample code to further show what I mean:
// User entity
component
extends="quick.models.BaseEntity"
accessors="true"
{
property name="id";
property name="firstName";
property name="lastName";
property name="userTypeId";
/**
* User belongs to a userType
*/
function userType() {
return belongsTo( "UserType", "userTypeId" );
}
/**
* User belongs to many locations
*/
function locations() {
return belongsToMany( "Location", "location_user", "userId", "locationId" );
}
}
// Users handler snippet
prc.user = getInstance( "User" ).findOrFail( rc.id );
prc.userType = getInstance( "UserType" ).findOrFail( 3 );
prc.locations = getInstance( "Location" ).all();
prc.user.setUserType( prc.userType ); // set relationship: Does not persist yet.
prc.user.setLocations( prc.locations ); // set relationship: Persists immediately - yikes!