XF 2.2 primary flag on entity?wha

briansol

Well-known member
What does the primary flag indicator on entity relations actually do?

is it to mark that the relationship is a PK field?
the primary relationship?

what happens if i get it wrong?

I haven't seen it do anything after trying it both ways so i'm not sure what the point of it is.

Code:
$structure->relations = [
            'MyRelation' => [
                'entity' => 'MyNs\MyAddon:MyOtherEntity',
                'type' => self::TO_ONE,
                'conditions' => [['id', '=', '$relation_id']],
                'primary' => false
            ],
 
is it to mark that the relationship is a PK field?
the primary relationship?
Yes.

You can use primary if the condition you're connecting to on the relation is a primary key. In this case it is saying that id on the MyOtherEntitiy entity is that entity's primary key.

If the value we're looking up is the primary key, we may have that entity cached already so we can retrieve that entity from the entity cache. Otherwise, we have to do a fresh query to find the related entity.

Conversely, if you use primary on a column that isn't the primary key this can cause unexpected results, so use it wisely!
 
In theory, yes, but it depends on whether that entity has been cached already or not. This is a bit of a contrived example:

PHP:
$user = $app->em()->find('XF:User', 1);
// above line fetches the User entity with user_id 1 and caches it.

$profile = $app->em()->find('XF:UserProfile', 1);
// above line fetches the UserProfile entity with user_id 1.

// later on we want to get the User entity (user_id = 1) back from the UserProfile entity that has user_id 1:
$user = $profile->User;
// above line does not require an additional query because the User entity with ID 1 was already fetched and cached in the first line.
// but... if we modified UserProfile.php and changed it to primary => false it would require an additional query
 
Back
Top Bottom