How to add auto increment in field of SuiteCRM - php

I am using SuiteCRM-7.11.5 on Windows. I want to create an ID Label which shows itself auto incremented in the the "Create New Task" window. If showing auto incremented is not possible I at least want it to auto increment in the mySQL database.
I've found some question in Stack Overflow and on SuiteCRM forum (No Extension folder) which are all outdated or didn't work at all. The plugins are removed from github. Any help, hack or work around is appreciated. I am also new to suitecrm and mysql so step by step answer would be appreciated.
I also tried adding auto increment option in phpmyadmin but it throw error as
Incorrect column specifier for column 'id'

Auto Increment is definitely possible in suiteCRM, all you need to put a field using code like this.
'auto_number' =>
array(
'name' => 'auto_number',
'vname' => 'Serial No',
'type' => 'int',
'len' => 11,
'required'=>true,
'auto_increment' => true,
),

Create int type field and create before save logic hook. Add below code,
global $db;
$query = "SELECT MAX(field_name) as max_count FROM table where deleted=0";
$result = $db->query($query);
$row = $db->fetchByAssoc($result);
$max_number = $row['max_count'];
if(empty($max_number)){
$max_number = 1;
}
else{
(int)$max_ticket_number++;
}
if(empty($bean->field_name)){
$bean->field_name = $max_ticket_number;
}
}

Related

Column name must be either a string or an array. yii

while I am updating record it display above error.
message id seems like this - 1536126282209770000
$q = new CDbCriteria(array(
'condition' => 'tokenId = :btokenid',
'params' => array(
':btokenid' => $tokenId,
),
));
$record = self::model()->find($q);
$record->messageId = $messageId;
if (!$record->save()) {
$_errors = current($record->getErrors());
throw new Exception($_errors[0]);
}
I added 2 primary keys for table.
table structure:
After adding primary keys to table need to flush the cache
To refresh the database cache :
Load all tables of the application in the schema
Yii::app()->db->schema->getTables();
clear the cache of all loaded tables
Yii::app()->db->schema->refresh();
If you want to refresh only one table, you can also do :
Yii::app()->db->schema->getTable('tablename', true);
After that It works fine.

multiple value insert using saveField function or other function in cakephp

I insert multiple values into my database using saveField function. Here is my code.
$this->Fuel->saveField(
'device_id', $int_fuel_func[0],
'func_code', $int_fuel_func[1],
'device_data', $int_fuel_func[2]
);
It's not a form value. Value automatically insert when the page is refreshed.
It's only inserts first value 'device_id', $int_fuel_func[0],. Other two values are not insert into my database. Any one suggest me how to insert these values.
Thanks in advance
Cake version 2.7.5
You can simply do this as well.
$data = array(
'device_id' => $int_fuel_value[0],
'func_code' => $int_fuel_value[1],
'device_data' => $int_fuel_value[2]
);
$this->Fuel->save($data);
Also try this blog tutorial (cakephp blog tutorial) if you haven't been there before to be more familiar with these things.
My id is not auto Increment it is defined as varchar. Time is not include there also. I got my answer. I use it and it works.
$this->Fuel->set(
array(
'device_id' => $int_fuel_value[0],
'func_code' => $int_fuel_value[1],
'device_data' => $int_fuel_value[2]
)
);
$this->Fuel->save();
saveField function is only for updating one field! use updateAll and pass an array to it :
$this->Fuel->updateAll(
array(
'device_id', $int_fuel_func[0],
'func_code', $int_fuel_func[1],
'device_data', $int_fuel_func[2]
),
array('Fuel.id' => 234) // conditions for the Fuel you want to update
);

php yii2 how to make automaticly id in query yii2 insert?

i have a code like this ,
$request = Yii::$app->request;
$post = $request->post();
$filesettingid = $post['filesettingid'];
$checkboxValue = $post['selection'];
for($i=0;$i<sizeof($checkboxValue);$i++) {
$store = Yii::$app->db->createCommand('SELECT id FROM store WHERE port='.$checkboxValue[$i])->queryAll();
$storeid = $store[$i]['id'];
Yii::$app->db->createCommand()->insert('tes',
[
'id' => $i+1,
'filesetting_id' => $filesettingid,
'store_id' => $storeid
])->execute();
}
what i want is, each i insert the new data, id will generate automaticly like 1,2,3,4.
the problem in above code is, the ID always be 1.
is it possible to make it real?
so what i want is :
First time insert, id = 1, second is id = 2 , and that is happen automatically.
Have you considered setting database engine to auto increment values with each insert?
Take as an example Yii2 default user table. ID filed is auto incremented, and you don't have to worry about setting it problematically. Every time you send a new insert engine increments ID filed by itself.
See default migration under "advanced template"\console\migrations\m130524_201442_int. (your file name might be different depending on the Yii2 version)
$this->createTable('{{%user}}', [
'id' => $this->primaryKey(),
'username' => $this->string()->notNull()->unique(),
'auth_key' => $this->string(32)->notNull(),
'password_hash' => $this->string()->notNull(),
'password_reset_token' => $this->string()->unique(),
'email' => $this->string()->notNull()->unique(),
'status' => $this->smallInteger()->notNull()->defaultValue(0),
.........
], $tableOptions);
When setting 'id' to primary key database automatically knows to auto increment it. If you already have a table the ID field is not primary key you can use the followign migration:
$this->alterColumn('{{%databaseName}}', 'columnName', $this->integer()->notNull().' AUTO_INCREMENT');
You can also set it from management console, or run a SQL query. Depending on database engine you are using this might look a little different but the concept is the same.
MYSQL:
In MySQL workbench right click on table in question, select Alter Table and check NNm and AI next to column you want auto increment. See Screenshot
Or run command:
ALTER TABLE `dbName`.`nameOfTheTable` MODIFY `columnName` INT AUTO_INCREMENT NOT NULL;
I am a bit rusty on my SQL, so if it does not work let me know I will get you right command.
Hope this helps. Good luck.

