function getByDepartmentBranchId($id, $where = NULL, $order = NULL) { if ($id == '') { return FALSE; } $dbf = new DepartmentBranchFactory(); $ph = array('id' => $id); $query = ' select a.* from ' . $this->getTable() . ' as a, ' . $dbf->getTable() . ' as b where b.id = a.department_branch_id AND department_branch_id = ? '; $query .= $this->getWhereSQL($where); $query .= $this->getSortSQL($order); $this->rs = $this->db->Execute($query, $ph); return $this; }
function setBranch($ids) { if (is_array($ids) and count($ids) > 0) { //If needed, delete mappings first. $dblf = new DepartmentBranchListFactory(); $dblf->getByDepartmentId($this->getId()); $branch_ids = array(); foreach ($dblf as $department_branch) { $branch_id = $department_branch->getBranch(); Debug::text('Department ID: ' . $department_branch->getDepartment() . ' Branch: ' . $branch_id, __FILE__, __LINE__, __METHOD__, 10); //Delete branches that are not selected. if (!in_array($branch_id, $ids)) { Debug::text('Deleting DepartmentBranch: ' . $branch_id, __FILE__, __LINE__, __METHOD__, 10); $department_branch->Delete(); } else { //Save branch ID's that need to be updated. Debug::text('NOT Deleting DepartmentBranch: ' . $branch_id, __FILE__, __LINE__, __METHOD__, 10); $branch_ids[] = $branch_id; } } //Insert new mappings. $dbf = new DepartmentBranchFactory(); foreach ($ids as $id) { if (!in_array($id, $branch_ids)) { $dbf->setDepartment($this->getId()); $dbf->setBranch($id); if ($this->Validator->isTrue('branch', $dbf->Validator->isValid(), TTi18n::gettext('Branch selection is invalid'))) { $dbf->save(); } } } return TRUE; } return FALSE; }