I'm having some difficulties to select the last entry I posted into my Mongo Collection. This is an example of what an object in my collection looks like:
{"category":1,"date:
{"sec":1356521350,"usec":0},"content":"Test Content","_id":
{"$id":"50dadf8639f992c83f000003"}}
Now, I want to sort on the field date and I am trying to do so by using the following functionality (by using the Yii-MongoDB-Suite):
$oCriteria = new EMongoCriteria;
$oCriteria->sort('date', EMongoCriteria::SORT_DESC);
$oOjbect = ObjectModel::model()->find($oCriteria);
Now, instead of returning the object which has the lastest date, it returns me the first object I entered in the collection.
I literally have no clue about what might be going wrong. Any clues?
I believe you need to pass a PHP array to sort(), so what you actually need is:
$oCriteria = new EMongoCriteria;
$oCriteria->sort(array('date', EMongoCriteria::SORT_DESC));
$oOjbect = ObjectModel::model()->find($oCriteria);
Related
Database table SITE has many columns. One of them is site_id. I need all the site_ids as an array since it has to be fed to a method which accepts only a string array.
What I tried so far is:
$sites = DB::select('select site_id from site_tab');
$sites_arr = $sites->toArray();
But this doesn't produce the result I want. I need $sites_arr to be like ['A','B','C',...]
Please suggest a way to get this done. A solution based on Eloquent is also OK for me.
Thanks
Try this:
DB::table('site_tab')->pluck('site_id')->toArray();
reference pluck
referen toArray
If you open a manual, you will see that
The select method will always return an array of results
So, there's no need to use ->toArray(), as result is already an array.
To get values as array of names you can do:
$site_ids = DB::table('site_tab')->pluck('site_id');
Using ->toArray() here is optional, as you can iterate over $site_ids (which is a Collection) with a foreach too.
Im a bit new to the php side of parse, mainly objective-c and swift but I need to write some code that I can query a column (not the objectID one) to return the results..
The column I'm trying to query is a pointer to another class.
Here is the very basic code I have which returns all the rows in the class and the pointers data with the include key, but I need to filter or get only the row/s that I'm looking for.
$query = new ParseQuery("ClassB");
$query->includeKey("ClassA");
$results = $query->find();
In the php sdk I see an option to use equalTo which has a key and a value to it so I tried the following code.
so I choose the column that was the pointer , and its objectid to hopefully only return those row/s that has that object id.
$query = new ParseQuery("ClassB");
$query->includeKey("ClassA");
$query->equalTo("ColumnNameX", "yjdyaGRWP7");
$results = $query->find();
Nothing was returned and a php error was spit out
'pointer field ColumnNameX needs a pointer value' in /var/www/parse/src/Parse/ParseClient.php:326
So im not 100% sure why I cant filter by a ColumnNameX using its objectID which is a pointer to ClassA..
Did I miss something in the PHP docs..
I mean ideally in mysql to just get that row I want would be
SELECT * FROM ClassB WHERE ColunNameX = yjdyaGRWP7
That would return me the row of data, I can use a Join of course to get some info from ClassA as well.
Any thoughts on what im missing or do I need to first query the Class A to get a pointer, then in the equalTo do something like ("ColumnNamX" , $pointerfromClassA) ?
any one have anyone point out what im missing or have a code example.. I have seen some that use the objectID but I dont have access to that.
Ok I figured out one way to do this, not sure if this is the right way but it returns now what I want..
$query->equalTo("ColunNameX", array("__type" => "Pointer", "className" => "ColunNameX", "objectId" => "yjdyaGRWP7"));
i'm relatively new to coding and I need a little help. I'm basically trying to loop through an entry in a mySQL database and push any new entry into an array , so that it only comes up once in my array.
// SQL query
$response = $bdd->query('SELECT serie_bd FROM inventaire_bd');
//creating array to group all elements from the db so that they do not repeat
$serie_bd_groupe=array();
while($data_collected = $response->fetch())
{
if(array_key_exists($data_collected,$serie_bd_groupe)==false)
{
array_push($data_collected,$serie_bd_groupe);
}
}
Will this work? - it seems like the loop will just stay stuck after it comes accross an entry a second time because the if statement wont execute itself.
Also in the future, are their any php equivalent to jsfiddle.net so i can test code syntaxically?
Thank you for your time
Your array keys will be default integers, so you don't want to check those. Instead of this:
if(array_key_exists($data_collected,$serie_bd_groupe)==false)
you should do this:
if(!(in_array($data_collected,$serie_bd_groupe)))
http://php.net/manual/en/function.in-array.php
On the other hand, if you're expecting your collected data to be the array key rather than value, you'd do something like this, instead of your array_push:
$serie_bd_groupe[$data_collected] = 1;
then your key check would work.
If you are looking for UNIQUE values (serie_bd) from your database, update your query to include "DISTINCT" like this:
$bdd->query('SELECT DISTINCT serie_bd FROM inventaire_bd');
On the other hand, I think you are looking for http://phpfiddle.org/
I am currently learning MongoDB. I have seen tutorials for querying a field in a collection. I would like to know how to query in PHP using the MongoId Object _id value. The closest answer to my question is at Perl Mongo find object Id .
Also, is there a way that when a new record is created, the Object _id value can be recorded in another field of that record?
Thanks.
Update:
In addition to the answer I chose below, a coworker found this as well:
mongodb php findone() by ID
This should help, from the mongodb web site (http://blog.mongodb.org/post/26903435041/mongodb-for-the-php-mind-part-2):
// This is only a string, this is NOT a MongoId
$mongoid = '4cb4ab6d7addf98506010000';
// You will not find anything by searching by string alone
$nothing = $collection->find(array('_id' => $mongoid));
echo $nothing->count(); // This should echo 0
// THIS is how you find something by MongoId
$realmongoid = new MongoId($mongoid);
// Pass the actual instance of the MongoId object to the query
$something = $collection->find(array('_id' => $realmongoid));
echo $something->count(); // This should echo 1
Regarding part of your question, I'm not aware of any automated way to store the objectid in another field but I'm also not aware of why you would want to do that - you already have it stored in _id, why would you want it in another field?
I'm lost at this point.
Here is what i have:
$criteria = new \EMongoCriteria();
$criteria->userId = new \MongoID($userId);
$criteria->expiresAt = array('>' => new \MongoDate(time()));
Then I'm running this:
$model->count($criteria);
And it always returns 0 when i know that there are documents that meet this criteria.
Any ideas?
UPDATE
findAllByAttributes() with the same criteria works perfectly. But I don't need those documents i need to count them.
When setting criteria field as property it uses simple comparision (field == value). You should set this criteria by calling field, like that:
$criteria->expiresAt('>', new \MongoDate(time()));
NOTE: Passing criteria to findAllByAttributes in wrong, it is not intended to work with EMongoCriteria, but with simple array. If you want to use criteria object pass it to findAll method.