In my controller I have the following lines
$request = Yii::$app->request;
print_r($request->post());
echo "version_no is ".$request->post('version_no',-1);
The output is given below
Array
(
[_csrf] => WnB6REZ6cTAQHD0gAkoQaSsXVxB1Kh5CbAYPDS0wOGodSRANKBImVw==
[CreateCourseModel] => Array
(
[course_name] => test
[course_description] => kjhjk
[course_featured_image] =>
[course_type] => 1
[course_price] => 100
[is_version] => 1
[parent_course] => test
[version_no] => 1
[parent_course_id] => 3
[course_tags] => sdsdf
)
)
version_no is -1
So here the return value of post() contains the version_no.But when it is called as $request->post("version_no"), it is not returning anything (or $request->post("version_no",-1) returns the default value -1).
As per Yii 2.0 docs, the syntax is correct and should return the value of post parameter.
But why is it failing in my case.The post array has the parameter in it.But the function is not returning when called for an individual parameter value.
your parameters are in $_POST['CreateCourseModel']['version_no'] etc. with $request->post('version_no',-1) you trying to get $_POST['version_no'] which is not defined so it returns you -1. So to get version_no use
$data = $request->post('CreateCourseModel');
print_r($data['version_no']);
You can access nested $_POST array elements using dot notation:
\Yii::$app->request->post('CreateCourseModel.version_no', -1);
Model properties are grouped like that for massive assignment that is done via $model->load(Yii::$app->request->post()).
Depending on your needs maybe it's better use default value validator like that:
['version_no', 'default', 'value' => -1],
Related
I am passing an array into my controller via axios.post request. I am trying to get the length of the $request array that I am passing to the controller. However, I keep recieving a "Parameter must be an array or an object that implements Countable" error.
Here is what my array looks like:
array (
0 =>
array (
'text' => 'It is this',
'question_id' => 98,
),
1 =>
array (
'text' => 'And it is that',
'question_id' => 98,
),
2 =>
array (
'text' => 'Also a little bit of this',
'question_id' => 98,
),
Here is what I have tried:
$count = sizeof($request));
$count = $request->length;
$count = count($request);
The only thing that has had even a little bit of success is doing:
$count = count($request[0])
This returns 2, which is for the elements inside the first array. It counts text, and question_id. While this is good progress this is not what I want
What I would like to see happen is to have the length of the entire $request object. In the example I gave above, I would like to either receive 2, (the end of 0,1,2) or 3 (the count of 0,1,2).
If $request is an Illuminate\Http\Request, it won't be directly countable.
You can count $request->all() or $request->input(), though.
What about:
count($request->all());
In as much as the accepted answer will count the number of items in the requests, it will not count the number of items in a single request request item say C. If you are sure that C is an array and C is part of the request then doing count($request->C) not e that count here is a regular PHP function
try ;
count($request->all());
This is the print_r from the $form_data variable / array in php log.
[28-Sep-2018 18:04:03 UTC] Array
(
[cfdb7_status] => unread
[meno] => data
[email] => data
[telefon] => phone
[meno-ucastnika__1] => data
[email-ucastnika__1] => data
[meno-komory__1] =>
[registracne-cislo__1] =>
[_wpcf7_groups_count] => Array
(
[emails] => 1
)
[obchodne-meno] => obchod
[obchodne-sidlo] => fs
[ico] => 50426508
[dic] => dic
[icdph] => icdicko
)
How can I get the value of _wpcf7_groups_count key?
If I want email I simply wrote $form_data['email']. Everything goes like this except _wpcf7_groups_count.
$form_data['_wpcf7_groups_count']
$form_data['_wpcf7_groups_count'][0]['emails']
$form_data['_wpcf7_groups_count']['emails']
Anything from above doesn't work. The first is giving me an illegal offset.
From the data you posted(*),
$form_data['_wpcf7_groups_count']['emails']
should work, and yield 1.
Note that the parent key value is an array, so if the form data goes through some sort of templating engine, the problem might lie there.
I find it strange that you get an error for the first method, and not for the others: in PHP, if you can't reference an array key, you cannot reference any of the descendants, and you still get the error from the parent key. This is what makes me suspect that something else is afoot.
==========
(*) I assumed that you have deleted some information while keeping the formatting. Otherwise writing
[meno-komory__1] =>
[registracne-cislo__1] =>
could possibly be interpreted as a nested key. I saw no 'Array', so I assumed there was just some data missing. But next time write it explicitly to avoid any ambiguity:
[meno-komory__1] => "(redacted)",
[registracne-cislo__1] => "(array, redacted)",
I'm using CodeIgniter framework for the easyappointments.org library. I'm trying to update the field data through the id of a row. Actually this is my code for the update:
return $this->db
->update('ea_appointments', $appointment)
->where('ea_appointments.id', $appointment_id);
The problem's that the update method need two parameter (table to update, associative array with the fields name column of db). In my above function I pass only the $appointment_id, so the variable $appointment is empty and doesn't contain any associative array. I want to know how to update only the field data to 1. And remain the other field in the same value condition.
This is the structure of the table:
id|book|start|end|notes|hash|unavailable|provider|data
Logic example:
previous condition row:
id => 0
book => example
start => 18/10/2015
end => 19/10/2015
notes => empty
hash => akdjasldja
unavailable => false
provider => 5
data => 0
I pass in the function $appointment_id with 0 value. I'm waiting this new result:
id => 0
book => example
start => 18/10/2015
end => 19/10/2015
notes => empty
hash => akdjasldja
unavailable => false
provider => 5
data => 1
So the main problem is retrieve first the all field value of the specific row and later update? Or something like this. Could someone help me?
In my above function I pass only the $appointment_id, so the variable
$appointment is empty and doesn't contain any associative array.
If you simply want to update the column data to 1 for appointment_id 0 then pass in an array with data for the key and 1 for the value.
$appointment_id = 0;
$appointment = array('data' => 1);
$this->db->where('id', $appointment_id);
$this->db->update('ea_appointments', $appointment);
I'm querying a DynamoDB table using the method described here in the AWS PHP developer guide.
The array that I pass to the Dynamo connection looks like the following:
Array(
[ConsistentRead] => false
[TableName] => bbq_lol_test
[KeyConditions] => Array(
[stinky_cheese] => Array(
[ComparisonOperator] => EQ
[AttributeValueList] => Array(
[S] => camembert)
)
)
)
As you can see, it's in the exact format that the example uses, with the exception of the Type enum (which is just a string).
When executing $connection->query with the above array as an argument, I get the following exception:
Guzzle\Service\Exception\ValidationException:
Validation errors: [KeyConditions][stinky_cheese][AttributeValueList][S][AttributeValue] must be of type object
Which is odd, because the array in the stack trace appears to be fine:
Aws/Common/Client/AbstractClient.php(103): Guzzle\Service\Client->__call("Query", array(array("ConsistentRead" => false, "TableName" => "bbq_lol_test", "KeyConditions" => array("stinky_cheese" => array("ComparisonOperator" => "EQ", "AttributeValueList" => array("S" => "camembert"))))))
I DO have an index on the stinky_cheese field (since I'm using query). I'm probably doing something daft, but I can't seem to figure it out now. Any help would be appreciated - thanks!
There were two problems.
First, AttributeValueList should be an array of arrays, not a single array, going from this:
[AttributeValueList] => Array([S] => camembert)
To this:
[AttributeValueList] => Array(Array([S] => camembert))
Since it's possible to add more stuff in there, like so:
[AttributeValueList] => Array(Array([S] => camembert), Array([S] => bleu))
The second problem was that I always have to query using the primary hash key, which I was not doing.
I am trying to do a batch_get_item to request multiple items from a table. I am following the PHP example in the DynamoDB documentation, but I am not getting the results I'm expecting.
Following is the code:
$batch_array = array ();
$batch_array[]= array ('HashKeyElement' =>
array( AmazonDynamoDB::TYPE_STRING => 'V1L3M5O5L1W8R5B6D2Q1S8V0B3R8M7A6R0X0'));
$options = array (
'RequestItems' => array(
'profile_dev' => array (
'Keys' => $batch_array
)
)
);
$result = $this->db->batch_get_item($options);
Instead of getting the data, I am getting a very long response, and I'm including the relevant information from the tail end of it:
[x-aws-body] => {"RequestItems":{"profile_dev":{"Keys":[{"HashKeyElement":{"S":"V1L3M5O5L1W8R5B6D2Q1S8V0B3R8M7A6R0X0"}}]}}} ) [body] => CFSimpleXML Object ( [__type] => com.amazon.coral.validate#ValidationException [message] => One or more parameter values were invalid: The provided key size does not match with that of the schema ) [status] => 400 ) )
The hashKey for this table is a string. It has a rangeKey, but I am using the hashKey so I can get all the rows matching the hashKey. What am I missing?
The DynamoDB documentation (and SDK samples) have colossal bugs in them. The documentation, and actual SDK code, make use only of the hashKeyElement, but in fact if a table has both a hashKey AND a rangeKey, both must be used.
When I used both the hashKey and the rangeKey, the call worked.
Get (or batch get) requires you to completely define the key of all items you are getting. If you want to retrieve all rows with the same hashKey using a single call, it seems like you're looking for Query.
You don't need to use BatchGet, you should be using Query. Here is an example using the PHP SDK to get all items with the HASH key 'YourHashKey' on table 'YourTable'
// Instantiate the class
$dynamodb = new AmazonDynamoDB();
$response = $dynamodb->query(array(
'TableName' => 'YourTable',
'HashKeyValue' => array( AmazonDynamoDB::TYPE_STRING => 'YourHashKey' ),
));
Reference: http://docs.amazonwebservices.com/amazondynamodb/latest/developerguide/LowLevelPHPQuerying.html