How do you get the string value of a MongoID using PHP? - mongodb

After doing an insert I want to pass the object to the client using json_encode(). The problem is, the _id value is not included.
$widget = array('text' => 'Some text');
$this->mongo->db->insert($widget);
If I echo $widget['_id'] the string value gets displays on the screen, but I want to do something like this:
$widget['widgetId'] = $widget['_id']->id;
So I can do json_encode() and include the widget id:
echo json_encode($widget);

Believe this is what you're after.
$widget['_id']->{'$id'};
Something like this.
$widget = array('text' => 'Some text');
$this->mongo->db->insert($widget);
$widget['widgetId'] = $widget['_id']->{'$id'};
echo json_encode($widget);

You can also use:
(string)$widget['_id']

correct way is use ObjectId from MongoDB:
function getMongodbIDString($objectId){
$objectId = new \MongoDB\BSON\ObjectId($objectId);
return $objectId->jsonSerialize()['$oid'];
}
and do not cast the objectId like (string) $row['_id'] or $row->_id->{'$oid'}

I used something similar:
(string)$widget->_id

I used something similar if object:
$widget->_id->{'$oid'}
or
(string)$widget->_id
or array :
$widget['id']->{'$oid'}
(string)$widget['_id']

Related

How can I manipulate a string in dart?

Currently I'm working in a project with flutter, but I realize there is a need in the management of the variables I'm using.
Basically I want to delete the last character of a string I'm concatenating, something like this:
string varString = 'My text'
And with the help of some method or function, the result I get:
'My tex'
Am I clear about it? I'm looking for some way which helps me to 'pop' the last character of a text (like pop function in javascript)
Is there something like that? I search in the Dart docs, but I didn't find anything about it.
Thank you in advance.
You can take a substring, like this:
string.substring(0, string.length - 1)
If you need the last character before popping, you can do this:
string[string.length - 1]
Strings in dart are immutable, so the only way to do the operation you are describing is by constructing a new instance of a string, as described above.
var str = 'My text';
var newStr = (str.split('')..removeLast()).join();
print(newStr);
Another way:
var newStr2 = str.replaceFirst(RegExp(r'.$') , '');
print(newStr2);

How to insert an option value into a select tag with CasperJS?

Well, the question is very self-explanatory.
Right now, I'm front of a form which has a select tag with a couple of options already. But I must insert a new one, with a different value that I will receive from a .json file.
The thing is: I haven't been able to find a suitable solution from the CasperJS documentation.
I've tried something like this:
this.fill('form.coworkerdiscountcode', {
'CoworkerDiscountCode.DiscountCode': ['Value1']
});
But no results. Any ideas?
Thanks in advance.
You can execute any javascript code by passing it to casper.evaluate like this:
casper.evaluate(function() {
var x = document.getElementById("coworkerdiscountcode");
var option = document.createElement("option");
option.text = "Kiwi";
x.add(option);
});

Angular2 Like request MongoDB

I want to do a pretty simple thing, a function that does a query to mongoDB using a Like. But I don't seem to make it works.
At the moment it looks like this :
searchChannel(valueToSearch:string){
this.items = Channels.find({'title':'/' + valueToSearch + '/'});
}
I tried /valueToSearch/ too, but it doesn't return any result.
To construct a regular expression from string you can use RegExp
searchChannel(valueToSearch:string){
this.items = Channels.find({'title': new RegExp(valueToSearch)});
}
Ok so... the issue is likely because you're inserting / characters into your mongo query. I think you want something like this:
searchChannel(valueToSearch:string){
this.items = Channels.find({'title': valueToSearch });
}
Unless the / characters are part of your stored content, they shouldn't be added to your search string.

PHP preg_replace not working as expected

I'm trying to run a find replace of multiple values to do a mail merge effect on an HTML signature stored in the database.
I can get the string part replaced no worries, but for some reason it's leaving the "[" & "]" behind in the output.
Merge tags in the HTML would look like this: [FirstName], [LastName]
Original HTML would look like this:
Kind regards
[FirstName] [LastName]
After running the mailmerge function it should look like this:
Kind regards
John Smith
Here is what I've come up with so far, and Im sure the issue is something small:
public function merge_user_signature() {
$user = $this->get_user_by_id();
//spit_out($user);
$authorisedMergeTags = array (
"[FirstName]" => $user->firstName,
"[LastName]" => $user->lastName
);
$keys = array_keys($authorisedMergeTags);
$values = array_values($authorisedMergeTags);
$html = $this->get_user_signature();
$mergedSignature = preg_replace($keys, array_values($authorisedMergeTags), $html);
return $mergedSignature;
}
Thanks in advance
You don't need to use a regex to deal with literal strings (whatever the situation):
return strtr($html, $authorisedMergeTags);

CakePHP FormHelper Input Date

I have following question. When I use this Helper:
echo $this->Form->input('start_date', array('required'=>false, 'class'=>'form-control date'));
I get following output:
Where can I change this output type? I tried it in
/lib/Cake/View/Helper/FormHelper.php
I found in this lib file, that the Helper gets the function __getInput() and in the date case, following sub function:
case 'date':
$options['value'] = $selected;
return $this->dateTime($fieldName, $dateFormat, null, $options);
But in the function dateTime() I got lost. Is there any updated Helper out or is there a simple trick to change the HTML-output format?
Thanks & regards
Set input type as text
echo $this->Form->input('start_date', array('type'=>'text','required'=>false, 'class'=>'form-control date'));