How to use DATE_FORMAT for Mongodate in Cakephp with Mongodb? - php

Hi i am using MongoDB as database in my cakephp application. and i am using this plugin for it. i want to search record(document) from my user collection. i am using this snippet
$userDetails = $this->User->find('all',array(
'conditions' => array(
'userId' => 18
)
));
it's working for me.
Now i wants to fetch the record by date like all user who was created in January Month of 2016. so for that i have to use similar to DATE_FORMAT function of MySQL and it might something like
$month = date('m');
$userDetails = $this->User->find('all',array(
'conditions' => array(
'userId' => 18,
'DATE_FORMAT(User.created_at,"%m") =' => $month
),
'fields' => array()
));
but how to use this type of Functionality with mongoDB can any one help me please?
thanks.

Try this :
var start = new Date(2016, 01, 01);
var end = new Date(2016, 01, 31);
db.User.find({userId: 18, created_at: {$gte: start, $lt: end}});
Source : here

Related

updateOrCreate does not match Carbon date

I am using Laravel Framework 6.16.0 and I am creating from a string a date object so that I can input it in my db:
$transaction_date = Carbon::createFromFormat('Y-m-d', $tradeDate);
$filling_Date = Carbon::createFromFormat('Y-m-d H:i:s', $fillingDate, 'UTC');
$product = Product::updateOrCreate(
[
'price' => trim($price),
'qty' => $qty,
'companies_id' => $company->id,
'persons_id' => $person->id,
'amount_range' => $owned,
'filling_date' => $filling_Date,
'transaction_date' => $transaction_date,
],
[]
);
When running the above query my product does not get found as $filling_Date and $transaction_date are not matched in the database, even if my product already exists.
I am guessing, the reason is that I am creating a "new" Carbon object.
Any suggestions how to match the filling_date and transaction_date in the database?
I appreciate your replies!
Try this when converting a string to a date with Carbon
$date = Carbon::parse($yourStringDate);

sfWidgetFormI18nDateTime with more years

I'm using sfWidgetFormI18nDateTime in a form, and I have to add more years to it (back in time). For example 2007, 2006, 2005 etc.
sfWidgetFormI18nDate widget does support the "years" attribute, but DateTime doesn't.
Here's my code:
$this->widgetSchema['created_at'] = new sfWidgetFormI18nDateTime(array('culture' => 'hu'));
$this->validatorSchema['created_at'] = new sfValidatorDateTime(array('required' => false));
I need something like this:
$this->widgetSchema['created_at'] = new sfWidgetFormI18nDateTime(array('culture' => 'hu', 'years' => metWidgetTools::getYears()));
$this->validatorSchema['created_at'] = new sfValidatorDateTime(array('required' => false, 'years' => metWidgetTools::getYears()));
Is there a way that I can go around this?
Your widget schema should looks something like this
$years = range(2000, date('Y')); // from to 2000 to current year
$this->widgetSchema['created_at'] = new sfWidgetFormI18nDateTime(array(
'culture' => 'hu',
'date' => array(
'years' => array_combine($years, $years)
)
));

Yii - find all by composite key

How do I find all by primary key? I don't want to specify the start and end dates as they aren't relevant when opening within booking form
$bookingRoom = BookingRoom::model()->findByPk(array('roomId' => 1, 'bookingId' => 1, 'startDate' => '20140619', 'endDate' => '20140620'));
You should use the findAllByAttributes() option:
$bookingRoom = BookingRoom::model()->findAllByAttributes(array('roomId' => 1, 'bookingId' => 1, 'startDate' => '20140619', 'endDate' => '20140620'));
If you don't want the startDate and endDate included remove it from the array.
Here is the documentation:
http://www.yiiframework.com/doc/api/1.1/CActiveRecord#findAllByAttributes-detail
you can try this:
$id = // code to set the id
$bookingRoom = BookingRoom::model()->findAllByPk($id);

MongoDB & PHP - How do I exclude results from a date range delimited query?

Long time peruser, first time question asker ...
Using PHP to query our MongoDB page visit log, I would like to get a set of records between two time periods, but exclude results that have a certain userAgent. I've figured out the time range but cannot find anywhere that explains the exclude.
Here's what I have for the query so far:
$dateRange = $collection->find(array("timeStamp" => array('$gt' => $start,
'$lt' => $end)));
Looking for code to complete the find function to exclude the records with a "userAgent" starting with "ELB"
What you're looking for is $ne or $nin, depending on whether the value you want to exclude is a single value or array of values. eg:
$dateRange = $collection->find(array("timeStamp" => array('$gt' => $start, '$lt' => end), 'userAgent' => array('$ne' => new MongoRegex('/^ELB/'))));
Documentation here:
http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24ne
http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24nin
You could append {$not: /^ELB/} to the mongo query.
Not really sure about the equivalent PHP but try something like this:
$dateRange = $collection->find(array(
'timeStamp' => array(
'$gt' => $start,
'$lt' => $end
),
'userAgent' => array(
'$not' => new MongoRegex('/^ELB/')
)
));

Insert Date into database table using Zend framework insert method

I have the foll code as:
$table_project_win = new Application_Model_DbTable_AfterWinProject();
$data_win = array(
'project_id' => $project_id,
'project_name' => $project,
'project_type_id' => $pro_type,
'start_date' => $dateStart,
'end_date' => $dateEnd,
'project_size' => $size,
'project_description' => $pro_des
);
$table_project_win->insert($data_win);
Here I get the $dateStart and $dateEnd variabled using as:
$dateStartt = $this->_getParam('dateStart');
echo 'date Start: '.$dateStartt;
$dateStart='"'.$dateStartt.'"';
$dateEndd = $this->_getParam('dateEnd');
$dateEnd='"'.$dateEndd.'"'
By using getParam I get the value of the date that the user has given input But when i will insert it into the database I use as
$dateStart='"'.$dateStartt.'"';
$dateEnd='"'.$dateEndd.'"'
But in the database table the value for date inserted is '0000-00-00' While when I echo the $dateStart which I have got through getParam It gives the correct value as '2012-12-11'.What is the reason of it??What Should I do??
replace $dateStart='"'.$dateStartt.'"';
with
$dateStart= $dateStartt ;
or
$dateStart='`'.$dateStartt.'`';

Categories