Create Unique field in Codeigniter DBForge Migration - codeigniter

How can I make the financial_year field as unique using Codeigniter Dbforge migration?
function up() {
$this->dbforge->add_field(array(
'id' => array(
'type' => 'INT',
'constraint' => 11,
'unsigned' => TRUE,
'auto_increment' => TRUE
),
'financial_year' => array(
'type' => 'VARCHAR',
'constraint' => 20
),
'start_date' => array(
'type' => 'DATE'
),
'end_date' => array(
'type' => 'DATE'
),
'status' => array(
'type' => "ENUM",
'constraint' => "'Active','Inactive'",
'default' => "Active"
),
'created_on' => array(
'type' => 'TIMESTAMP'
)
));
$this->dbforge->add_key('id', TRUE); // add `id` as primary key
$this->dbforge->create_table('financial_year'); // create table schema
}

There is not an option to do that in the database driver.
either write the SQL to create the table manually, or (and this isn't what i would recommend) change the DB_forge.php and your database driver. (eg mysql_forge.php) to have unique keys. looking at the code i guess you would just have a class variable called 'unique_keys' and follow the code where 'keys', and 'primary keys' are to add unique keys in.
another option is to use the dbforge as written, then just modifiy the table after creation
$this->db->query('ALTER TABLE `financial_year` ADD UNIQUE INDEX (`financial_year`)');

This question is marked as CodeIgniter2 - #pgee70 Answer is correct
but
CodeIgniter3 supports the creation of unique fields.
Just add
'unique' => TRUE
to the field
In this case:
'financial_year' => array(
'type' => 'VARCHAR',
'constraint' => 20,
'unique' => TRUE
),
see
CodeIgniter user_guide

Related

How to change type of column table field in laravel backpack?

So my table would be looks like this one
[ // Table
'name' => 'options',
'label' => 'Options',
'type' => 'table',
'entity_singular' => 'option', // used on the "Add X" button
'columns' => [
'name' => 'Name',
'desc' => 'Description',
'price' => 'Price'
],
'max' => 5, // maximum rows allowed in the table
'min' => 0, // minimum rows allowed in the table
]
So the default for the columns is text, and i want to change it to choice. Anyone have an solution for this ?
Updating column attributes with migrations. Check documentation:
updating columns attributes in Laravel

added fields are not working in edit or create mode in backpack cms

In Backpack CMS framework I have created a table using migration fields like : 'parent_id' and 'is_active'.
I have added these fields in the crud controller using the following command:
$this->crud->addFields(array(
['name' => 'title',
'label' => 'Title',
'type'=>'text'],
['name' => 'parent_id',
'label' => 'Parent ID',
'type'=> 'number'],
['name' => 'is_active',
'label' => 'Is Active',
'type'=>'number']
), 'update/create/both');
It should work in edit mode and create mode like my other tables. It shows the defined fields in the create and update form but unfortunately they doesn't work and always return the default value or previous value in the record.
I've tried adding the field in single format but there was no use.
The problem is here: 'update/create/both' you should pick only one of the three options.
What you want is to use only both. But since it is the default value, you don't really need to add it to the end of the addFields function.
This will work:
$this->crud->addFields([[
'name' => 'title',
'label' => 'Title',
'type' => 'text'
], [
'name' => 'parent_id',
'label' => 'Parent ID',
'type' => 'number'
], [
'name' => 'is_active',
'label' => 'Is Active',
'type' => 'number'
]
]);
The problem was with the fillable variable..... Only the 'title' field was in the fillable variable of the related model.

Magento issue with adding validation into multi-select checkbox

I have a form in magento admin panel. In the form i have checkboxes which i can select multiple options or one. The issue is i am unable to put validations for that. Because without selecting any option i can save records. My code is as in below:
$fieldset-> addField('time_ranges', 'checkboxes', array(
'label' => Mage::helper('CheckoutTime')->__('Time Ranges'),
'required' => true,
'class' => 'required-entry',
'name' => 'time_ranges[]',
'values' => array(
array(
'label' => Mage::helper('CheckoutTime')->__('Education'),
'value' => 'education',
),
array(
'label' => Mage::helper('CheckoutTime')->__('Business'),
'value' => 'business',
),
array(
'label' => Mage::helper('CheckoutTime')->__('Marketing'),
'value' => 'marketing',
),
array(
'value' => 'investment',
'label' => Mage::helper('CheckoutTime')->__('Investment'),
)
),
));
Can anyone please tell me how to add validations into this form.
Thank You
Try by changing
'name' => 'time_ranges[]'
to
'name' => 'time_ranges'
This way is correct. There were some issues in other places in my coding. That's why it didn't work early. Or else this is the correct way to do that.

Specifying actions for programmatically created coupon code

Based off information found in a couple of posts online (http://www.magentocommerce.com/boards/viewthread/178767/ and http://marius-strajeru.blogspot.co.uk/2010/04/create-bulk-discount-rules.html), I am putting some code together to generate some coupon codes.
One piece I am stuck on is how to write the code to specify the "actions" specific conditions for the coupons use. That would be specified in the "Apply the rule only to cart items matching the following conditions" section of the Actions tab in the Magento admin system.
In the Magento admin system, I would build the following line:
Category is not one of 10,20,30
What I need to know is how to replicate this in code.
I currently have the following, which doesn't appear to be working - at least, when I check the generated coupon code, the action values I require are missing.
$actions = array();
$actions[1] = array(
'type' => 'salesrule/rule_condition_category',
'aggregator' => 'all',
'value' => 1,
'new_child' => ''
);
$actions['1--1'] = array(
'type' => 'salesrule/rule_condition_category',
'attribute' => 'category_ids',
'operator' => '!()',
'value' => '932,341,800',
'is_value_processed' => 0,
);
$model->setData('actions',$actions);
I am assuming that the code is simply wrong, although not tripping up the system.
How I might achieve what I need?
Here's what I ended up with, which works great!
$conditions = array(
"1" => array(
'type' => 'salesrule/rule_condition_combine',
'aggregator' => 'all',
'value' => 1,
'new_child' => false
),
"1--1" => array(
'type' => 'salesrule/rule_condition_product_found',
'value' => 1,
'aggregator' => 'all',
'new_child' => false
),
"1--1--1" => array(
'type' => 'salesrule/rule_condition_product',
'attribute' => 'category_ids',
'operator' => '!()',
'value' => '10,20,30'
)
);
$actions = array(
"1" => array(
"type" => "salesrule/rule_condition_product",
"aggregator" => "all",
"value" => "1",
"new_child" => false
),
"1--1" => array(
"type" => "salesrule/rule_condition_product",
"attribute" => "category_ids",
'operator' => '!()',
'value' => '10,20,30'
)
);
$rule->setData('conditions',$conditions);
$rule->setData("actions",$actions);

Kohana 3.2 : Error reading session data

I'm working on a module (a simple cms) with Kohana 3.2 and i'm getting this exception "Error reading session data."
I'm using native session and the funny thing is if i set a "default" group database connection the error isn't showed... (i'm using a custom connection group and i've set this database connection group to the user,role and user_token models).
here's my config file
auth.php
return array(
'driver' => 'orm',
'hash_method' => 'sha256',
'hash_key' => 'just a test 1',
'lifetime' => 1209600,
'session_type' => 'native',
'session_key' => 'just a test 2',
// Username/password combinations for the Auth File driver
'users' => array(
// 'luca' => 'e12afe0d3ead3d36191d86229d27057d96d9f2e063fe6f3e86699aaab5310d42'
// 'admin' => 'b3154acf3a344170077d11bdb5fff31532f679a1919e716a02',
),
);
session.php
return array(
'native' => array(
'name' => 'session_native',
'lifetime' => 43200,
),
'cookie' => array(
'name' => 'session_cookie',
'encrypted' => TRUE,
'lifetime' => 43200,
),
'database' => array(
'name' => 'session_database',
'encrypted' => TRUE,
'lifetime' => 43200,
'group' => Pencil::db_group(),
'table' => 'sessions',
'columns' => array(
'session_id' => 'session_id',
'last_active' => 'last_active',
'contents' => 'contents'
),
'gc' => 500,
),
);
You set encrypted to true, so you need an encrypt key. In your config/encrypt.php add this:
<?php
return array(
'default' => array(
'key' => 'MY_RANDOM_KEY_I_MADE_UP_ALL_BY_MYSELF',
),
);
I would keep session_key set to 'auth_user' instead of your random key as well. I think key in that circumstance is not the same as a hash key.
Check your logs in application/logs to see if anything else is missing.

Resources