Is there no API for has_one relations?

46 views
Skip to first unread message

Patrick Nelson

unread,
Aug 22, 2016, 7:06:59 PM8/22/16
to silverst...@googlegroups.com
I'm noticing that there's a PHP-based API for has_many and many_many relations, but I'm not seeing any documentation covering an API for has_one relations, e.g. take a polymorphic has_one for example:

// Where "tag" could also be any other DataObject which points back to "Story.Polymorphic"
$tag = Tag::create();
$tag->Title = 'Test Tag';
$tag->write();

$story->Polymorphic = $tag
$story->write();

Intuitively, this is how I'd write it, but it doesn't seem to work for me. In this example, TagID and TagClass remain 0 and null respectively. Is there some other magic to this or do I really need to edit those fields directly? Just curious since I'm not sure how else I'd implement this in the CMS anyway (albeit my use case may still require more direct access like $story->relation()->add(...) anyway instead of some pre-built FormField).

Any ideas? Is there a better way?

Patrick Nelson

unread,
Aug 22, 2016, 7:09:56 PM8/22/16
to silverst...@googlegroups.com
D'oh, I sent before I could link to the current documentation:

I think a refinement of my question would be this: Is there an API for properly associating DataObjects in a has_one/belongs_to (1:1) relationship? Technically you can certainly setup an association in a has_one since typically there would then be a has_many on the other end, so you'd simply branch off that has_many association to then call the ->add() method. 

Loz Calver

unread,
Aug 23, 2016, 4:08:10 AM8/23/16
to SilverStripe Core Development
Related: https://github.com/silverstripe/silverstripe-framework/issues/3832. I’ve not investigated how much work it’d be, I said in my comment that I thought it’d be easy but I can’t shake the feeling that I’ve oversimplified it somehow!

Loz

Patrick Nelson

unread,
Aug 23, 2016, 12:46:02 PM8/23/16
to silverst...@googlegroups.com
I agree. With SS it can be a bit of a pandora's box sometimes :) Thanks!

--
You received this message because you are subscribed to the Google Groups "SilverStripe Core Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to silverstripe-dev+unsubscribe@googlegroups.com.
To post to this group, send email to silverstripe-dev@googlegroups.com.
Visit this group at https://groups.google.com/group/silverstripe-dev.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages