ModelAdmin with a has_many relationship - php

I'm extending ModelAdmin to manage a DataObject and I have a $has_many relationship that is managed by another DataObject. I'd like to manage this object on another tab but am lost as to how I'd add it. My basic code:
ApplicationAdmin.php:
class Applications extends ModelAdmin {
private static $menu_title = 'Applications';
private static $url_segment = 'apps';
private static $managed_models = array (
'Application'
);
}
Application.php
class Application extends DataObject {
private static $db = array(
'Name' => "varchar"
);
private static $has_many = array(
'Documents' => 'Document',
);
public function getCMSFields() {
$fields = FieldList::create(
TextField::create('Name'),
);
return $fields;
}
}
Document.php
class Document extends DataObject {
private static $db = array(
'Title' => "varchar",
);
private static $has_one = array(
"Document" => "File",
"Application" => "Application"
);
public function getCMSFields () {
$fields = FieldList::create(
TextField::create('Title'),
$doc = new UploadField('Document')
);
$doc->getValidator()->setAllowedExtensions(array('pdf', 'docx'));
return $fields;
}
}
Basically I'd like to manage the documents for this entry under Root.Documents tab.

You can use a GridField to handle the relation between Application and Document, and place that field on its own tab if you wish. Example:
# Application.php
public function getCMSFields() {
$fields = parent::getCMSFields();
$nameField = TextField::create('Name');
$documentsField = GridField::create(
'Documents',
'Documents',
$this->Documents(),
GridFieldConfig_RelationEditor::create()
);
$fields->addFieldToTab(
'Root.Main',
$nameField
);
$fields->addFieldToTab(
'Root.Documents',
$documentsField
);
return $fields;
}
Your code contains a some typos in the Document class (the classname has .php in it, getValidator should be getValidator()), and that class also needs a $has_one = ['Application' => 'Application']; for the relation to work.

Related

has_many and has_one relation SilverStripe

I followed the tutorial on https://www.silverstripe.org/learn/lessons/working-with-data-relationships-has-many?ref=hub to create some featured items for my homepage. But somehow I missed one piece, because I get this error
[Error] Uncaught Exception: No has_one found on class 'HomePageFeatured', the has_many relation from 'HomePage' to 'HomePageFeatured' requires a has_one on 'HomePageFeatured'
HomePage.php
<?php
/**
* Defines the HomePage page type
*/
class HomePage extends Page {
// private static $db = array(
// );
// private static $has_one = array(
// );
private static $has_many = array (
'Featured' => 'HomePageFeatured'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Featured', GridField::create(
'Featured',
'Hervorgehobene Produkte',
$this->Featured(),
GridFieldConfig_RecordEditor::create()
));
return $fields;
}
private static $icon = "themes/hstheme/images/treeicons/home";
}
class HomePage_Controller extends Page_Controller {
}
HomePageFeatured.php
<?php
/**
* Holds the featured items from the Homepage
*/
class HomePageFeatured extends DataObject {
private static $db = array(
'Title' => 'Varchar',
'Description' => 'Text'
);
private static $has_one = array(
'Photo' => 'Image',
'HomePageFeatured' => 'HomePageFeatured'
);
public function getCMSFields() {
$fields = FieldList::create(
TextField::create('Title'),
TextareaField::create('Description'),
$uploader = UploadField::create('Photo')
);
$uploader->setFolderName('featured-photos');
$uploader->getValidator()->setAllowedExtensions(array('png','gif','jpeg','jpg'));
return $fields;
}
}
As I understand, the problem is the $has_one from HomePageFeatured.php. But it has a reference from HomePageFeatured.
HomePageFeatured needs a has_one of 'HomePage'
class HomePageFeatured extends DataObject {
private static $has_one = array(
'Photo' => 'Image',
'Parent' => 'HomePage',
);
}

Silverstripe custom search

