Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.6] Improving relation get set interface #7506

Conversation

rossriley
Copy link
Contributor

This is based on feedback from #7497 that the interface for interacting with relations has become a bit complicated.

This is a small PR that adds a helper method to make it easier to associate content entities with an owner.

The public-facing API is straightforward, given either an entity, a collection or a string/id combination we can easily associate relations.

$repo = $app['storage']->getRepository('entries');
$entry = $app['query']->getContent('entries/1');
// Or direct from the repo
$entry = $repo->find(1);

// Option 1: Associate relations with this entity by name and ids
$entry->relation->associate('pages', [1,2,3]);

// Option 2: Associate relations via passing a single Entity
$page = $app['query']->getContent('pages/1');
$entry->relation->associate($page);

// Option 3: Associate a set of relations by passing a collection
$pages = $app['query']->getContent('pages', ['id'=> '1 || 2 | |3]']);
$entry->relation->associate($pages);


// Save
$repo->save($entry);

- allow a reference to the owning entity to be stored, reducing the complexity of working with object
- add some helper methods to the relations collection to make associating easier
- also allow collections or arrays to be passed
- fix the coercion of contenttype strings
- add new unit test to cover new behaviour
$this->assertCount(count($collectionToTest), $relations['pages']);
}

}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The closing brace for the class must go on the next line after the body

bobdenotter
bobdenotter previously approved these changes Jun 3, 2018
Copy link
Member

@bobdenotter bobdenotter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it! 👍

@GwendolenLynch GwendolenLynch changed the title [3.5? Feature] Improving relation get set interface [3.6] Improving relation get set interface Jun 10, 2018
@GwendolenLynch GwendolenLynch changed the base branch from 3.5 to 3.6 June 10, 2018 11:44
@GwendolenLynch GwendolenLynch force-pushed the feature/improving-relation-get-set-interface branch from 030768b to 38804af Compare June 10, 2018 11:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants