/** * The "C" in CRUD * * @param Model $model * @param array $fields containing the field names * @param array $values containing the fields' values * @return true on success, false on error */ function create(&$model, $fields = null, $values = null) { $basedn = $this->config['basedn']; $key = $model->primaryKey; $table = $model->useTable; $fieldsData = array(); $id = null; $objectclasses = null; if ($fields == null) { unset($fields, $values); $fields = array_keys($model->data); $values = array_values($model->data); } $count = count($fields); for ($i = 0; $i < $count; $i++) { if ($fields[$i] == $key) { $id = $values[$i]; } elseif ($fields[$i] == 'cn') { $cn = $values[$i]; } $fieldsData[$fields[$i]] = $values[$i]; } //Lets make our DN, this is made from the useTable & basedn + primary key. Logically this corelate to LDAP if (isset($table) && preg_match('/=/', $table)) { $table = $table . ', '; } else { $table = ''; } if (isset($key) && !empty($key)) { $key = "{$key}={$id}, "; } else { //Almost everything has a cn, this is a good fall back. $key = "cn={$cn}, "; } $dn = $key . $table . $basedn; $res = @ldap_add($this->database, $dn, $fieldsData); // Add the entry if ($res) { $model->setInsertID($id); $model->id = $id; return true; } else { $this->log("Failed to add ldap entry: dn:{$dn}\nData:" . print_r($fieldsData, true) . "\n" . ldap_error($this->database), 'ldap.error'); $model->onError(); return false; } }
/** * The "U" in CRUD * This could be collapsed under create, for now it's separate for better debugging * It's important to note that edit requires a FileMaker -recid that should be * passed as a hidden form field * * @param Model $model * @param array $fields * @param array $values * @param mixed $conditions * @return array */ function update(&$model, $fields = array(), $values = null, $conditions = null) { // get connection parameters $fm_database = empty($model->fmDatabaseName) ? $this->config['database'] : $model->fmDatabaseName; $fm_layout = empty($model->defaultLayout) ? $this->config['defaultLayout'] : $model->defaultLayout; if (!empty($model->id)) { // set basic connection data $this->connection->SetDBData($fm_database, $fm_layout); // **1 here we remove the primary key field if it's marked as readonly // other fields can be removed by the controller, but cake requires // the primary key to be included in the query if it's to consider // the action an edit foreach ($fields as $index => $field) { if (isset($model->primaryKeyReadOnly) && $field == $model->primaryKey) { unset($fields[$index]); unset($values[$index]); } } // ensure that a recid is passed if (!in_array('-recid', $fields)) { array_push($fields, '-recid'); array_push($values, $model->getId()); } // there must be a -recid field passed in here for the edit to work // could be passed in hidden form field foreach ($fields as $index => $field) { $this->connection->AddDBParam($field, $values[$index]); } // perform edit $return = $this->connection->FMEdit(); if (!$this->handleFXResult($return, $model->name, 'update')) { return FALSE; } if ($return['errorCode'] != 0) { return false; } else { foreach ($return['data'] as $recmodid => $returnedModel) { $recmodid_Ary = explode('.', $recmodid); $model->id = $recmodid_Ary[0]; $model->setInsertID($recmodid_Ary[0]); } return true; } } else { return false; } }
/** * Create Data * * @param Model $model Model Instance * @param array $fields Field data * @param array $values Save data * @return boolean Insert result * @access public */ public function create(&$model, $fields = null, $values = null) { if ($fields !== null && $values !== null) { $data = array_combine($fields, $values); } else { $data = $model->data; } $result = $this->_db->selectCollection($model->table)->insert($data, true); if ($result['ok'] === 1.0) { $id = is_object($data['_id']) ? $data['_id']->__toString() : null; $model->setInsertID($id); $model->id = $id; return true; } return false; }