Consider the following scenario for API testing
Given I am using the API Service
When I send the <request> as <method> to <endpoint> endpoint with <key> having value <value>
Then The response status code is 200
Examples:
| request | method | endpoint | key | value |
| "All Keys" | "POST" | "endpointName" | "numericField" | 15 |
| "All Keys" | "POST" | "endpointName" | "numericField" | 15.12345 |
The above example creates a request with the specified parameters.
My problem is that while the integer (15) value is passed to the function accordingly, the float (15.12345) is converted into a string ("15.12345"). This happens straight as the function is called; it is not modified later on during another step.
Is there a way to keep the float value from turning into a string?
As requested, the send request step method is:
$data = $this->fulfilmentOptions->getDataValue($request);
$uri = $this->getMinkParameter('base_url') . $this->setEndpoint($endpoint);
array_walk_recursive($data, function(&$item, $originalKey) use ($key, $value) {
if ($originalKey === $key) {
$item = $value;
}
});
try {
$this->response = $this->client->request($method, $uri, [
'headers' => $this->fulfilmentOptions->getDataValue('CreateOrder API Headers'),
'body' => json_encode($data)
]);
} catch (\GuzzleHttp\Exception\RequestException $e) {
$this->response = $e->getResponse();
}
$this->responseContent = $this->response->getBody()->getContents();
One way of fixing the issue is to use floatval method for 'value' key to make sure you get the right type.
Related
When I use get_field('repeater_name') it returns a string that contains the a number which is the count of the repeater while it should return array of sub-fields, this issue have been reported repeatedly before but none of them provided a stable fix to this, the only way is to edit the post and hit the update button and it will re-generate the repeater fields, but in my case, I have 19k posts and it's not possible to edit each post.
Also have_rows('repeater_name') returns false as expected and the real field data (get_post_meta($post_id, "field_{$field_key}", true) returns NULL
I would appreciate any help or any automated script that regenerates the repeater field keys and populate the values of all posts.
This issue is reported previously in here, here and here and none came with a real fix.
EDIT:
I spotted the problem, the problem is in wp_postmeta table, the wrong field keys are stored:
wp_postmeta table
| meta_key | meta_value |
| ------------- |:-------------:|
| _repeater_name | field_XXXXXXX |
| _repeater_0_subfield | field_ABCDEFG |
| _repeater_0_subfield2 | field_HIJKLMN |
The same field keys should match in wp_posts table, but they don't (maybe due to data migration or whatever reason):
| post_excerpt | post_name | post_type |
| ---------------| ------------- |-----------|
| repeater_name | field_YYYYYYY | acf-field |
| subfield | field_BBBBBBB | acf-field |
| subfield2 | field_CCCCCCC | acf-field |
I think if there's a way to sync field keys in the database, that will fix the problem
I made a solution for this, might not be the exact solution for this problem but it worked out for me, I wrote this fix to rebuild faulty fields, it will rebuild only fields that have this problem:
/**
* #param string $name
* #param string[] $keys
* #param int|null|\WP_Post $post
*/
function dw_build_repeater($name, $keys = [], $post = null) {
$post = get_post($post);
$repeater = get_field($name, $post->ID);
$arr = [];
if (is_array($repeater)) {
return $repeater;
}
if (!$repeater || !is_numeric($repeater)) {
return $arr;
}
for ($i = 0; $i < intval($repeater); $i++) {
foreach ($keys as $key) {
$arr[$i][$key] = get_post_meta($post->ID, $name . "_" . $i . "_{$key}");
}
}
update_field($name, $arr, $post->ID);
return $arr;
}
so whenever i want to call a repeater i simply do this:
if (
have_rows('repeater_name')
|| dw_build_repeater('repeater_name', ['subfield1', 'subfield2', 'subfield3'])
) {
while (have_rows('repeater_name')) {
the_row();
// And the rest
}
}
// OR simply with a foreach loop
foereach (dw_build_repeater('repeater_name', ['subfield1', 'subfield2', 'subfield3']) as $row) {
echo $row['subfield1'] ?? '';
}
I haven't tested it with nested repeaters but i suppose it might not be working with nested repeaters.
I've been having this issue when trying to access repeaters added to options pages through the PHP API. Specifically, in my case I was trying to get the repeater data during the acf/load_field filter. It didn't matter whether the hook was registered during init or acf_init.
I discovered that, at least in this case, you always have to call acf_enable_filter( 'local' ) before get_field() to get the data array instead of the item count.
while playing with PHPExcel I came across some questions how properly handle validation/and inserting values into a database. I do not need any codes, just the general concept how to do it.
Firstly I iterate through first row to check if the columns are matching the given one ( if it fits the schema ).
On the next step, I get the rows and meanwhile its beeing validated row/column wise. If the type doesn't match I will get an error.
While validating the row, I need to get the Worker name and convert it to id get_worker_id().
Question number #1.
Is such solution a good practice? It will produce upto 100 queries. Foreach row - 1.
Question number #2
I also need to validate the rows once again, I would take the worker_id, the F and G column to check if such record isn't present in the database. I would simply introduce a function similar to get_worker_id() but it would return true/false if entry exists.
But again is this the proper way of doing it? By raw calculations my method would produce 100 selects ( get_worker_id ), 100 selects ( validate if exists ), 100 insert ( if all is ok ).
Im not sure if I am doing it properly. Could you hit me up with some advices?
Thanks in forwards.
Model for handling the xlsx file.
class Gratyfikant_model extends CI_Model {
private $_limit = 100;
const columns = array(
'A' => "Z",
'B' => "KS",
'C' => "G",
'D' => "S",
'E' => "Numer",
'F' => "Miesiąc", // required
'G' => "Data wypłaty", // required
'H' => "Pracownik", // required
'I' => "Brutto duże", // required
'J' => "ZUS pracownik", // required
'K' => "ZUS pracodawca", // required
'L' => "Do wypłaty", // required
'M' => "Obciążenie", // required
'N' => "FW");
const validators = array(
'F' => 'date',
'G' => 'date',
'H' => 'string',
'I' => 'float',
'J' => 'float',
'K' => 'float',
'L' => 'float',
'M' => 'float',
);
const validators_errors = array(
'float' => "Wartość nie jest liczbą",
'string' => "Wartość nie jest poprawna",
'date' => "Wartość nie jest datą"
);
protected $_required = array(
'H', 'I', 'J', 'K', 'L', 'M'
);
private $_sheet = array();
private $_sheet_pracownicy = array();
private $_agregacja = array();
protected $_invalid_rows = array();
public function __construct() {
parent::__construct();
}
public function read_data(array $dane) {
if (count($dane) > $this->_limit) {
throw new Exception('Limit wierszy to ' . $this->_limit);
}
$this->_sheet = $dane;
return $this;
}
public function column_validation() {
foreach ($this->_required as $r) {
if (!isset($this->_sheet[1][$r]) || $this->_sheet[1][$r] != self::columns[$r] || !array_key_exists($r, $this->_sheet[1])
) {
throw new Exception('Kolumna - ' . $r . ' - Wartość nagłówka nie pasuje do szablonu, powinno być ' . self::columns[$r]);
}
}
return $this;
}
function validateDate($date) {
$d = DateTime::createFromFormat('Y-m-d', $date);
return $d && $d->format('Y-m-d') === $date;
}
private function row_validation($k, $a, $v, $f) {
switch ($v) {
case "date":
$cellval = $this->validateDate(PHPExcel_Style_NumberFormat::toFormattedString($f, PHPExcel_Style_NumberFormat::FORMAT_DATE_YMD));
break;
case "float":
$cellval = is_float($f);
break;
case "string":
$cellval = is_string($f);
break;
default:
break;
}
if (!$cellval) {
$this->_invalid_rows[$a][$k] = $v;
}
}
public function get_sheet_data() {
$dane = $this->_sheet;
unset($dane[1]); // remove first col
$zus_pracownik = 0;
$zus_pracodawca = 0;
$zus_lacznie = 0;
$do_wyplaty = 0;
$obciazenie = 0;
$brutto = 0;
foreach ($dane as $a => $d) {
foreach (self::validators as $k => $v) {
echo $this->row_validation($k, $a, $v, $d[$k]);
}
if (!is_null($d["H"]) && !empty($d["H"])) {
// $this->_sheet_pracownicy[$d["H"]]["numer"] = PHPExcel_Style_NumberFormat::toFormattedString($d["E"], PHPExcel_Style_NumberFormat::FORMAT_DATE_DDMMYYYY);
$this->_sheet_pracownicy[] = array(
"pracownik" => $d["H"],
"miesiac" => PHPExcel_Style_NumberFormat::toFormattedString($d["F"], PHPExcel_Style_NumberFormat::FORMAT_DATE_YMD),
"data_wyplaty" => PHPExcel_Style_NumberFormat::toFormattedString($d["G"], PHPExcel_Style_NumberFormat::FORMAT_DATE_YMD),
"zus_pracownik" => $d["J"],
"zus_pracodawca" => $d["K"],
"zus_lacznie" => bcadd($d["K"], $d["J"]),
"do_wyplaty" => $d["L"],
"obciazenie" => $d["M"],
"brutto" => $d["I"],
"id_prac" => $this->get_worker_id($d["H"]));
$zus_pracownik = bcadd($zus_pracownik, $d["J"]);
$zus_pracodawca = bcadd($zus_pracodawca, $d["K"]);
$zus_lacznie = bcadd($zus_lacznie, bcadd($d["K"], $d["J"]));
$do_wyplaty = bcadd($do_wyplaty, $d["L"]);
$obciazenie = bcadd($obciazenie, $d["M"]);
$brutto = bcadd($brutto, $d["I"]);
}
}
$this->_agregacja = array(
"zus_pracownik" => $zus_pracownik,
"zus_pracodawca" => $zus_pracodawca,
"zus_lacznie" => $zus_lacznie,
"do_wyplaty" => $do_wyplaty,
"obciazenie" => $obciazenie,
"brutto" => $brutto
);
return $this;
}
public function display_result() {
if (empty($this->_invalid_rows)) {
return array(
"wartosci" => $this->_sheet_pracownicy,
"agregacja" => $this->_agregacja
);
}
}
public function display_errors() {
foreach ($this->_invalid_rows as $k => $a) {
foreach ($a as $key => $value) {
throw new Exception('Pole ' . $key . '' . $k . ' ' . self::validators_errors[$value]);
}
}
return $this;
}
public function get_worker_id($getAd) {
$this->db->select('id_pracownika as id')
->from('pracownicy')
->like('CONCAT( imie, \' \', nazwisko )', $getAd)
->or_like('CONCAT( nazwisko, \' \', imie )', $getAd);
$query = $this->db->get();
$result = $query->result_array();
if (isset($result[0]["id"])) {
return $result[0]["id"];
} else {
throw new Exception('Nie odnaleziono ' . $getAd . ' w bazie danych, proszę dodać pracownika a następnie ponownie wczytać plik');
}
}
}
Display
try {
$data['s'] = $this->gm
->read_data($sheetData)
->column_validation()
->get_sheet_data()
->display_errors()
->display_result();
} catch (Exception $e) {
$data['ex'] = $e->getMessage();
}
XLSX file example
+---+---------------+---+---+------------+---------+--------------+-----------+-------------+---------------+----------------+------------+------------+--------+
| Z | KS | G | S | Numer | Miesiąc | Data wypłaty | Pracownik | Brutto duże | ZUS pracownik | ZUS pracodawca | Do wypłaty | Obciążenie | FW |
+---+---------------+---+---+------------+---------+--------------+-----------+-------------+---------------+----------------+------------+------------+--------+
| | nieprzekazany | G | | 03.08.2017 | sie.17 | 08.09.2017 | Worker1 | 2000 | 274,2 | 392,2 | 1459,48 | 2392,2 | (brak) |
| | nieprzekazany | G | | 03.08.2017 | sie.17 | 08.09.2017 | Worker2 | 1000 | 137,1 | 171,6 | 768,24 | 1171,6 | (brak) |
| | nieprzekazany | G | | 03.08.2017 | sie.17 | 08.09.2017 | Worker3 | 2000 | 274,2 | 392,2 | 1413,88 | 2392,2 | (brak) |
| | nieprzekazany | G | | 03.08.2017 | sie.17 | 08.09.2017 | Worker4 | 2000 | 274,2 | 392,2 | 1418,88 | 2392,2 | (brak) |
+---+---------------+---+---+------------+---------+--------------+-----------+-------------+---------------+----------------+------------+------------+--------+
This really depends on the scale of your application and how frequently this Excel file will be imported. For example, if your application receives little to no traffic then running several queries per line is not the end of the world. If you already have the server and database setup and running then you might as well make use of them. Conversely, if your application is under constant heavy load then trying to minimize the amount of queries you run may be a good idea.
Option 1
If your application is small and/or doesn't get much traffic then don't worry about the ~300 queries you need to make. MySQL is not fragile and if you have indexed your data well your queries will be very fast.
Option 2
Move to querying the data you need first and storing it in memory so you can perform your logic checks in PHP.
This means for Question 1 you should get all of your workers in one query and then build a lookup array in PHP.
Here is a very rough example:
// Get all workers
SELECT worker_name, worker_id FROM workers;
// Build a lookup array from the query results
$worker_array = array(
'Worker1' => 1,
'Worker2' => 2,
...
);
// Then as you loop each row check if the work is in your lookup array
if ( ! isset($worker_array[$excel_row['worker_name']])) {
// do something
}
Likewise for Question 2 you could get your unique data samples in one query (you don't need the entire record, just the unique fields). However, this may present a problem if you have a lot of unique data samples.
Option 3
Create a temporary table in MySQL and import your Excel data without performing any logic checks. Then you can perform your logic checks entirely in SQL.
Here is a very rough example without knowing anything about your data structure:
-- Get all records in the Excel data that match unique data samples
SELECT
*
FROM
temporary_table tt
JOIN
workers w
ON w.worker_name=tt.worker_name
JOIN
data d
ON d.worker_id=w.worker_id
AND d.col_f=tt.col_f
AND d.col_g=tt.col_g
If there are no issues with the data then you can perform an INSERT from your temporary table into your data table. This limits your queries to the initial insert (you can batch this for better performance as well), the data check and the insert from temp to real data.
Recap
It all comes down to your application. If you can get away with doing Option 1 and you've already got it implemented then that's fine for now. You don't need to over optimize things if you don't see this application growing like crazy.
However, if you are worried about scale and growth then I'd personally look at implementing Option 3.
There are multiple concerns here:
Split import into stages.
Validate headers. (Break importing if errors found) 2) Iterate
over each row.
Validate row.
Import if valid.
Log error if any.
Stop processing file if all rows where processed or go back to 2.
As to weather you need some chunking it depend on how much time and memory your script is consuming. If you need it, it's as simple as reading X rows to memory and then processing it. At extreme you can load each record separately.
If you do not need it just load it all to array.
chunking - consuming up to X rows in a single iteration, then clearing memory then consuming next chunk...
100 queries just doesn't sound right to me for a single php instance.
https://secure.phabricator.com/book/phabcontrib/article/n_plus_one/
Or just search for n plus 1 query problem
I have a problem using the usort function. My array has the following model :
Fusion
|
| Array_1
| |
| | Array_1_1
| | | clock => "08:08"
| | | //Other fields
| | |
| |
| | Array_1_2
| | | clock => "04:51"
| | | //Other fields
| | |
|
| Array_2
| ...
I want to sort the arrays in Array_X basing on the 'clock' field. I made this code, basing on the PHP documentation of usort (4th example) :
foreach ($fusion as $fus){
usort($fus,function ($key = 'clock'){
return function ($a,$b) use ($key){
return strnatcmp($a[$key],$b[$key]);
};
});
}
... but it returns the following exception :
Object of class Closure could not be converted to int
Do you have any idea ? Thanks for your help.
You're trying to pass the following as a callback (2nd argument of usort)
function ($key = 'clock'){
return function ($a,$b) use ($key){
return strnatcmp($a[$key],$b[$key]);
};
}
But the documentation clearly specifies that
The comparison function must return an integer [...]
However, your callback returns another closure. This is why you get a Closure to int convertion Exception.
I believe what you're trying to do is to execute the inner-closure to get the final callback using the right values. I'd say this is useless because, unlike in Javascript, php closures are opt-in, not all-in, which means their context must be included with use the way you did for $key in the outer-closure.
You could simply do:
$key = 'clock';
foreach ($fusion as $fus){
usort($fus,function ($a,$b) use ($key){
return strnatcmp($a[$key],$b[$key]);
});
}
And if $key comes from some other kind of context you may just have to adjust the variable assignment.
Convert your object into an array using get_object_vars($object) first.
If the object that you are getting is generated by a different function, then read documentation for that function and find a way for it to return an array instead of object.
Examples and documentation can be found here http://php.net/manual/en/function.get-object-vars.php
I have a Document Model which represents a Document (name, description etc). I then have a DocumentData Model which represents the data of a Document. A Document has One to Many DocumentData,
So to create a Document I have a form. Lets say that my form has a text input for Document Owner. Within my DocumentData table, the label documentOwner is set as the key and the inputted data is set as the value. So with multiple form elements, my DocumentData table might look like this
document_data
id | documentId | key | value |
----------------------------------------------
1 | 1 | clientName | Google |
----------------------------------------------
2 | 1 | projectName | Analytics |
----------------------------------------------
3 | 1 | Contact | Mr Sharp |
----------------------------------------------
4 | 1 | startDate | 29/12/2016 |
----------------------------------------------
The Document name and description for the Document table is created using hidden fields within the view of the Document.
So I can create Documents without a problem. I am having a problem with my update function though. So far I have the following
public function update(Request $request, Project $project, $id)
{
$document = $project->document()->where('id', '=', $id)->first();
$docData = $document->documentData()->get();
$input = $request->all();
foreach($docData as $orgData) {
foreach($input as $key => $value) {
if($key !== "filePath" && $key !== "documentType" && $key !== "documentTypeDesc") {
if($key == $orgData->key) {
$orgData->value = $value;
$orgData->update();
}
}
}
}
return View::make($document->name.'Doc.edit', compact('project', 'document'));
}
So firstly I get the Document I am working on and store it in $document. I then get the DocumentData for this Document and store it in $docData. $docData is a collection containing my key-value pairings for a Document.
I then loop both the $docData and the inputted data and where the keys match, I reset its updated value.
However, at the moment, it updates everything to the last inputted data field. I am not sure where else I can perform the update operation, but I only need it to update the row it is referring too, not the whole thing.
How could I go about doing this?
Thanks
I've cleaned up the code a little, and made a few implementation changes. This code should work, so let us know if it doesn't, and if not, what failed.
public function update(Request $request, Project $project, $id)
{
// you can use eager loading and find on the relationship query
$document = $project->document()->with('documentData')->find($id);
// you can just access the relationship attribute, no need for the query
$docData = $document->documentData;
// gets all input, except the keys specified
$input = $request->except(['filePath', 'documentType', 'documentTypeDesc']);
foreach($docData as $orgData) {
// check if the input has a key that matches the docdata key
if (array_key_exists($orgData->key, $input)) {
// update the value
$orgData->value = $input[$orgData->key];
// use save, not update
$orgData->save();
}
}
return View::make($document->name.'Doc.edit', compact('project', 'document'));
}
I have two very similar classes. Lets say Class A and Class B.
+---------------+ +---------------+
| Class A | | Class B |
|---------------| |---------------|
| Name | | Name |
| ZIP | | ZIP |
| TelPhone | | TelPhone |
| | | MobilePhone |
+---------------+ +---------------+
I want to compare them in the values for all common attributes.
Here's a way I tried it, but doing it for all attributes (got more than only 3 attributes) looks like a overkill for me:
$differences = array();
if($classA->getName() != $classB->getName()) {
array_push(
$differences,
array('name' => array(
'classA' => $classA->getName(),
'classB' => $classB->getName()
)
));
}
// and the same code for every attribute....
What's the best approach here?
Additional to the handiwork, it is also not automatically updated if the classes are getting altered. For example if Class A gets also a MobilePhone attribute.
Please don't tell me, that I should do some Polymorphism, it's just an example to clarify.
I'm interested in the difference, so not only the attributes, also the values itself inside the attributes.
thanks
It's not the sexiest thing (because of the substring to getters.., but I can't go with properties, I think sym, but I got it:
// getting an array with all getter methods of a class
private function getGettersOf($object) {
$methodArray = array();
$reflection = new ReflectionClass($object);
$methods = $reflection->getMethods();
foreach ($methods as $method) {
// nur getter
if(strtolower(substr($method->getName(), 0, 3)) == "get") {
array_push($methodArray, $method->getName());
}
}
return $methodArray;
}
// getting an array with all differences
public function getDifferences($classA, $classB) {
// get list of attributes (getter methods) in commond
$classAMethods = $this->getGettersOf($classA);
$classBMethods = $this->getGettersOf($classB);
$intersection = array_intersect($classAMethods, $classBMethods);
$differences = array();
// iterate over all methods and check for the values of the reflected methods
foreach($intersection as $method) {
$refMethodClassB = new ReflectionMethod($classB, $method);
$refMethodClassA = new ReflectionMethod($classA, $method);
if($refMethodClassB->invoke($classB) != $refMethodClassA->invoke($classA)) {
array_push(
$differences,
array($method => array(
'classA' => $refMethodClassA->invoke($classA),
'classB' => $refMethodClassB->invoke($classB)
)
));
}
}
return $differences;
}
Special thanks to George Marques's comment.