I have a product catalog. There are:
Dzial.php - category page
class Dzial extends Page {
...
static $has_many = array(
'Marka' => 'Marka'
);
function getCMSFields() {
...
$gridField = new GridField("Marka", "marki:", $this->Marka(), $gridFieldConfig);
$fields->addFieldToTab("Root.Marki", $gridField);
PodDzial.php - subcategory page
class PodDzial extends Page {
.....
static $has_many = array(
'Kategoria' => 'Kategoria',
'Produkt' => 'Produkt'
);
function getCMSFields() {
....
$gridField = new GridField("Produkt", "Produkty:", $this->Produkt(), $gridFieldConfig);
$fields->addFieldToTab("Root.Produkty", $gridField);
$gridField2 = new GridField("Kategoria", "Kategoria:", $this->Kategoria(), $gridFieldConfig);
$fields->addFieldToTab("Root.Kategorie", $gridField2);
Produkt.php - product - DataObjects which belong to PodDzial.php
class Produkt extends DataObject {
.....
static $has_one = array (
'PodDzial' => 'PodDzial',
'Marka' => 'Marka',
'Kategoria' => 'Kategoria'
);
Kategoria.php - type of product- which belong to PodDzial (has_one) and to Produkt (has_many)
class Kategoria extends DataObject {
....
private static $has_one = array( 'PodDzial' => 'PodDzial' );
private static $has_many = array ('Produkt' => 'Produkt' );
Marka.php - brand of product- which belong to Dzial (has_one) and to Produkt (has_many)
class Marka extends DataObject {
...
private static $has_one = array( 'Dzial' => 'Dzial' );
private static $has_many = array ('Produkt' => 'Produkt');
I work on custom search for Produkt's on PodDzial page.
The code which doesn't work properly:
class Produkt extends DataObject {
...
public function getDefaultSearchContext() {
$fields = new FieldList(
DropdownField::create(
'MarkaID',
'Marka',
Marka::get()->map('ID','Title')
)->setEmptyString('--Wybierz marke--') ,
DropdownField::create(
'KategoriaID',
'Kategoria',
Kategoria::get()->filter(array('PodDzialID' => $this->PodDzialID))->map('ID','Title')
)->setEmptyString('--Wybierz kategorie--')
);
$filters = array(
'MarkaID' => new ExactMatchFilter('MarkaID'),
'KategoriaID' => new ExactMatchFilter('KategoriaID')
);
return new SearchContext(
$this->class,
$fields,
$filters
);
}
Both dropdown fields are working bad. They should list Kategoria's only from its PodDzial holder and Marka's from parent Dzial. Marka's field shows all Marka even from other categories now. Kategoria show nothing.
I use those same dropdowns on fieldlist. Kategoria's work ok. It shows only data from given PodDzial. Markas are on parent holder (Dzial) and I cant access it there too :
class Produkt extends DataObject {
....
$xxx = 'LEFT JOIN `SiteTree` ON `SiteTree`.ParentID=`Marka`.DzialID';
function getCMSFields(){
$fields = FieldList::create(
TabSet::create("Root",
Tab::create("Main",
....
DropdownField::create(
'MarkaID',
'Marka',
Marka::get()->filter($xxx, array('DzialID' => 'ParentID'))->map('ID','Title')
)->setEmptyString('-- None --'),
DropdownField::create(
'KategoriaID',
'Kategoria',
Kategoria::get()->filter(array('PodDzialID' => $this->PodDzialID))->map('ID','Title')
)->setEmptyString('-- None --')
Can anyone help?

Saving to a many_many relationship with a ListBoxField

I've created a DataObject called Service and I've got a many_many relationship to RelatedServices as follows:
class Service extends DataObject {
private static $db = array (
'Name' => 'Varchar',
'Description' => 'Varchar',
);
private static $many_many = array (
'RelatedServices' => 'RelatedService'
);
public function getCMSFields() {
$fields = FieldList::create(TabSet::create('Root'));
$services = $this->get()->where("\"Service\".\"Name\" != '$this->Name'")->map('ID', 'Name')->toArray();
$fields->addFieldsToTab('Root.Main', array(
TextField::create('Name'),
TextField::create('Description'),
ListBoxField::create('RelatedServices', 'Related services')->setMultiple(true)->setSource($services)
));
return $fields;
}
}
and:
class RelatedService extends DataObject {
private static $db = array (
'Name' => 'Varchar',
);
private static $belongs_many_many = array (
'RelatedServices' => 'RelatedService'
);
}
This is being used in a ModelAdmin and the service works right including the related services text area, however it doesn't save. I did it previously that it was in a seperate tab in the CMS and had RelatedService have and admin section which looking through the DB looked like it worked however I thought it was an unnecessary section so tried to make it all in one and now no longer saves to the DB.
You don't need the 'RelatedService' class, as you are referencing to the class you are working in. So relating to the class 'Service' itself would make more sense.
The reason why your code won't work is because you have got your relations mixed up.
class Service extends DataObject{
private static $db = array (
'Name' => 'Varchar',
'Description' => 'Varchar',
);
private static $many_many = array (
'RelatedServices' => 'Service'
);
private static $belongs_many_many = array (
'ParentServices' => 'Service'
);
public function getCMSFields() {
$fields = FieldList::create(TabSet::create('Root'));
$services = $this->get()->where("\"Service\".\"Name\" != '$this->Name'")->map('ID', 'Name')->toArray();
$fields->addFieldsToTab('Root.Main', array(
TextField::create('Name'),
TextField::create('Description'),
ListBoxField::create('RelatedServices', 'Related services')->setMultiple(true)->setSource($services)
));
return $fields;
}
}

SilverStripe gridField Entries not visible for normal backend user

I have two user groups Administrator and Inhaltsautoren
My LandingPage has a Tab Teaser with a gridField. The normal user can not see the entries and i dont know why?
I cant find something for setting the permissions for Inhaltsautoren. Has someone an idea why there are no entries in the gridField?
Teaser.php
<?php
class Teaser extends DataObject {
private static $db = array (
'Title' => 'Varchar',
'Description' => 'HTMLText'
);
private static $has_one = array (
'Photo' => 'Image',
'Link' => 'Link'
);
private static $many_many = array(
'Tags' => 'Tag'
);
private static $summary_fields = array (
'GridThumbnail' => '',
'Title' => 'Titel',
'Description' => 'Beschreibung'
);
public function getGridThumbnail() {
if($this->Photo()->exists()) {
return $this->Photo()->SetWidth(100);
}
return "(no image)";
}
public function getCMSFields() {
$fields = FieldList::create(
TextField::create('Title'),
$tags = TagField::create('Tags','Tags',Tag::get(),$this->Tags()),
HTMLEditorField::create('Description', 'Beschreibung'),
LinkField::create('LinkID', 'Weiterleitung'),
$uploader = UploadField::create('Photo')
);
$tags->setShouldLazyLoad(true); // tags should be lazy loaded
$tags->setCanCreate(true); // new tag DataObjects can be created
$uploader->setFolderName('teaser');
$uploader->getValidator()->setAllowedExtensions(array('png','jpeg','jpg'));
return $fields;
}
}
and my LadingPage.php
$fields->addFieldToTab('Root.Teaser', $gridField = GridField::create(
'Teasers',
'Landing Page Teaser',
$this->Teasers(),
GridFieldConfig_RecordEditor::create()
));
$gridField->getConfig()->getComponentByType("GridFieldDataColumns")->setFieldCasting(array("Description"=>"HTMLText->BigSummary"));
Use canView() on your dataobject and check inside this function if your user is allowed to see this object or not.
public function canView($member = null) {
return Permission::check('ADMIN', 'any');
}

Silverstripe admin: "Has one" dropdown converts to ordinary input field after import

I am having some problems with the admin of Silverstripe. I defined a database model (see class definitions below), and after I do a dev/build everything is looking as expected. When I try to add a new "package" all the "has one" fields are there with a drop down (see screen shot 1). I also built an importer which imports these packages. When run, everything is looking fine, except when you open a package. Then 'Festival' is correctly coupled. You can see the name, and you can select the drop down. "Troupe", on the other hand, has mysteriously converted to an input field that only shows the id of the record in the other table (See screen shot 2).
Does anyone know what is happening here? Is there something that triggers this behaviour that I am unaware off? Is there something wrong with my code (yes, but related to this problem? ;-))? I have checked the structure of the tables, and there is nothing suspicious there...
Before:
After:
Package.php
class Package extends DataObject {
public static $db = array(
'Number' => 'Int',
'Title' => 'Varchar(255)',
'Description' => 'HTMLText',
'Credits' => 'HTMLText',
);
public static $has_many = array(
'Events' => 'Event',
);
public static $many_many = array(
'Genres' => 'Genre',
);
public static $has_one = array(
'Festival' => 'Festival',
'Troupe' => 'Troupe',
);
}
class PackageAdmin extends ModelAdmin {
public static $managed_models = array('Package'); // Can manage multiple models
static $url_segment = 'packages'; // Linked as /admin/packages/
static $menu_title = 'Packages';
}
Troupe.php
class Troupe extends DataObject {
public static $db = array(
"Name" => "Varchar(255)",
"Description" => "HTMLText",
"Url" => "Varchar(255)",
);
public static $has_many = array(
'Packages' => 'Package.Troupe',
);
}
class TroupeAdmin extends ModelAdmin {
public static $managed_models = array('Troupe','Package'); // Can manage multiple models
static $url_segment = 'troupes'; // Linked as /admin/troupes/
static $menu_title = 'Troupes';
}
Festival.php
class Festival extends DataObject {
public static $db = array(
'Name' => 'Varchar(255)',
'Description' => 'HTMLText'
);
public static $has_many = array(
'Packages' => 'Package.Festival'
);
}
class FestivalAdmin extends ModelAdmin {
public static $managed_models = array('Festival','Package'); // Can manage multiple models
static $url_segment = 'festivals'; // Linked as /admin/festivals/
static $menu_title = 'Festivals';
}
You probably shouldn't only rely on the admin scaffolding, and use getCMSFields on your DataObjects to customize what happen in the CMS. In you case, a simple replace of the Troupe dropdown could work, adding this to your Package class:
function getCMSFields()
{
$fields = parent::getCMSFields();
$troupeList = Troupe::get()->map()->toArray();
$troupeSelect = DropdownField::create('TroupeID', 'Troupe')->setSource($troupeList);
$fields->replaceField('TroupeID', $troupeSelect);
return $fields;
}
This is quite minimalist and a lot more could me customised.

Categories