It would be useful to allow unary relations, ie where the arity is 1.

This is in some ways identical to just a flag -- but as a relation, it's also fieldable.

Comments

chx’s picture

Agreed. If you would provide a patch with tests I would be happy to do this.

mikran’s picture

Assigned: Unassigned » mikran

I will implement this, soon(ish)

derhasi’s picture

That was great! Would be a nice alternative to field_collection, without the overhead of caring about parent node save & co.

Would like to help out.

steveoliver’s picture

Status: Active » Needs work

mikran: I know you're probably busier than me, but do you have anything started you can post up? Maybe derhasi can run with it?
derhasi: can you provide a patch with tests as per #1?

mikran’s picture

Assigned: mikran » Unassigned

This somehow slipped. I don't have anything but the change itself is not a big thing. The interface will however require some adjustments

naught101’s picture

Status: Needs work » Active

@steve 'needs work' is for issues that have partially completed patches.

hazah’s picture

I've a use case where the minimum arity of the relation needs to be 1. In my case I have a team (content) and a roster (relation) of team members (content) belonging to that team. I needed a directional relationship from team to roster, but a non directional one for the roster itself. This works great except for the minimum 2 arity set on the roster... It means I cannot create one without at least 2 team members, which makes adding the first member a challange. With the allowance of minimum arity of 1, this would not be a problem.

// these should probably be changed to numerical (validated) textfields.
$options = array('1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6', '7' => '7', '8' => '8');  
cdale’s picture

StatusFileSize
new8.96 KB

I've created a patch for this but I have a few questions.

I'm questioning the change in relation_get_related_entity(). I have no doubt that the related entity in an unary relation is the only entity in the relation, I'm just not sure if returning it is wise? Potential breakage if relation_get_related_entity() is used in a loop to loop over relations? Though, the same thing can happen without unary relations, so possibly a non-issue.

One thing that has come up in the tests, specifically this code:


// Get unary relation on node 5, should return 1 relation.
$count = relation_query('node', $this->node5->nid)
  ->related('node', $this->node5->nid)
  ->count()
  ->execute();
  $this->assertEqual($count, 1);

This seems to return any relation that involves node5. In a way that makes sense, but I'm not sure if it's right or wrong. For the time being I've left this test as failing to await others feedback.

Are there any other tests that people would like to see?

cdale’s picture

Status: Active » Needs review
cdale’s picture

Status: Needs review » Needs work
cdale’s picture

Status: Needs work » Needs review
StatusFileSize
new8.07 KB

The more I thought about this, the less I felt it was needed to be able to query the "Other endpoint" in unary relations. In fact, it no longer made sense at all to me. Not to mention the way endpoints are stored, the only real way to tell if it's unary is to check the arity.

Attached is a patch that reflects this. There is in effect, no other endpoint. The code above will still return all endpoints that are node5, but relation_get_related_entity() will now return FALSE for unary relations.

mikran’s picture

Status: Needs review » Reviewed & tested by the community

The patch looks really solid, thanks cdale.

The more I thought about this, the less I felt it was needed to be able to query the "Other endpoint" in unary relations. In fact, it no longer made sense at all to me. Not to mention the way endpoints are stored, the only real way to tell if it's unary is to check the arity.

Yeah, I agree.

mikran’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

k.skarlatos’s picture

Does this work? I have tried to use this feature with latest dev of relation and relation add, and although the relation and the fields are shown on the edit page, on save no relation is created. Also there are no error messages.

naught101’s picture

@Aegean_communication: Please open a separate bug report, with details on what you're doing, and a copy+paste of the error messages.