I have the following conditions in my query:
'conditions' => array('Zona.nombre LIKE' => $buscar,
'date_format("%Y", Zona.created)' => 'date_format("%Y", NOW())',
How do I get result of current year ?
In CakePHP you can simply do this:
'YEAR(Zona.created)' => date('Y'),
It can be done something like that.
"DATE_FORMAT(Zona.created, '%Y') = " => "DATE_FORMAT(YEAR(), '%Y')",
Related
I am using Laravel with mongodb(Jenssegers), i have array data like following,
$insert[] = ['sub_id'=>$loggedin,
'userid' => $row->userid,
'username' => $row->username,
'email' => $row->email,
'mobileno' => $row->mobileno,
'manager_mail' => $row->manager_mail,
'roleid' => $userrole->roleid];
my insert query is,
$user->save($insert);
But its not working, please suggest any solution?
You need to use create() or insert() methods:
User::create($insert);
Or:
User::insert($insert);
Or:
DB::table('table_name')->insert($insert);
Eloquent save method accept 1D array. But you provided 2D. Please try following code:
$insert = ['sub_id'=>$loggedin,
'userid' => $row->userid,
'username' => $row->username,
'email' => $row->email,
'mobileno' => $row->mobileno,
'manager_mail' => $row->manager_mail,
'roleid' => $userrole->roleid];
$user->save($insert);
Hi i am working on backend of web application & want to find the documents from mongodb database that contain key active_status with value set to both 1 & 2. With mongodb PHP i am confused of how to find with both parameters in single query.
My query was this:
$mongoDb = MongoDbConnector::getCollection("endusers");
$endUserData = $mongoDb->find(array('active_status' => 1, '$and' => array('active_status' => 2)));
I have to fetch the users whose active_status should be 1 & 2. The above query doesnt seems to work. What is the right one for that?
Thanks on advance for quick response.
You have $and the wrong way around. Both arguments need to be included:
$endUserData = $mongoDb->find(array(
'$and' => array(
array( 'active_status' => 1 )
array( 'active_status' => 2 )
)
));
And since that would only make sense when looking for both elements within an array element, then you should instead use $all, which is shorter syntax:
$endUserData = $mongoDb->find(array(
'active_status' => array( '$all' => array(1,2) )
));
I should add that unless you intend to match a document like this:
{ "active_status" => [1,2] }
The you do not in fact want $and at all, but rather you want $or or better yet $in for multiple possible values on the same field:
$endUserData = $mongoDb->find(array(
'active_status' => array( '$in' => array(1,2) )
));
This matches documents like this:
{ "active_status": 1 },
{ "active_status": 2 }
Is it possible to set a date field (capturing a start and end time) on a drupal entity WITHOUT using entity metadatawrapper?
with EWM I could do (with a corresponding ->save() after, of course)
$wrapper->field_custom_date->set(array(
'value' => $date_start_value,
'value2' => $date_end_value,
));
I've tried
$entity->field_custom_date = array(LANGUAGE_NONE => array(0 => array(
'value' => $date_start_value,
'value2' => $date_start_value,
)));
and a few other very similar variations to no avail. I've tried Googling but I can only find solutions and examples using EMW. Is this possible or do I have to use the entity metadata wrapper?
Thanks!
Try this
$entity->field_custom_date[LANGUAGE_NONE][0]['value'] = $date_start_value;
$entity->field_custom_date[LANGUAGE_NONE][0]['value2'] = $date_end_value;
I am using ICHIKAWAY's mongodb driver for cakephp.
One thing that I don't really get is how to perform a "LIKE" statement in cakephp using MONGODB.
I tried this statement:
$users = $this->User->find('all', array('conditions' => array('data_type' => 'user', 'profile.firstname LIKE' => '%'.$string)));
but its not working since "LIKE" is an mysql function.
Thanks for the suggestions.
Use MongoRegex
Mongo DB has a LIKE operator - it's simply a regular expression, to use it:
$users = $this->User->find('all', array(
'conditions' => array(
'data_type' => 'user',
'profile.firstname' => new MongoRegex("/$string/i")
)
));
There's an SQL Compatibilty behavior
The Mongodb driver contains a behavior providing sql syntax compatibility. To use it, simply make sure your model is using this behavior:
class User extends AppModel {
$actsAs = array(
'MongoDB.SqlCompatible'
)
}
And then you can use your query exactly as it appears in the question:
$users = $this->User->find('all', array(
'conditions' => array(
'data_type' => 'user',
'profile.firstname LIKE' => '%'.$string
)
));
User::model()->update($_id, array('$set' => array('d' => 1)[, array('deleted' => 1)[, array('w' => 2)]]);
I want to update user table with specified user id.
it's not working
The brackets are optional parts, this stems from the PHP manual ( i.e.: http://php.net/manual/en/function.pathinfo.php ) so:
User::model()->update($_id, array('$set' => array('d' => 1)
[, array('deleted' => 1)[, array('w' => 2)]]);
Will never work and will actually throw a syntax error in PHP in its current state. Instead you can try:
User::model()->update($_id, array('$set' => array('d' => 1)));