firstly, forgive scrubbiness , lack of experience in third-party orm's (have gone long "tailor-making" requirements)
i having difficult time understanding relations in silverstripe orm
what i'm trying achieve correlate set of attributes single roomtypeid
/mymodule/models/rooms.php
class rooms extends dataobject { private static $db = array( "roomtypeid" => 'int', // needs correlate attributes.roomtypeid "roomtypegroupid" => 'int', "subpropertyid" => 'int', "name" => 'varchar(255)', "description" => 'text', "maxoccupancy" => 'int', "raw" => 'int', "nodiscountallowed" => 'int', "defaultarrival" => 'varchar(10)', "defaultdeparture" => 'varchar(10)', "image1" => 'varchar(255)', "image2" => 'varchar(255)', "image3" => 'varchar(255)', "image4" => 'varchar(255)', "image5" => 'varchar(255)', "mincost" => 'currency' ); public static $has_many = array( "attributes" => "attribute" ); }
/mymodule/models/attributes.php
class attributes extends dataobject { private static $db = array( "roomtypeid" => 'int', // needs correlate room.roomtypeid "attributeid" => 'int', "name" => 'varchar(255)' ); private static $has_one = array( "rooms" => "rooms" ); }
however docs column added db <relation-name>id
not conform need correlate multiple attributes rooms.roomtypeid
due misunderstanding , time limitations i've literally been
rooms::get()->filter([...]) attributes::get()->filter([...])
which , on php side of things non-plausible context of template.
any guidance highly appreciated!
note: roomtypeid retrieved external api
i believe confusion may stemming treating objects full sql tables - not case. orm allow developer work in oop terms, , not have particularly worry how information stored.
i hope i'm not wrong (your description rather brief), seems you're doing too much rather particularly wrong.
each relation in silverstripe requires reverse set. not strictly required in has_one
or many_many
, in has_many
. way silverstripe inspects model information , generates storage (cf. dev/build
).
as seem particularly clued in sql, i'll jump straight technical explanations:
$db
object's native properties, not relational data.- a
has_one
store external id on object's table, in form<relationship name>id
, i.e."attributes"."roomsid"
. generated you. - a
has_many
store object's own id on relation's table. not generated you, , requireshas_one
on related class (as have in code demo). - a
many_many
stores relations in own table, matching 2 external ids. reasonably traditional join table. reverse not strictly required because of nature of join table, idea set reverse anyway, in formbelongs_many_many
. let orm know relationship exists when you're dealing directly 'child' objects.
you can see above bullet points visually explained in image @ http://www.silverstrip.es/blog/diagram-of-relationships-in-silverstripe/
as side note looks you're storing image references varchars. required if information kept externally. iff images managed via silverstripe, many_many
image
sufficient. otherwise there examples of having many images related object in lesson 7 on official silverstripe docs website.
and furthermore, manual fetch , filter plausable template. defining function on model (or controller) rooms::getattributes() and/or attributes::getrooms() , returning get()->filter() sequence allow resulting queries accessed in template via $attributes
on rooms , $rooms
on attributes, respectively. should careful though, can gives issues magic fetch (via php's __call
) easily.
Comments
Post a Comment