rails associations -- can I use :through multiple times? - rails-models

Can I do something like this?
Class School < ActiveRecord::Base
has_many :courses
has_many :students, :through => courses, :through => coursesections, :through => enrollments
end
Class Course < ActiveRecord::Base
has_many :coursesections
has_one :school
end
Class CourseSection < ActiveRecord::Base
has_one :course
has_one :school, :through => courses
has_many :students, :through => enrollments
end
Class Student < ActiveRecord::Base
has_many :sections, :through => enrollments
end
The Enrollment table just has a student_id and a section_id for course section that a student is enrolled in.

Related

Silverstripe - Repeating / Reusable grouped fields

I would like to implement a layout in which a column is repeated several times and can be specified as dynamic content in the CMS. However, I cannot find an object type that would fit for this.
Do I have to specify the input fields individually for each column?
private static $db = [
'Intro_Headline' => 'Varchar',
'Intro_Subheadline' => 'Varchar',
'Intro_Text' => 'HTMLText',
'Intro_Headline2' => 'Varchar',
'Intro_Subheadline2' => 'Varchar',
'Intro_Text2' => 'HTMLText',
...
];
--
$fields = parent::getCMSFields();
//Intro Field 1
$fields->addFieldToTab('Root.Intro', TextField::create('Intro_Headline', 'Headline'), 'Content');
$fields->addFieldtoTab('Root.Intro', TextField::create('Intro_Subheadline', 'Subheadline'), 'Content');
$fields->addFieldToTab('Root.Intro', HTMLEditorField::create('Intro_Text', 'Text'), 'Content');
//Intro Field 2
$fields->addFieldToTab('Root.Intro', TextField::create('Intro_Headline2', 'Headline'), 'Content');
$fields->addFieldtoTab('Root.Intro', TextField::create('Intro_Subheadline2', 'Subheadline'), 'Content');
$fields->addFieldToTab('Root.Intro', HTMLEditorField::create('Intro_Text2', 'Text'), 'Content');
...
Or can someone tell me which field type I can't find?
Update:
Now I have a $has_many variable in addition to my $db variable in my page-model:
private static $has_many = [
'Intro_Columns' => IntroColumn::class,
];
In the getCMSFields() function I add them like this:
$fields->addFieldToTab('Root.Columns', GridField::create('Intro_Columns', 'Columns', IntroColumn::get()), 'Content');
And my data object looks like this:
class IntroColumn extends DataObject
{
private static $db = [
'img_url' => 'Text',
'headline' => 'Varchar',
'subheadline' => 'Varchar',
'text' => 'Text',
'link' => 'Text'
];
}
But the fields are not yet displayed in the CMS. How do I output the data fields from a data object?
For things to be repeatable, you will have to put them into a different object, and then link multiple of those objects to your current object/page.
GridFIeld
The default way of doing this in SilverStripe 4 is using the built in Database Relation ($has_many or $many_many instead of $db) and GridField` as the form field.
I'd recommend you go through this tutorial: https://docs.silverstripe.org/en/4/developer_guides/model/relations/
In paticular the section about $has_many will apply to your usecase. (Example where 1 Team has multiple Players or 1 Company multiple People)
$has_many/$many_many is a very generic option and can be used for any number of possible database relation (linking Categories, Images, Pages, ...)
elemental module
Another option is an officially supported module called elemental. This is very specifically built for repeatable content.
https://github.com/silverstripe/silverstripe-elemental
serialized-dataobject module
Probably not ideal for your usecase, but I maintain a module that provides an alternative to GridField, but it's more suitable for small form fields. The HTMLEditor is to large to be useful in this module.
https://github.com/Zauberfisch/silverstripe-serialized-dataobject
PS: regardless of what way you go, I highly recommend you go through the tutorial from (1.). It's a pretty important fundamental functionality of SilverStripe.
EDIT: response to your updated question:
If you are using GridField, I'd recommend the following:
class Page extends SiteTree {
private static $has_many = [
'Intro_Columns' => IntroColumn::class,
];
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Columns', new GridField(
'Intro_Columns',
'My Columns',
$this->Intro_Columns(),
new GridFieldConfig_RecordEditor()
), 'Content');
return $fields;
}
}
class IntroColumn extends DataObject {
private static $db = [
'headline' => 'Varchar',
'subheadline' => 'Varchar',
'text' => 'Text',
'link' => 'Text'
];
private static $has_one = [
'Image' => 'Image',
]
public function getCMSFields() {
$fields = new FieldList();
$fields->push(new TextField('headline', 'My Headline'));
$fields->push(new TextField('subheadline', 'My Subheadline'));
// ... and so on
$fields->push(new UploadField('Image', 'Upload an image'));
return $fields;
}
}
Note that I am using $this->Intro_Columns() as the Value for the GridField and not IntroColumn::get(). Because $this->Intro_Columns() is a automatically generated Method that returns all IntroColumn objects linked to the current page. But $this->Intro_Columns() would return all IntroColumns from all Pages
In the template, you cal also access this automatically generated method:
<!-- Page.ss -->
<h1>Page Title: $Title</h1>
<div class="intro-columns">
<% loop $Intro_Columns %>
<div class="intro-column">
<!-- here we are scoped into a single IntroColumn, so you can use all DB fields and methods of that object -->
<h2>$headline <br> $subheadline</h2>
$Image.URL <br>
...
</div>
<% end_loop %>
</div>

how to embed one to many sonata admin child views in ConfigeShowFields

I have a one to many relationship between account and contacts. I use sonata admin bundle
I want to display all contacts of an account in the view detail of an account ( ConfigureShowFields in AccountAdmin class)
in class AcountAdmin.php i have :
protected function configureShowFields(ShowMapper $showMapper)
{
$showMapper
# .......
->with('Liste des contacts', array('class' => 'col-md-12'))
->add('contacts')
->end()
;
}
I believe you can do this via sonata_type_collection.
->add('contacts', 'sonata_type_collection', array(
'associated_property' => 'email',
'route' => array(
'name' => 'show'
),
'admin_code' => 'app.admin.contacts',
))
The associated_property is the associated property found in the Contacts entity, and the admin_code is the contacts admin.

Does silverstripe have recursive data relationships?

Does silverstripe have recursive data relationships? I tried to implement it and it gives no errors but the page is blank on modeladmin.
Example has_one recursive relation on Product to itself:
class Product extends DataObject {
private static $db = array(
'Name' => 'Varchar',
'ProductCode' => 'Varchar',
'Price' => 'Currency'
);
private static $has_one = array(
'Product' => 'Product'
);
}
Yes, this is possible.
There can be problems when doing this with Many_Many relationships, though.
My answer would be "no" for this done in this way. Where I've need this in the past I've created the "has one" as an "int" in the db array...
class Product extends DataObject {
private static $db = array(
'Name' => 'Varchar',
'MyProductID' => 'Int',
);
}
this means I've had to add casting for the summary field, custom scaffolding for the search fields and in the getCMSFields to replaceField the int field for a DropdownField to select a product.

symfony sonata admin roles on embedded onetomany entities

I have Symfony 2.3 + Sonata Admin + Sonata User Bundle.
I have created an entity Student, and another entity Contact. An Student have one-to-many relationship with Contact. I have added Contact to Student with sonata_type_collection in my StudentAdmin class. I also have created a group of users Operator and assigned all permissions to Student, but only list and view to Contact.
My problem is that any user of Operator can't add or delete Contact (from Student edit page), but they can edit (and values are saved).
Any suggestions or examples?
Some code:
Roles assigned:
ROLE_SONATA_ADMIN_STUDENT_EDIT
ROLE_SONATA_ADMIN_STUDENT_LIST
ROLE_SONATA_ADMIN_STUDENT_CREATE
ROLE_SONATA_ADMIN_STUDENT_VIEW
ROLE_SONATA_ADMIN_STUDENT_DELETE
ROLE_SONATA_ADMIN_CONTACT_LIST
ROLE_SONATA_ADMIN_CONTACT_VIEW
ROLE_ADMIN: ROLE_USER, ROLE_SONATA_ADMIN
/**
* #ORM\OneToMany(targetEntity="MyBundle\Entity\Contact",
mappedBy="student",
cascade={"persist", "remove"})
**/
private $contact;
->add('contact', 'sonata_type_collection',
array(
'label' => 'Contact',
'by_reference' => false,
),
array(
'edit' => 'inline',
'inline' => 'table',
))
Thanks!
I understood your problem and I don't think Sonata handle this by default.
You have to check the current user roles and either remove contact fields or add readonly or disabled attribute on the contact fields.
Remove Contact Fields
protected function configureFormFields(FormMapper $formMapper)
{
// check if current user has role contact edition
$hasContactRole = $this->getConfigurationPool()->getContainer()->get('security.context')->isGranted('ROLE_SONATA_ADMIN_CONTACT_EDIT'));
if ($hasContactRole) {
$formMapper->add('contact', 'sonata_type_collection',
array(
'label' => 'Contact',
'by_reference' => false,
),
array(
'edit' => 'inline',
'inline' => 'table',
)
);
}
}

CakePHP deep (multiple related models) validation?

I have a model structure that is:
Organization belongsTo Address belongsTo CountryCode
So, Organization has foreign keys: mailing_address_id and physical_address_id
Address has foreign key: country_code_id
In the Organization model, relationship is defined as:
public $belongsTo = array(
'MailingAddress' => array('className'=>'Address', 'foreignKey'=>'mailing_address_id')
, 'PhysicalAddress' => array('className'=>'Address', 'foreignKey'=>'physical_address_id')
);
This appears to be working great - validation is functioning properly, etc.
In the Address model, relationship is defined as:
public $belongsTo = array(
'CountryCode' => array('className'=>'CountryCode', 'foreignKey'=>'country_code_id')
);
In my OrganizationsController, in a function to create a new organization, I'm testing validation using this code:
if($this->Organization->saveAll(
$data, array('validate'=>'only')
)) {
// Validates
$this->DBG('Org validated.');
} else {
// does not validate
$this->DBG('Org NOT NOT NOT validated.'.print_r($this->Organization->invalidFields(),true));
}
The $data array looks like this going into validation.
2015-06-08 21:03:38 Debug: Array
(
[Organization] => Array
(
[name] => Test Organization
)
[MailingAddress] => Array
(
[line1] => 100 Main Street
[line2] =>
[city] => Houston
[state] => TX
[postal_code] => 77002
[CountryCode] => Array
(
[name] => United St
)
)
[PhysicalAddress] => Array
(
[line1] => 100 Main Street
[line2] =>
[city] => Houston
[state] => TX
[postal_code] => 77002
[CountryCode] => Array
(
[name] => United St
)
)
)
The country code SHOULD NOT validate with the rules I have set in the CountryCode model:
public $validate = array(
'name' => array(
'nonemptyRule' => array(
'rule' => 'notEmpty'
,'required' => 'create'
,'message' => 'Must be provided.'
)
,'dupeRule' => array(
'rule' => array('isUnique', array('name','code'), false)
,'message' => 'Duplicate'
)
)
,'code' => array(
'rule' => 'notEmpty'
,'required' => 'create'
,'message' => 'Must be provided.'
)
);
However, validation PASSES on Organization->saveAll.
Also, if I attempt to access the CountryCode model from the OrganizationController, it's not loaded.
As in:
$this->Organization->MailingAddress->CountryCode->invalidate('name','Invalid!');
In that case, I get an error that CountryCode is null.
Any ideas why CountryCode wouldn't be validating or loaded?
Is validation supposed to work two steps away?
It turns out, there IS a deep option when validating (and saving). It's documented here with the saveAll options:
http://book.cakephp.org/2.0/en/models/saving-your-data.html
So, the validation block in the question works perfectly well if you include the deep option like so:
if($this->Organization->saveAll(
$data, array('validate'=>'only', 'deep'=>true)
)) {
// Validates
$this->DBG('Org validated.');
} else {
// does not validate
$this->DBG('Org NOT NOT NOT validated.'.print_r($this->Organization->invalidFields(),true));
}

Resources