I have a query in CakePHPthat has a stored "datetime" field called DropIn.drop_in_time. I would like to only "find" entries where the DropIn.drop_in_time is > NOW() but am having trouble getting it to do that.
The condition DropIn.drop_in_time >' => 'NOW() didn't get the right results in the query below. Is there a better way to do it?
$requests = $this->DropIn->find('all', array(
'conditions' => array('DropIn.drop_in_time >' => 'NOW()', 'or' => array(array('DropIn.user_id' => $this->Auth->user('id')), array('DropIn.id' => $drop_in_ids))),
'order'=>array('DropIn.created'=>'DESC')));
If you separate the value as 'DropIn.drop_in_time' => 'NOW()', 'NOW()' is taken to mean the literal value string "NOW()". Just write it as one SQL fragment instead: 'DropIn.drop_in_time > NOW()'. Alternatively, use 'DropIn.drop_in_time >' => date('Y-m-d H:i:s').
If you really want to put DB expressions into CakePHP finds, you can use the expression method:
'DropIn.drop_in_time.' => $db->expression('CURDATE()');
However this is kinda of losing the point of the database abstraction that a framework provides, so do as suggested by deceze and compare it with date('Y-m-d H:i:s')
use DboSource::expression('NOW') instead of only NOW()
Related
It might be ridiculous quesion, but I can't find the way with googling.
I am making form like this.
$form = $this->createFormBuilder($searchTime)
->add('date','date',array(
"property_path" => false,
'years' => $years,
'data' => new \Datetime()
but it shows month select box as name such as jun,july,aug
however I want to show number here 6,7,8 ....
How can I make it ?
Maybe it's worth a try to specify a format, as the reference says:
$builder->add('date', 'date', array(
'widget' => 'choice',
'format' => 'yyyy-MM-dd',
// ...
));
The reference says, that if your widget is choice and you specify the format then the selects will be rendered in the order of the format. Maybe the select format is also will be adjusted to the format pattern.
(The month without leading zeros would be: yyyy-M-dd)
this is my first post here at stackoverflow and i'll try make sharp and short ;-)
I am running into problems when inserting a textarea form field using CKeditor into the database.
Here is my form element:
$form['add']['description'] = array(
'#wysiwyg' => true,
'#name' => 'description',
'#title' => t('description'),
'#type' => 'text_format',
'#base_type' => 'textarea',
);
When submitting the form i do get a SQL error like this because Drupal appends the placeholder elements 'value' and 'format'
db_insert failed. Message = SQLSTATE[21S01]:
Insert value list does not match column list:
1136 Column count doesn't match value count at row 1,
query= INSERT INTO {tablename} (description)
VALUES (:db_insert_placeholder_13_value, :db_insert_placeholder_13_format)
Unfortunately i exactly have to set up the textarea this way to make the CKeditor work. Can you advice me how to get rid of the
:db_insert_placeholder_13_value,
:db_insert_placeholder_13_format
to a single variable again?
I really appriciate your help
PROBLEM SOLVED --- silly me, should have used my glasses more :-D
Just use the correct array index inside the submit hook for saving the data to the database and it will work:
function MYMODULE_form_add_submit($form, &$form_state) {
...
'description' => $form_state['values']['description']['value'],
...
}
If you're just interested in the text itself (not the format), add a validation handler for the form and use something like this
function MYMODULE_form_name_validate($form, &$form_state) {
$form_state['values']['description'] = $form_state['values']['description']['value'];
}
A client requested this. He wants to allow multiple date formats for a birthday field. The documentation didn't give me any clues how to realize it and neither did google.
Anyone who experienced such a request before and has a lead how to achieve this?
Currently it looks like this:
$builder->add('xxx', 'birthday', array('widget' => 'single_text', 'invalid_message' => 'some message (dd-MM-yyyy)', 'format' => 'dd-MM-yyyy'))
If you want to handle different date formats in your form, you ought to look at DataTransformer
It helps you to transform data from one format to another, for example:
2013-03-26 ==transform==> 2013/03/26
2013.03.26 ==transform==> 2013/03/26
26.03.2013 ==transform==> 2013/03/26
Data transformer should NOT be used in this case.
The main point of data transformer is to convert a data back and forth between view <=> norm <=> model.
It's not your case, you don't want a bidirectional transformation.
What you want is to filter the data, and for that, you can use a form event:
$builder->add(
$builder
->create('xxx', 'birthday', [...])
->addEventListener(
FormEvents::PRE_SUBMIT,
function(FormEvent $event) {
$value = $event->getData();
// Do the filtering you want (for example replacement of special chars with a dash)
// $value = preg_replace('[^\d]', '-', $value)
$event->setData($value);
}
)
);
And you are done, the data is filtered on the PRE_SUBMIT event, before validation.
I wrote this example from memory and didn't tested it, maybe you'll should adapt it (and add your field option instead of [...].
Looking to incorporate a date filter within a Solr Index View. Presently the only option is for the user to manually enter a text date to filter by date. It would be nice to expose a date filter and search by M/d/Y using the 'Between' function which is available in the 'Content' View. Creating a 'Solr Index' View seems to remove that functionality.
I noticed that the Between operator is missing from handler_filter.inc within the Search API module:
public function operator_options() {
return array(
'<' => t('Is smaller than'),
'<=' => t('Is smaller than or equal to'),
'=' => t('Is equal to'),
'<>' => t('Is not equal to'),
'>=' => t('Is greater than or equal to'),
'>' => t('Is greater than'),
);
}
What other code would we need to modify in order to get a more user-friendly date filter available for the user to query?
You can use something like that:
$or = $query->createFilter('AND');
$or->condition('created', $start_date, '>=');
$or->condition('created', $end_date, '<=');
$query->filter($or);
in hook_search_api_query_alter.
Is there anyway say Drupal to validate form elements like email fields, passwords, numeric fields validate automatically lets say bind a system validator
$form['email] = array(
'#title' => t('Email'),
'#type' => 'textfield',
'#validate_as' => array('email', ...),
...
);
To validate a numeric field in Drupal use:
'#element_validate' => array('element_validate_number')
No need to create a custom validation function.
http://api.drupal.org/api/drupal/includes%21form.inc/function/element_validate_number/7
Rimian is both correct and wrong.
The good thing as Rimian points out, is that you can attach any validation function to your form fields using the #element_validate.
However I'm not aware of a set of form api validation functions you can call to test most common things like, if the value is:
a int
a positive int
a valid date (such a function exists in the date module though)
a email-address (you can use valid_email_address to check the email, but you need a function to raise validation error)
So while you can do this, it's a bit more work than you were hoping for, as you will need to create these validation functions yourself. But once you have done this, you can reuse them with #element_validate.
The use of #element_validate is mostly centered around complex validation fx date validation, location validation and such, as it requires some work to create these validation functions. Most of the time, you don't need to validate that many numbers etc (which you quite easily could do within a normal validation function using a loop). So I'm not sure how much this will be of help to you, but it's definitely a possibility.
The Form API Validation module does exactly what you request: http://drupal.org/project/fapi_validation
For client-side validation there is also http://drupal.org/project/clientside_validation (which can use rules provided by Form API Validation).
Yep!
Though I have not experimented with it much.
http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/6#element_validate
$form = array(
'#type' => 'fieldset',
'#title' => t('Input format'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => $weight,
'#element_validate' => array('filter_form_validate'),
);