I am trying to convert post data into a format that would allow me to pass it right into my collection. For example: When I print_r on the $_POST I get this form data:
Array
(
[Name] => Steve
[Email] => Steve#mail.com
[submit] => Submit
)
I am wondering how i can convert this to an acceptable object to insert into mongodb collection using php similar to:
$Record = array(
'Name' => 'Steve',
'Email' => 'Steve#mail.com',
'submit' => 'Submit'
);
$Collection->insert($Record);
I am thinking a loop of the above array with some additional formatting but I can't seem to figure it out. I have also tried json_encode but keep getting the same error "Call to a member function insert() on a non-object in..." saying that its not a proper object. Thank you for any help.
No need to encode anything, it's just PHP native and expects an array. Let the driver do the work for you:
$Collection->insert( $_POST );
As it is the two should be equivalant:
$rec = array(
'Name' => 'Steve',
'Email' => 'Steve#mail.com',
'submit' => 'Submit'
);
print_r ($rec);
Related
There is field affiliated_facility which is in array and i am trying to update my values:
My controller code:
<?php
$affiliated_facility = implode(",", $userProfile['affiliated_facility']);
$userBasicInfoId = UserBasicInfo::where('user_id', $userProfile['id'])->value('id')->update([
'work_phone' => $userProfile['work_phone'],
'fax' => $userProfile['fax'],
'extension' => $userProfile['extension'],
'primary_facility' => $userProfile['primary_facility'],
'employed_by' => $userProfile['employed_by'],
'emergency_phone' => $userProfile['emergency_phone'],
'designation' => $userProfile['designation'],
'department' => $userProfile['department'],
'employment_type' => $userProfile['employment_type'],
'biography' => $userProfile['biography'],
'hiring_date' => $userProfile['hiring_date'],
'from' => $userProfile['from'],
'to' => $userProfile['to'],
'affiliated_facility' => $affiliated_facility]); // this is in array so i used implode in top of this code
if ($userBasicInfoId) {
$userBasicInfo = $this->userBasicInfo->find($userBasicInfoId);
$userBasicInfo->fill($userProfile)->save();
} else {
$userBasicInfo = $this->userBasicInfo->create($request->only($this->userBasicInfo->getModel()->fillable));
}
?>
But when I hit my request it says
Call to a member function update() on integer
There is some mistakes in my code i want to update my record and there is one field coming which is in array
"affiliated_facility": [1,2]
can someone please help me to modify this code i am stuck on this your help will be highly appreciated!
Thankyou in advance
UserBasicInfo::where('user_id', $userProfile['id']) is a Builder instance. you should get Model instance to update it. So, try:
UserBasicInfo::where('user_id', $userProfile['id'])->first()->update([...]);
Im working with an api which stores data into a JSON file. This data is gathered from a form that the users fill in my website. The way its inserted goes as follow:
$pers_payload = array(
'gender' => 'Unknown', //or Male / Female
'first_name' => $_POST['billing_first_name'],
'family_name' => $_POST ['billing_last_name'],
'email' => $_POST['billing_email'],
'linked_as_contact_to_organization' => array(
array(
'organization_id' => $organization_id, // add the person as a contact to the newly created organization
'work_email' => $_POST['billing_email'],
'work_phone' => $_POST['billing_phone']
)
),
'visiting_address' => array(
'country_code' => 'NL'
), // can be extented with other address data
'postal_address' => array(
'country_code' => $_POST['billing_country']
) // can be extented with other address data
);
And then:
$person = $SimplicateApi->makeApiCall('POST','/crm/person',json_encode($pers_payload));
Now instead of post i want to get the data. I tried getting data like this:
$SimplicateApi->makeApiCall('GET','/crm/organization?q[name]=*my name*');
I dont know if this is the right way, well it didn't work so obviously its not.
Anyways what im trying to achieve is with PHP i want to gather the name value of an existing person. this data is stored in /api/v2/crm/person.json
Api documentation (which i read but didn't understand to well) http://api.simplicate.nl/
It's been a while but i'm trying to answer all my open questions without an answer which i ended up solving on my own.
So for this.
You have to create a variable which makes the get request like this:
$test = $SimplicateApi->makeApiCall('GET','/crm/organization?q[name]=My name');
Now you can for example do a var_dump($test);
And as output you will get all the data inside
/crm/organization?q[name]=My name
i m trying to to get the request values of form in zend framework
earlier i was using the post method
but the the iphone app is sending me data in get method .
how do i can use it has i was using the post values like
$post = array(
'id'=>'2',
'email'=>'4',
)
i want to get values also in this form value when a form is submitted .
i was using this to get post values
$post = $this->getRequest ()->getPost ();
and i tried this to get get method values
$post = $this->getRequest();
but
i get this error
Cannot use object of type Zend_Controller_Request_Http as array in
this is the full error message
"Zend_Controller_Request_Http Object ( [_paramSources:protected] =>
Array ( [0] => _GET [1] => _POST ) [_requestUri:protected] =>
/platinum-picks/p-picks/index/registration [_baseUrl:protected] =>
/platinum-picks/p-picks [_basePath:protected] => [_pathInfo:protected]
=> /index/registration [_params:protected] => Array ( [controller] => index [action] => registration [module] => default )
[_rawBody:protected] => [_aliases:protected] => Array ( )
[_dispatched:protected] => 1 [_module:protected] => default
[_moduleKey:protected] => module [_controller:protected] => index
[_controllerKey:protected] => controller [_action:protected] =>
registration [_actionKey:protected] => action ) Fatal error: Cannot
use object of type Zend_Controller_Request_Http as array in D:\Program
Files\Zend\Apache2\htdocs\platinum-picks\application\models\User.php
on line 267"
If I understand correctly, you want to get the values passed using GET method and you have done the same successfully with POST values. The only change you need to make in such a scenario is this:
$post = $this->getRequest()->getPost ();
becomes
$get = $this->getRequest()->getParams();
This will also return parameters from other sources:
Retrieves a merged array of parameters, with precedence of userland params (see setParam()), $_GET, $_POST (i.e., values in the userland params will take precedence over all others).
So refer to the manual if this may be a problem for you.
You can find the GET variables using for example:
$this->getRequest()->getParams();
Or, for a specific variable:
$this->getRequest()->getParam('myVar');
This will also search for POST values. To search the GET vars only, use:
$this->getRequest()->getQuery('myVar');
I'm using PHP to create a system that will fetch webhook payload when someone is unsubscribing newsletters, but I can figure out how to fetch the actual payload information in PHP.
Is there any POST data to fetch? How does PHP look for this POST data?
UPDATE: I may be on to something. Seems like the function http_get_request_body() will do the trick?
$http_get_request_body solves it :)
I recently ran into this issue and used the following PHP code for handling Campaign Monitor Web Hooks:
<?php
$json = file_get_contents('php://input');
$data = json_decode( $json, TRUE ); //convert JSON into array
foreach ($data['Events'] as $event)
{
// Process each entry in the request
}
The JSON data, once converted to an array will give you data in this format:
array (
'ListID' => 'LIST_ID_KEY',
'Events' => array (
0 =>
array (
'Type' => 'Subscribe',
'Date' => '2014-01-01 16:00:00',
'EmailAddress' => 'test#example.com',
'Name' => 'John Smith',
'CustomFields' => array (),
'SignupIPAddress' => 'API',
),
),
)
I've looked at loads of forums about validation errors not showing and tried various things but to no avail...
Basically, the validation is correctly recognising the fields do not have values when they should, however the error messages don't 'automagically' appear below the input boxes.
Model validation rule is shown below:
var $validate = array(
'description' => array(
'rule' => 'notEmpty',
'required' => true,
'allowEmpty' => false,
'message' => 'Please enter a description of the change'
)
);
echo pr($this->data); output is shown below:
Array
(
[Change] => Array
(
[0] => Array
(
[id] => 3237
[cn_id] => 5132
[req_id] => 25
[description] =>
)
[1] => Array
(
[id] => 3238
[cn_id] => 5132
[req_id] => 22
[description] =>
)
[2] => Array
(
[id] => 3239
[cn_id] => 5132
[req_id] => 4
[description] =>
)
)
)
echo pr($this->Change->invalidFields()); output is shown below:
Array
(
[0] => Array
(
[description] => Please enter a description of the change
)
[1] => Array
(
[description] => Please enter a description of the change
)
[2] => Array
(
[description] => Please enter a description of the change
)
[description] => Please enter a description of the change
)
So, it is generating the errors messages for display, but they don't actually display in the view, and I don't know why?
Excerpt from the 'view' code is show below:
<?php echo $form->input('Change.'.$i.'.description',
array('value' => $cn['Change'][$i]['description'],
'label' => $engReq['Req']['description'])); ?>
Does anybody have ideas why the error messages are not showing?
I experienced the same issue with a hasMany model (where the form had numerically indexed fields) and came up with a validation solution that worked for me.
Quick answer: Before trying to actually save the data, I validated the data separately like (notice 'validate'=>'only'):
if($this->ModelName->saveAll($this->data, array('validate' => 'only'))) {
// proceed to save...
}
Doing it this way gave me the model's validation error message in the form, right under the input field that failed the validation (the normal Cake way of showing the validation error).
Note: I could not use saveAll() to actually save my data (I'll explain why in a minute). If I could use saveAll() to actually save the data, I could have gotten the validation at the same time as I saved by using (notice 'validate' => 'first'):
if($this->ModelName->saveAll($this->data, array('validate' => 'first')))
However, I could not use saveAll() to actually save the data, due to the fact that I needed to use a transaction to save several models at once, where some of the models were not directly related to other models. saveAll() will only save the model on which it is called, plus models directly related to it. Since Cake does not currently support nested transactions, and saveAll() uses one transaction automatically, I had to use save() on my models and start and end my transaction manually. However, this caused me to loose the validation message in my form on the hasMany items, even if I saved by using "$this->ModelName->save($this->data, array('validate'=>'first')".
Further explanation: The issue does seem to be related to using numerically indexed fields in the form. For example:
$this->Form->input("ModelName.0.field_name");
It seems this indexing scheme is the proper way to handle hasMany items in the form, but the validation messages would not find their way to this form input. It is interesting to notice that my view did in fact have access to the validation error. This can be seen in the view by using (notice no numerical index in these lines):
if($this->Form->isFieldError("ModelName.field_name")) {
echo $this->Form->error("ModelName.field_name");
}
Putting these lines after the '$this->Form->input("ModelName.0.field_name")' inserted a the validation message into the page, just not in the same div as the input field (and thus it didn't look ideal).
I couldn't figure out a way to tell Cake to use that validation message in the '$this->Form->input("ModelName.0.field_name")'. So I resorted to the 'validate' => 'only' method described earlier, which is working well for me.
shouldnt it be
var $validate = array(
'description' => array(
'notEmpty' => array(
'rule' => 'notEmpty',
'required' => true,
'allowEmpty' => false,
'message' => 'Please enter a description of the change'
)
)
);
?