$this->Model->id not working before saveAll in CakePHP

I have the following code in CakePHP 2:
$this->Order->id = 5;
$this->Order->saveAll(array(
'Order' => array(
'person_id' => $this->Session->read('Person.id'),
'amount' => $total,
'currency_id' => $code
),
'Lineitem' => $lineitems /* a correctly-formatted array */
));
I would expect this to update the row with the Primary Key of 5 in the Order table and then insert the Lineitem rows with an order_id of 5.
However, all it does is create a new row in Order and then use the new id from the new Order record to create the Listitem rows.
Note: I'm only setting the ID as above for debugging purposes and to easily demonstrate this question. In my final code, I'll be checking to see if there's already a pending order with the current person_id and doing $this->Order->id = $var; if there is and $this->Order->create(); if there isn't.
In other words, sometimes I will want it to INSERT (in which case I will issue $this->Order->create(); ) and sometimes I will want it to UPDATE (in which case I will issue $this->Order->id = $var; ). The test case above should produce an UPDATE but it's producing an INSERT instead.
Any idea what I am doing wrong here?
The array you pass to Model->saveAll() doesnt't contain the order's id, so Cake creates a new one. If you wanto to update an existing record, either you set the order id in the passed array, or you retrieve it with a find. The documentation explicitly remarks
If you want to update a value, rather than create a new one, make sure
your are passing the primary key field into the data array
$order = $this->Order->findById(5);
// ... modify $order if needed
$this->Order->saveAll(array('Order' => $order, 'LineItem' => $items));
In your case, you may want to use something like the following to be as concise as possible. Model::saveAssociated() is smart enough to create or update depending on the id, but you must provide suitable input. Model::read($fields, $id) initializes the internal $data: for an existing record all fields will be read from the database, but for a nonexistent id, you'll need to supply the correct data for it to succeed. Assuming an order belongsTo a customer, I supply the customer id if the order doesn't exist
// set the internal Model::$data['Order']
$this->Order->read(null, 5);
// You may want to supply needed information to create
// a new order if it doesn't exist, like the customer
if (! $this->Order->exists()) {
$this->Order->set(array("Customer" => array("id" => $customer_id)));
}
$this->Order->set(array('LineItem' => $items));
$this->Order->saveAssociated();
As a final note, it seems you are implementing a shopping cart. If that's the case, maybe it'd be clearer to use a separate ShoppingCart instead of an Order with a finalized flag.
Have you tried following:
$this->Order->saveAll(array(
'Order' => array(
'id' => 5,
'person_id' => $this->Session->read('Person.id'),
'amount' => $total,
'currency_id' => $code
),
'Lineitem' => $lineitems /* a correctly-formatted array */
));
Its pretty much the same what you did with :
$this->Order->id = 5;
Maybe that would fix your problem.
Cake is checking if you set id field and if its there it updates record, if not found it creates new record instead.
update:
Then maybe check before you saveAll if there is id field, then save result of check to some boolean and create array to save determined by this boolean for example:
if($id_exist) $order['Order']['id'] = 5;
$order['Order']['id'] = 5;
$order['Order']['person_id'] = $this->Session->read('Person.id'),
$order['Order']['amount'] = $total;
$order['Order']['currency_id'] = $code;
$this->Order->saveAll(array(
'Order' => $order,
'Lineitem' => $lineitems /* a correctly-formatted array */
));

CakePHP Merging date fields to the shortest

I have 2 dates (create, update) that i want to merge in a new column, selecting the newest date... how can I do it?
Here is the array creation:
$this->Message= array(
'fields' => array('Message.id','Message.type','Message.createdate','Message.updatedate'),
'conditions' => $cond);
$messages = $this->Message->find('all', $conditionsMessage);
Now I need another field (lets call it NewDate) Message.NewDate that gets the newest date from Message.createdate and Message.updatedate, so i can call it after in a view using $messages[NewDate]
Help plz...
Thx!
UPDATE:
It's not hard to loop over the array with something like
foreach($messages as $k => $m){
if(strtotime($m['Message']['updatedate']) > strtotime($m['Message']['createdate'])){
$messages[$k]['Message']['NewDate'] = $m['Message']['updatedate'];
}else{
$messages[$k]['Message']['NewDate'] = $m['Message']['createdate'];
}
}
ORIGINAL ANSWER:
I would think that your Message.updatedate would always be the newest date, so you could just select that. But assuming that's not the case for some reason, you can create a virtual field in your model:
public $virtualFields = array(
'NewDate' => "IF(Message.updatedate > Message.createdate, Message.updatedate, Message.createdate)"
);
This uses the MySQL IF() function. If you're not using MySQL you'd have to figure out how to do something similar with your database.
http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#function_if

Categories