Through a logichook, I'm trying to insert a new Quote record through the bean class. The "quote_num" field is an auto-increment field. When I try this code below, instead of inserting in MySQL with the quote_num i specify, it uses the next number in the auto-increment sequence. I know I could just use an SQL INSERT but I'm trying to stick to the bean. Any ideas?
$newQuote = new Quote();
$newQuote->name = "Web Order";
$newQuote->quote_num = 902011;
$newQuote->quote_order_c = $orderorcredit;
$newQuote->save();
For Auto Increment : You can do like below ,
$count = "SELECT IFNULL(MAX(your field), 0) as count FROM table;
$count = '';
while ($row = $GLOBALS['db']->fetchByAssoc($result)) {
$count_coc = $row['count'];
}
$bean->ignore_update = true;
$bean->number_c = $count_coc + 1;
$bean->save();
Related
So I tried this but it didn't work (on isServer) :
var tableName= "";
(...)
if (silly_cond === 1){
tableName = "Table1";
}else{
tableName = "Table2";
}
TableCol = new Mongo.Collection(tableName);
For some reason I can't get it to work. It seems to only accept
TableCol = new Mongo.Collection("Table1");
The idea was to fetch the tablename from the table ID, and apply the same JS to different tables (on different templates). What am I doing wrong?
You need to declare tableName outside the functions, otherwise it can't be seen.
var tableName = "";
if (silly_cond === 1){
tableName = "Table1";
}else{
tableName = "Table2";
}
TableCol = new Mongo.Collection(tableName);
I ended up using the dburles:mongo-collection-instances package. It let's me access any collection by the collection name. So in my example:
TableCol = new Mongo.Collection("Table1");
Using the above package I just write for example:
var dbvar = "Table1";
Meteor.Collection.get(dbvar).find()
and this way I can use variables to get collections.
I've been using JPA to insert entities into a database but I've run up against a problem where I need to do an insert and get the primary key of the record last inserted.
Using PostgreSQL I would use an INSERT RETURNING statement which would return the record id, but with an entity manager doing all this, the only way I know is to use SELECT CURRVAL.
So the problem becomes, I have several data sources sending data into a message driven bean (usually 10-100 messages at once from each source) via OpenMQ and inside this MDB I persists this to PostgreSQL via the entity manager. It's at this point I think there will be a "race condition like" effect of having so many inserts that I won't necessarily get the last record id using SELECT CURRVAL.
My MDB persists 3 entity beans via an entity manager like below.
Any help on how to better do this much appreciated.
public void onMessage(Message msg) {
Integer agPK = 0;
Integer scanPK = 0;
Integer lookPK = 0;
Iterator iter = null;
List<Ag> agKeys = null;
List<Scan> scanKeys = null;
try {
iag = (IAgBean) (new InitialContext()).lookup(
"java:comp/env/ejb/AgBean");
TextMessage tmsg = (TextMessage) msg;
// insert this into table only if doesn't exists
Ag ag = new Ag(msg.getStringProperty("name"));
agKeys = (List) (iag.getPKs(ag));
iter = agKeys.iterator();
if (iter.hasNext()) {
agPK = ((Ag) iter.next()).getId();
}
else {
// no PK found so not in dbase, insert new
iag.addAg(ag);
agKeys = (List) (iag.getPKs(ag));
iter = agKeys.iterator();
if (iter.hasNext()) {
agPK = ((Ag) iter.next()).getId();
}
}
// insert this into table always
iscan = (IScanBean) (new InitialContext()).lookup(
"java:comp/env/ejb/ScanBean");
Scan scan = new Scan();
scan.setName(msg.getStringProperty("name"));
scan.setCode(msg.getIntProperty("code"));
iscan.addScan(scan);
scanKeys = (List) iscan.getPKs(scan);
iter = scanKeys.iterator();
if (iter.hasNext()) {
scanPK = ((Scan) iter.next()).getId();
}
// insert into this table the two primary keys above
ilook = (ILookBean) (new InitialContext()).lookup(
"java:comp/env/ejb/LookBean");
Look look = new Look();
if (agPK.intValue() != 0 && scanPK.intValue() != 0) {
look.setAgId(agPK);
look.setScanId(scanPK);
ilook.addLook(look);
}
// ...
The JPA spec requires that after persist, the entity be populated with a valid ID if an ID generation strategy is being used. You don't have to do anything.
If I have a main table, lets say orders, and a sub table of items and the items table has a fields for item number BUT it also has a nullable (optional) field for color that applied only to certain items. How would I update the items table, at the same time as the orders table, using Entity Framework?
Here is a code example of what I have so far. Two problems, I'm only entering one of my items and, from what my research indicates, I can't add another field to the items table?
foreach (Guid c in AllItems)
{ Items.OrderItemID = Guid.NewGuid();
ITemsOrderID = order.OrderID;
ITems.ItemID = c;
If (ItemID = ItemThatLetsYouChoseAColorID)
{
Items.ItemColorID = ColorID;
} else {
Items.ItemColorID = null;
}
}
context.Orders.AddObject(Orders);
context.Items.AddObject(Items);
context.SaveChanges();
My Orders table gets a record inserted, and the Items gets ONE record inserted. I'm missing something basic here, I'm afraid. BTW, this is Entity Framework 4.0, which. I believe, does not require the use of EntityKey.
You're adding an object to the Items collection only one time after the scope of your foreach.
Have you tested something like:
foreach (Guid c in AllItems)
{
var Item = new Item();
Item.OrderItemID = Guid.NewGuid();
Item.OrderID = order.OrderID;
Item.ItemID = c;
If (ItemID = ItemThatLetsYouChoseAColorID)
{
Item.ItemColorID = ColorID;
}
else
{
Item.ItemColorID = null;
}
context.Items.AddObject(Items);
}
context.Orders.AddObject(order);
context.SaveChanges();
And I'm not sure to understand what you mean by
I can't add another field to the items table
You should be more precise about what you actually expect. Insert a row, add a column in the table...? What is a "field"?
Here is the working code. I had the new Item outside the foreach item loop, so was overwriting the value. Also, I need to add each one to the context. I had a hard time with this, hope it helps someone else:
<-fill the order object->
foreach (Guid i in Items)
{
**Items item = new Items();**
item.ItemID = Guid.NewGuid();
item.OrderID = order.OrderID;
if (i == ItemWithColorGuid)
{
foreach (Guid c in Colors)
{
**Items color = new Items();**
color.ItemsID = Guid.NewGuid();
color.OrderID = order.orderID;
color.itemID = g;
color.colorID = c;
context.item.AddObject(color);
}
}
else
{
item.ItemID = i;
item.ColorID = null;
context.item.AddObject(item);
}
}
context.orders.AddObject(order);
context.SaveChanges();
How would I write an Zend DB query to select all from the column ID?
So far I have tried:
public function getLatestUserID()
{
$ids = $this->select()
->where('id = ?');
return $ids;
}
But to no avail.
You just want the id column,
You failed to call an execute command.
try:
//assuming you are using a DbTable model
public function getLatestUserID()
{
$ids = $this->fetchAll('id');
return $ids;
}
I would do it like this, because I use the select() object for everything:
public function getLatestUserID()
{
$select = $this->select();
//I'm not sure if $this will work in this contex but you can out the table name
$select->from(array($this), array('id'));
$ids = $this->fetchAll($select);
return $ids;
}
The first two examples should return just the id column of the table, now if you actually want to query for a specific id:
public function getLatestUserID($id)
{
$select = $this->select();
$select->where('id = ?', $id);
//fetchAll() would still work here if we wanted multiple rows returned
//but fetchRow() for one row and fetchRowset() for multiple rows are probably
//more specific for this purpose.
$ids = $this->fetchRow($select);
return $ids;
}
make sure your class containing getLatestUserID does extend Zend_Db_Table_Abstract also :
$ids = $this->select()->where('id = ?'); can't work because where('id = ?'); expects an id value like where('id = ?', $id);
if what you want is the latest inserted row's Id use :
$lastInsertId = $this->getAdapter()->lastInsertId();
(however if you are using an oracle database this will not work and you should use $lastInsertId = $this->getAdapter()->lastSequenceId('USER_TABLE_SEQUENCE'); )
I am new to piwik.Please help me in my issue.
Issue: I have to create 4 levesl of subtables.Currently I can able to create upto 2nd level,I mean table with one subtable per row.
Basically If I click on a table row I shoud get the subtable.If I click on subtable row again it should show inner subtable.
I need to create Table->subtable->subtable->subtable.
My code :
function getpageViewsLevel1($idSite, $date, $period)
{
$query = "select * from....";
$result = Piwik_FetchAll($query, array($idSite, $dateStart, $dateEnd));
// convert this array to a DataTable object
$dataTable = new Piwik_DataTable();
//Add subtable to each result
foreach ($result as $arr){
$piwik_row = new Piwik_DataTable_Row;
$piwik_row->setColumns($arr);
$subDataTable = new Piwik_DataTable();
$piwik_row->addSubTable($subDataTable);
$dataTable->addRow($piwik_row);
}
return $dataTable;
}
function getpageViewsLevel2($idSite, $date, $period, $idSubtable)
{
// Find selected parent row and retrieve data
$dataTable_old = $this->getpageViewsLevel1($idSite, $date, $period);
$row_old = new Piwik_DataTable_Row;
$row_old=$dataTable_old->getRowFromIdSubDataTable($idSubtable+1);
$tmp=$row_old->getColumns();
//Using $actionName in DB Query
$actionName=$tmp['pageTitle'].'%';
//db query
$query = "select * ....";
$result = Piwik_FetchAll($query, array($idSite, $dateStart, $dateEnd, $actionName));
// convert this array to a DataTable object
//$dataTable = new Piwik_DataTable();
foreach ($result as $arr){
$piwik_row = new Piwik_DataTable_Row;
$piwik_row->setColumns($arr);
$subDataTable = new Piwik_DataTable();
$piwik_row->addSubTable($subDataTable);
$dataTable->addRow($piwik_row);
}
return $dataTable;
}
Till here works fine.For the 3rd level,I am not able to apply the same logic as I dont have the 2ng level table ID.
function getpageViewsLevel3($idSite, $date, $period, $idSubtable)
{
// Find selected parent row and retrieve data
$dataTable_old = $this->getpageViewsLevel2($idSite, $date, $period, ___???????????????);
Please help me how can I proceed with this issue.Please let me know is there any other solution to do this.
Though if I pass some number like '1' for testing,
$dataTable_old = $this->getpageViewsLevel2($idSite, $date, $period, 1);----->this is not working.
I need to use parent row info in my DB query.
Thanks in advance for your help.