query('SELECT * FROM de_account_templates'); // returns PDOStatement $existing = $stmt->fetchAll(); // returns the result as an array if (!$existing) { $data = [ [ 'id' => 1, 'name' => 'Basic', ] ]; $table = $this->table('de_account_templates'); $table->insert($data)->save(); $templatedAccountsTable = TableRegistry::getTableLocator()->get('CakeAccounting.DeTemplatedAccounts'); $roots = [ [ 'account_number' => 10000, 'account_template_id' => 1, 'parent_id' => null, 'account_type_code' => DE_ACCOUNT_TYPE_ASSET, 'name' => 'Assets', ], [ 'account_number' => 20000, 'account_template_id' => 1, 'parent_id' => null, 'account_type_code' => DE_ACCOUNT_TYPE_LIABILITY, 'name' => 'Liabilities', ], [ 'account_number' => 30000, 'account_template_id' => 1, 'parent_id' => null, 'account_type_code' => DE_ACCOUNT_TYPE_EQUITY, 'name' => 'Equity', ], [ 'account_number' => 40000, 'account_template_id' => 1, 'parent_id' => null, 'account_type_code' => DE_ACCOUNT_TYPE_REVENUE, 'name' => 'Revenue', ], [ 'account_number' => 50000, 'account_template_id' => 1, 'parent_id' => null, 'account_type_code' => DE_ACCOUNT_TYPE_EXPENSES, 'name' => 'Expenses', ], ]; $added = []; foreach ($roots as $root) { $rootEntity = $templatedAccountsTable->newEntity($root); $saveResultRootEntity = $templatedAccountsTable->save($rootEntity); if (!$saveResultRootEntity) { Log::debug(print_r('$rootEntity->getErrors()', true)); Log::debug(print_r($rootEntity->getErrors(), true)); continue; } $added[$saveResultRootEntity->account_number] = $saveResultRootEntity->id; } $children = [ [ 'account_number' => 11000, 'account_template_id' => 1, 'parent_id' => 10000, 'account_type_code' => DE_ACCOUNT_TYPE_ASSET, 'name' => 'Cash', ], [ 'account_number' => 12000, 'account_template_id' => 1, 'parent_id' => 10000, 'account_type_code' => DE_ACCOUNT_TYPE_ASSET, 'name' => 'Investments', ], [ 'account_number' => 13000, 'account_template_id' => 1, 'parent_id' => 10000, 'account_type_code' => DE_ACCOUNT_TYPE_ASSET, 'name' => 'Accounts Receivable', ], [ 'account_number' => 14000, 'account_template_id' => 1, 'parent_id' => 10000, 'account_type_code' => DE_ACCOUNT_TYPE_ASSET, 'name' => 'Inventory', ], [ 'account_number' => 15000, 'account_template_id' => 1, 'parent_id' => 10000, 'account_type_code' => DE_ACCOUNT_TYPE_ASSET, 'name' => 'Prepaid Expenses', ], [ 'account_number' => 16000, 'account_template_id' => 1, 'parent_id' => 10000, 'account_type_code' => DE_ACCOUNT_TYPE_ASSET, 'name' => 'Property & Equipment', ], // liabilities [ 'account_number' => 21000, 'account_template_id' => 1, 'parent_id' => 20000, 'account_type_code' => DE_ACCOUNT_TYPE_LIABILITY, 'name' => 'Accounts Payable', ], [ 'account_number' => 22000, 'account_template_id' => 1, 'parent_id' => 20000, 'account_type_code' => DE_ACCOUNT_TYPE_LIABILITY, 'name' => 'Accrued Payroll Expenses', ], [ 'account_number' => 23000, 'account_template_id' => 1, 'parent_id' => 20000, 'account_type_code' => DE_ACCOUNT_TYPE_LIABILITY, 'name' => 'Accrued Expenses', ], [ 'account_number' => 24000, 'account_template_id' => 1, 'parent_id' => 20000, 'account_type_code' => DE_ACCOUNT_TYPE_LIABILITY, 'name' => 'Taxes', ], [ 'account_number' => 25000, 'account_template_id' => 1, 'parent_id' => 20000, 'account_type_code' => DE_ACCOUNT_TYPE_LIABILITY, 'name' => 'Long Term Debt', ], // equity [ 'account_number' => 31000, 'account_template_id' => 1, 'parent_id' => 30000, 'account_type_code' => DE_ACCOUNT_TYPE_EQUITY, 'name' => 'Owner\'s Equity', ], [ 'account_number' => 32000, 'account_template_id' => 1, 'parent_id' => 30000, 'account_type_code' => DE_ACCOUNT_TYPE_EQUITY, 'name' => 'Retained Earnings', ], // revenue [ 'account_number' => 41000, 'account_template_id' => 1, 'parent_id' => 40000, 'account_type_code' => DE_ACCOUNT_TYPE_REVENUE, 'name' => 'Sales', ], [ 'account_number' => 41100, 'account_template_id' => 1, 'parent_id' => 41000, 'account_type_code' => DE_ACCOUNT_TYPE_REVENUE, 'name' => 'Product Sales', ], [ 'account_number' => 41200, 'account_template_id' => 1, 'parent_id' => 41000, 'account_type_code' => DE_ACCOUNT_TYPE_REVENUE, 'name' => 'Service Sales', ], // expenses [ 'account_number' => 51000, 'account_template_id' => 1, 'parent_id' => 50000, 'account_type_code' => DE_ACCOUNT_TYPE_EXPENSES, 'name' => 'Payroll Expenses', ], [ 'account_number' => 52000, 'account_template_id' => 1, 'parent_id' => 50000, 'account_type_code' => DE_ACCOUNT_TYPE_EXPENSES, 'name' => 'Rent & Utilities', ], [ 'account_number' => 53000, 'account_template_id' => 1, 'parent_id' => 50000, 'account_type_code' => DE_ACCOUNT_TYPE_EXPENSES, 'name' => 'Marketing', ], [ 'account_number' => 54000, 'account_template_id' => 1, 'parent_id' => 50000, 'account_type_code' => DE_ACCOUNT_TYPE_EXPENSES, 'name' => 'Insurance', ], [ 'account_number' => 55000, 'account_template_id' => 1, 'parent_id' => 50000, 'account_type_code' => DE_ACCOUNT_TYPE_EXPENSES, 'name' => 'Cost of Goods Sold', ], [ 'account_number' => 56000, 'account_template_id' => 1, 'parent_id' => 50000, 'account_type_code' => DE_ACCOUNT_TYPE_EXPENSES, 'name' => 'Other Expenses', ], ]; foreach ($children as $child) { $child['parent_id'] = $added[$child['parent_id']]; $childEntity = $templatedAccountsTable->newEntity($child); $saveResultChildEntity = $templatedAccountsTable->save($childEntity); if (!$saveResultChildEntity) { Log::debug(print_r('$childEntity->getErrors()', true)); Log::debug(print_r($childEntity->getErrors(), true)); continue; } $added[$saveResultChildEntity->account_number] = $saveResultChildEntity->id; } } } }