toArray(); $result = $this->wpdb->insert( $this->table, [ 'fieldIndex' => $data['fieldIndex'], 'formId' => $data['formId'], 'type' => $data['type'], 'label' => $data['label'], 'required' => (int)$data['required'], 'defaultValue' => $data['defaultValue'], 'placeholder' => $data['placeholder'], 'position' => $data['position'], 'settings' => json_encode([ 'hideLabel' => (int)$data['hideLabel'], 'readOnly' => (int)$data['readOnly'], 'description' => $data['description'], 'requiredMessage' => $data['requiredMessage'], 'cssClasses' => $data['cssClasses'], ]), 'dateCreated' => current_time('mysql') ], ['%s', '%d', '%s', '%s', '%d', '%s', '%s', '%d', '%s', '%s'] ); if ($result === false) { throw new QueryExecutionException( BackendStrings::getExceptionStrings()['unable_to_add_data'] . __CLASS__ ); } return $this->wpdb->insert_id; } /** * Update field in the database * * @param int $id * @param Field $entity * * @return bool * * @throws QueryExecutionException */ public function update(int $id, $entity): bool { $data = $entity->toArray(); $result = $this->wpdb->update( $this->table, [ 'fieldIndex' => $data['fieldIndex'], 'formId' => $data['formId'], 'type' => $data['type'], 'label' => $data['label'], 'required' => (int)$data['required'], 'defaultValue' => $data['defaultValue'], 'placeholder' => $data['placeholder'], 'position' => $data['position'], 'settings' => json_encode([ 'hideLabel' => (int)$data['hideLabel'], 'readOnly' => (int)$data['readOnly'], 'description' => $data['description'], 'requiredMessage' => $data['requiredMessage'], 'cssClasses' => $data['cssClasses'], ]), ], [ 'id' => $data['id'] ], ['%s', '%d', '%s', '%s', '%d', '%s', '%s', '%d', '%s'], ['%d'] ); if ($result === false) { throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['unable_to_save_data'] . __CLASS__); } return $result; } /** * Get fields by form ID * * @param int $id * * @return mixed[] */ public function getFieldsById(int $id): array { $rows = $this->wpdb->get_results( $this->wpdb->prepare( $this->selectQuery() . " WHERE $this->table.formId = %d ORDER BY $this->table.position ASC", $id ), ARRAY_A ); $result = []; foreach ($rows as $row) { $result[] = call_user_func([static::FACTORY, 'create'], $row); } return $result; } }