1

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?

4

0 に答える 0