I am trying to update records in a mongodb by using php codeigniter, but I am unable to do so.
My controller class:
function updatetodb_post(){
$updateddata = array('$set' => array("lang" => "English"));
$this->load->model('data_model');
$uid = 1;
$this->data_model->updaterecords($uid, $updateddata);
}
My Model class:
function updaterecords($uid, $updatedata){
$this->load->library('mongo_db');
$recoundsbyuid = $this->mongo_db->get_where($this->_testcollectoin, array("uid" => $uid));
$this->mongo_db->update($this->_testcollectoin, $recoundsbyuid, $updatedata);
}
data that I want to update in a collectoin:
$data = array(
"uid" => "1",
"type" => "Movie",
"genre" => "Action"
);
where _testcollectoinis my collection name. I want to add one more field (lang) to the array.
We can use below commands to update with given data
$this->mongo_db->where(array("uid" => $uid))->set($updatedata)->update($this->_testcollectoin);
Related
I am trying to add more email address to the list. The architecture and the example in Dynamo DB
ID: STRING
DEP: MAP
Contacts: MAP
EMAIL: LIST
Version: Number
{
“ID”: "1234567888",
"Dep": {
"Contacts": {
"Email": [
"test#test.com”
]
}
},
"Version": 34
}
I tried but it just updates the value but don't add the new one. (if you can also help me with if the value does not exist than only add would be nice but not mandatory)
$eav = $marshaler->marshalItem([":email" => [$email]]);
$params = ['TableName' => "xxx", 'ID' => $key,
'UpdateExpression' => 'SET Dep.Contacts.Email = :email',
'ExpressionAttributeValues' => $eav, 'ReturnValues' => 'UPDATED_NEW'];
$result = $dynamodb->updateItem($params);
You should list_append to add an element to a list. CLI example below:
--update-expression "SET #ri = list_append(#ri, :vals)"
You can see this in the documentation here:
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html#Expressions.UpdateExpressions.SET.AddingListElements
I'm very new to PHP and having trouble making an API that's essentially taking a GET request and returning all users stored in a table. This application uses Zend Framework 2 and Doctrine.
Controller:
public function viewAllAction(){
$restService = $this->getServiceLocator()->get("Rest");
$restService->auth();
$business = $restService->getIdentity();
$bizID = $business->getId();
$clientModel = $this->getServiceLocator()->get('clientModel');
$clients = $clientModel->findAllByBusinessID($bizID);
$params['client'] = array(
"name" => $clients->getName(),
"email" => $clients->getEmail(),
"phone" => $clients->getPhone(),
"address" => $clients->getAddress(),
"suburb" => $clients->getSuburb(),
"postcode" => $clients->getPostcode(),
"state" => $clients->getState(),
"country" => $clients->getCountry(),
);
return $restService->send($params);
}
Model:
public function findAllByBusinessID( $business_id ){
$clientRepository = $this->getMapper()->getRepository( self::ENTITY_CLASS );
$clients= $clientRepository->findBy(array("bid" => $business_id));
foreach($clients as $client) {
return $client;
}
}
At the moment I can successfully retrieve data and return it via rest, but only the first set(row) of data. There are more in the table with the same business ID.
How to I return all rows that have the same business ID instead of just the first one? Thank you!
The problem is in your loop in findAllByBusinessID method. return breaks your loop, so only first row is returned. What do you want is probably something like this:
public function findAllByBusinessID( $business_id ){
$clientRepository = $this->getMapper()->getRepository( self::ENTITY_CLASS );
$clients= $clientRepository->findBy(array("bid" => $business_id));
$results = [];
foreach($clients as $client) {
$results['client'][] = [
"name" => $client->getName(),
"email" => $client->getEmail(),
"phone" => $client->getPhone(),
"address" => $client->getAddress(),
"suburb" => $client->getSuburb(),
"postcode" => $client->getPostcode(),
"state" => $client->getState(),
"country" => $client->getCountry(),
];
}
return $results;
}
But you should split this method into 2 separate functions. One function for retrieving data from database, one to format data the way you want.
Another solution would be to use Doctrine's queryBuilder and return data as array set (getArrayResult())
I'm wondering if there's any PHP "standard" way to apply properties to an object or array, but only if they exist. Array example
$prototypeConfig = array(
"username" => null,
"password" => null,
"email" => null
);
$receivedConfig = array(
"username" => "test",
"password" => "123",
"email" => "foo#example.com",
"nonexisting" => true
);
$config = some_magic_array_function($prototypeConfig, $receivedConfig);
print_r($config);
// Result would be:
// "username" => "test",
// "password" => "123",
// "email" => "foo#example.com"
```
Basically that magic array function should throw away all properties or keys which aren't existing in the prototype. Of course, I can easily implement an own function which does that, but probably there's a PHP function which does exactly that.
ExtJS has the Ext.applyIf method to do exactly that, but I'm missing a PHP counterpart, if such one exists.
I have a Podio app which is populated by the php API via a form on a website.
Using the Create Item snippet works for text fields, currency values, etc but have been I unable to untangle the documentation as to how to set a category field.
The category field, "attending", is a simple yes/no choice.
The method shown here generates a server error when combined with Create Item snippet as below:
$fields = new PodioItemFieldCollection(array(
new PodioTextItemField(array("external_id" => "title", "values" => $name)),
new PodioTextItemField(array("external_id" => "email", "values" => $email)),
));
$item = new PodioItem(array(
'app' => new PodioApp(intval($app_id)),
'fields' => $fields,
));
pseudo code: if attending = yes, $attending = 1, else $attending = 2 //id's of yes and no in category field
$item->fields['attending']->values = $attending;
$item->save();
What am I missing? Thanks.
For those still searching for an answer: The trick is to use an array.
Perform the evaluation of $attending before opening the Collection and then simply
add this line into the PodioItemFieldCollection.
new PodioCategoryItemField(array(
'external_id' => 'attending', 'values' => array($attending))),
So the whole snippet would look like this
($foo == 'yes') ? $attending = 1: $attending = 2 ; // evaluating $foo for yes
$fields = new PodioItemFieldCollection(array(
new PodioTextItemField(array("external_id" => "title", "values" => $name)),
new PodioTextItemField(array("external_id" => "email", "values" => $email)),
new PodioCategoryItemField(array(
'external_id' => 'attending', 'values' => array($attending))) // must be an array
));
$item = new PodioItem(array(
'app' => new PodioApp(intval($app_id)),
'fields' => $fields,
));
$item->save();
You can find examples at: http://podio.github.io/podio-php/fields/#category-field
If it's still not working, place podio-php into debug mode so you can see what data is being sent to Podio: http://podio.github.io/podio-php/debug/
Im trying to save data inside a pivot table with an extra field called data.
when i save i have this array:
[
5 => "files"
4 => "pictures"
3 => "tags"
1 => "thumbs"
]
My table looks like this:
project_id
option_id
name
The ids shown above refer to option_id and the string to name inside the database.
When i try to use sync like this: $project->options()->sync($data);
$data is the array shown above
Im getting a error thats its trying to save the option_id with "files".
Here is how i build up the data that i use for sync:
Im trying to get what you suggested but dont know how to achieve it:
here is how i build up the array:
foreach($request->input('option_id') as $id) {
$option['option_id'][] = $id;
$option['data'][] = $request->input('data')[$id];
}
$data = array_combine($option['option_id'], $option['data']);
This is covered in the manual:
Adding Pivot Data When Syncing
You may also associate other pivot table values with the given IDs:
$user->roles()->sync(array(1 => array('expires' => true)));
In your example, you would have to change your array to look something like below but I believe this would translate to:
$data = [
5 => [ 'name' => "files" ],
4 => [ 'name' => "pictures" ],
3 => [ 'name' => "tags" ],
1 => [ 'name' => "thumbs" ],
];
$project->options()->sync($data);
I believe you may also need to modify how your Project model relates itself to your Options model:
// File: app/model/Project.php
public function options()
{
return $this->belongsToMany('Option')->withPivot('name');
}
This is also noted in the linked-to manual page:
By default, only the keys will be present on the pivot object. If your pivot table contains extra attributes, you must specify them when defining the relationship.
Update
Try creating your $data array like this:
$data = [];
foreach($request->input('option_id') as $id) {
$data[$id] = [ 'name' => $request->input('data')[$id] ];
}