While adding phinx migration, Is it possible to addColumn enum with default value?
To achieve:
1. All existing rows to have the default value ('active' in this case)
2. All new entries to have the default value ('active' in this case)
Something I am building up is:
$this->table('my_table')
->addColumn('status', 'enum', ['values' => ['active', 'cancelled', 'expired']])
->create();
//how to add 'active' as default?
I am running Phinx 0.5.0, and the way I accomplished the default value for an ENUM type is by adding in the 'default' => '' option.
Example:
public function change()
{
$table = $this->table('example');
$table->addColumn(
'example_column',
'enum',
array(
'values' => ['abc','def','ghi'],
'default' => 'abc'
)
)
->update();
}
Hope this helps!
Related
When I selected multiple values from select list then array returns the first value empty.
= f.select :assignedto, options_from_collection_for_select(User.all, 'name', 'name',f.object.assignedto),{}, { :multiple => true}
I tried with {:include_blank => false} and {:include_hidden => false} but this is not working for rails 3.2.11. I have many solutions to handle this empty value in the controller but I want to stop adding empty value in the array.
Properly because the second arg is 'name' instead of 'id'.
Try
options_from_collection_for_select(User.all, 'id', 'name', f.object.assignedto)
I am using best_in_place_if for inline editing. Here I want to catch the id of current element edited by using the ajax success event of best_in_place.
Below is code snippet I am trying to add id attribute. But when I inspect the html, the value for id attribute is the default value generated by the bes_in_place. As per their doc, its mentioned that the default value can be changed by providing our own value.
The default value for id attribute is shown as id="best_in_place_trip_32_is_active" and I want is only id=32
best_in_place_if(current_user.admin,trip,:is_active, :type => :checkbox, :classes => 'trip_disable', :id => trip.id)
Please let me know what I am missing.
Firstly, you should not try to use a numeric-only ID attribute for the element, this violates the HTML specs.
Then, to access the id of the currently edited record in the success callback, you can add the record id to the data attribute instead. Something like this should work:
# adding the record ID to the data attribute
best_in_place_if(current_user.admin, trip, :is_active, :as => :checkbox, :class => 'trip_disable', :data => { :id => trip.id })
# accessing the record ID in the success calback
$('.best_in_place.trip_disable').bind("ajax:success",
function(){
alert('Record ID is '+$(this).data('id'));
});
See the Best in place docs for more info.
When setting the pattern for the currencyFormatter, you can use:
#,##0.###
To get:
1,0000.00
But I want to get:
1 0000.00
How do I alter the format string to get that?
Try with the use of 'symbol' attribute to overwrite your own curreny.
echo Zend_Currency->toCurrency(
4000,
array(
'currency' => "USD",
'symbol' => ''
)
);
I'm trying to use multi_field syntax of elasticsearch in combination with Elastica. I create an index and an mapping which looks like this:
$mapping->setProperties(array(
'id' => array('type' => 'string', 'include_in_all' => true),
'title' => array('type' => 'string', 'include_in_all' => true),
'publisher' => array('type' => 'multi_field', 'include_in_all' => TRUE, 'fields' =>
array('publisherName' => array('type' => 'string', 'index' => 'analyzed'),
'untouched' => array('type' => 'string', 'index' => 'not_analyzed')
)
));
So far, so good. I can run queries against the title field.
But when I try to query the field "publisher" in http://example.com:9200/_plugin/head/ I'm not able to select the field publisher or to create a structured query. I looks, that the field publisher is not in the index.
But I can build facets on publisher.untouched which works very well. Whats wrong in my mapping? I need to search for the publisher.
See the docs on multi_field mapping. Looks like you need to set a default field by changing 'publisherName' to just 'publisher'.
I need to have a start and end time widget on a form.
i.e. the user selects a date and then selects the start and end time.
In the standard date widget, you can select a date and a time, however I need to be able to select a finish time too.
Have any of you done it before?
I could create 3 separate widgets:
Date
Start Time
End Time
When the form is saved, I'll do an update on the object to combine all values into one. For example, I'll get the date and add the start time to it and then save to the field "start_date" and then I'll get the date and add the end time to it and then save to the field "end_date". It does however seem a very long winded way to do something which should be fairly trivial for a form framework.
Is this how you'd do it? Thanks guys!
I think what you want to want achieve with symfony forms is pretty easy. You are right, you need three separate widgets and three separate validators, there isn't an out of the box solution for this exact situation.
In your form configure method you would have something like:
$this->setWidgets(array(
'date' => new sfWidgetFormDate(),
'time_start' => new sfWidgetFormTime(array('label' => 'Start Time', 'with_seconds' => false)),
'time_finish' => new sfWidgetFormTime(array('label' => 'End Time', 'with_seconds' => false)
));
$this->setValidators(array(
'date' => new sfValidatorDate(), // by default outputs in format Y-m-d
'time_start' => new sfValidatorTime(), // by default outputs in format H:i:s
'time_finish' => new sfValidatorTime(),
));
Let's assume that the object has two properties, as you suggested, both are datetime fields.
In your action you would have something like the following to set the datetime fields:
$values = $this->form->getValues();
$object->setStartDateTime(sprintf('%s %s', $values['date'], $values['time_start']));
$object->setFinishDateTime(sprintf('%s %s', $values['date'], $value['time_finish']));
Edit: another suggestion is not use the built in time widget sfWidgetFormTime as it can look pretty ugly. You can simply use a normal text box (centre aligned, with maxlength=5), and the sfValidatorTime validator will still work perfectly.
Hope that helps.
Your requirement sounds application-specific and not really something the Symfony form framework would help you with out-of-the-box.
I would go with your suggestion of generating start_date and end_date from the output of the three widgets, or if your application needs to return date, start_time and end_time separately later, then possibly just save three values separately and manipulate them when queried.
So, I've done the code for it and it works very well. I've removed all the unnecessary widgets and validators. Here is how I did it:
class VisitForm extends BaseVisitForm
{
private function getMinutes()
{
$minutes = array();
for($i = 0; $i < 60; $i = $i + 5)
{
$minutes[$i] = sprintf('%02d', $i);
}
return $minutes;
}
public function configure()
{
$this->setWidgets(array(
'id' => new sfWidgetFormInputHidden(),
'date' => new sfWidgetFormJQueryDate(array('date_widget' => new sfWidgetFormDate(array('years' => $years, 'can_be_empty'=> false)), 'image' => '/images/icons/calendar.png', 'format'=>'%day%/%month%/%year%')),
'start_time' => new sfWidgetFormTime(array('with_seconds' => false,'can_be_empty'=> false, 'default' => '08:00', 'minutes'=> array_combine($this->getMinutes(), $this->getMinutes()))),
'end_time' => new sfWidgetFormTime(array('with_seconds' => false,'can_be_empty'=> false, 'default' => '08:00', 'minutes'=> array_combine($this->getMinutes(), $this->getMinutes())))
));
$this->setValidators(array(
'start_time' => new sfValidatorTime(),
'end_time' => new sfValidatorTime(),
'date' => new sfValidatorDate(),
));
$this->widgetSchema->setNameFormat('visit[%s]');
$this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);
}
protected function doUpdateObject($values)
{
$this->getObject()->setStartDate(sprintf("%s %s", $values['date'], $values['start_time']));
$this->getObject()->setEndDate(sprintf("%s %s", $values['date'], $values['end_time']));
parent::doUpdateObject($values);
}
public function updateDefaultsFromObject()
{
if(!$this->getObject()->isNew())
{
$this->setDefault('date', $this->getObject()->getDateTimeObject('start_date')->format('Y-m-d'));
$this->setDefault('start_time', $this->getObject()->getDateTimeObject('start_date')->format('H:i'));
$this->setDefault('end_time', $this->getObject()->getDateTimeObject('end_date')->format('H:i'));
}
parent::updateDefaultsFromObject();
}
}