1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php

class DOMTransaction extends DataObject {

    static $db = array (
    	'Date' => 'SS_DateTime',
    	'Name' => 'VarChar(255)',
    	'Total' => 'Decimal(9,2)'
    );

	static $has_many = array(
		'Items' => 'DOMTransactionItem'
	);

	static $summary_fields = array(
		'ID',
		'Date',
    	'Name',
    	'Total'
    );

	static $searchable_fields = array(
		'ID'
	);
	

	public function canCreate() {return false;}
	public function canView() {return true;}
	public function canEdit() {return false;}
	public function canDelete() {return false;}
	public function getCMSFields()
    {
        $fields = parent::getCMSFields();

        $fields->removeByName('Items');
		$manager = new DataObjectManager(
			$this, // Controller
			'Items', // Source name
			'DOMTransactionItem', // Source class
			array(
				'Name' => 'Name'
				, 'Count' => 'Quantity'
				, 'Price' => 'Price'
				, 'Subtotal' => 'Subtotal'
			), // Headings 
			'getCMSFields_forPopup'
			// Filter clause
			// Sort clause
			// Join clause
		);
		$manager->setPermissions(array('view'));
        $fields->addFieldToTab('Root.Main', $manager);

        return $fields;
      }

	function requireDefaultRecords() {
		parent::requireDefaultRecords();
		$tx = DataObject::get_one('DOMTransaction');
		
		if (!$tx) {
			$tx = new DOMTransaction();
			$tx->Date = SS_Datetime::now();
			$tx->Name = 'Test Transaction';
			$tx->Total = 10;
			$tx->write();
			
			$txi = new DOMTransactionItem();
			$txi->TransactionID = $tx->ID;
			$txi->Name = 'Test Product';
			$txi->Count = 4;
			$txi->Price = 2.5;
			$txi->write();
		}
     } 
}

class DOMTransactionItem extends DataObject {

    static $db = array (
    	'Name' => 'Text',
    	'Count' => 'Int', 
    	'Price' => 'Decimal(9,2)',
    );
	static $has_one = array(
		'Transaction' => 'DOMTransaction'
	);

	static $summary_fields = array(
    	'Name',
    	'Count', 
    	'Price'
    );

}

class DOMModelAdmin extends ModelAdmin {

  public static $managed_models = array(   //since 2.3.2
      'DOMTransaction' => array('title' => "Transactions")
   );

	public $showImportForm = false;

	static $url_segment = 'domtest';
	static $menu_title = 'DOM Test';
}