The identity ID is returned but not recorded in the table - zend-framework

I am building a project in Zand 1.1.
I created 'email' table and I want to enter record with the insert method. My code looks like:
$this->_db_table = new Mconsole_Model_DbTable_Emailmessages(array(
'db' => parent::getDb()
));
$this->_db_table->insert('email',data);
This code return last inserted id(for example '17') but no realy record inserted in the table. Can anyone help me?

Related

How to select a column from table/collection using odata URI?

I am using mongodb and odata.
I want to select name field alone for particular user id. (i.e) select name from userdata where userid=1;
/*my collection schema - userdata*/
{
id:number,
userid:string,
name:string,
data:object
}
I tried http://localhost:27017/userdata?$format=json&$filter=userid eq '1'&$select=name
Instead of getting name file alone I got whole object/document that matches userid=1. What I am doing wrong here?
I spoted the problem after the comment from #jps.
The issue is not with query, the problem is with data model which I am using is mismatching with my database schema (i.e) I missed out name field in model, so it is returning whole collection.
Now model is fixed, so the service is responding back with names for given userid.

Autoincrement postgre DB id field in RAILS

I am trying to make an autoincrement id field into a postgre DB in rails. I am trying to get the maximum id and then just add +1 and pass it down to create method. I am not quite sure what I am doing wrong, but if I have 4 projects and add the 5th one, that is getting id 5, but if I delete that one and try to add another one, that's getting id 6, not 5 again. Here is my code:
# GET /admin/projects/new
def new
#project = Project.new
#project.id = Project.maximum(:id).next.to_i
respond_to do |format|
format.html # new.html.erb
end
end
# GET /admin/projects/1/edit
def edit
#project = Project.find(params[:id])
end
# POST /admin/projects
def create
#project = Project.create(params[:project])
respond_to do |format|
if #project.save
format.html { redirect_to projects_path, notice: 'Project was successfully created.' }
else
format.html { render action: "new" }
end
end
end
I tried to add #project.save to new, but as much as I understand, the .save should only be done in the create method. Any ideas will be kindly appreciated :D
The auto increment fields goes up acordingly to a sequence, if the value 5 has already been taken(even if the entre has been deleted after, but the 5 has been associated to a record) the sequence dont stop, it will give you the 6th and so on...
Thankfully you can reset the sequence by 2 methods.
1- Truncate the table, truncate also resets the sequences to 1.
2- Use the Alter sequence method, you have here the official documentation for this method and here another question regarding this same matter with an example to solve it.
wich is:
ALTER SEQUENCE ${table}_${column}_seq RESTART WITH 1453
Hope it helps.
UPDATE 1:
With the update info on the coments, the awnser changes.
it is needed to create a custom method that handles that.
Project model
before_create :check_max_value
def check_max_value
self.project_id = Project.last.project_id + 1
end
you need to create a migration to add the "project_id" to the table, that field is the name of that "id you want".
You really shoudn't use the "ID" field created by default by rails.
A few problems here,
First, is the :id field actually in the view in a hidden field? If not, it's not returned in the params.
Second, if you did explicity set :id to the value of a deleted record, PostgreSQL will complain about a duplicate key value regardless. The id is still being used, you just can't access the record.
Third, you should not be assigning a value to :id, ever. :id has only one purpose and that's to provide a unique key for the record in the database, it should not have any other ancillary meaning. It increments automatically, that's all part of how the PostgreSQL adapter works.
If you want some sort of sequential numbering that can be reset, use a different, new field.

How to compare 2 'created_at' dates in Laravel 4.2 Mongodb

In my application the user can view an image and click on the 'next' or 'previous' photo button. Currently, the logic for pulling the next / previous image urls is based on the 'created_at' field. I am trying to pick all documents from my collection where the 'created_at' date is greater than the current resource's (being viewed by the user) value. However, the first document that is returned has the same 'created_at' value as my current resource's 'created_at' field.
Eloquent Query:
$photos = Photo::where('_id', '!=', $this->_id)
->where('created_at', '>', $this->created_at)
->orderBy('created_at', 'ASC');
Using:
- Laravel 4.2
- Mongodb 2.6.10
- Library: jenssegers/mongodb 2.*
Any help will be much appreciated!
Thank you
i have one idea to your question....
1)add viewed_at column in photo table
2)onlcik function with update table value 'viewd_column' using date('Y-m-d H:i:s'); via ajax when user click image or next button
3)compare the created_at with viewed_at like below query
u need jquery onclick function here...for when user view the image that time the value stored in your table ..if u have created_at column...create another one column viewed_at now u can update that table column when user click that source image..then now u can get the click date from viewed_at column... now compare both date
$photos = Photo::where('_id', '!=', $this->_id)
->where('created_at', '>', $this->viewed_at)
->orderBy('created_at', 'ASC');
Its help to u.. thank u .. all the best.. if u have doubt ask to me.. i will help u..

How to perform an INSERT INTO SELECT query in ZF2

What’s the best way to perform an INSERT INTO SELECT query in ZF2?
I need to develop a function in ZF2 that selects a subset of records from one table and inserts those records into another table. If I were programming in SQL, the statement would look like this:
INSERT INTO target (tgt_col1, tgt_col2)
SELECT 'flag' as marker, src_col2 FROM source
WHERE src_col1='mycriteria'
I’ve searched the Doctrine docs and cannot find an INSERT method. I’ve posted a similar question under the Doctrine tag and the lack of response leads me to believe that INSERT INTO SELECT is too complex for Doctrine to handle.
In ZF2 it appears that I “could” useZend\Db\Sql\Sql. However, for that solution, SELECT and INSERT are two separate functions and it looks like the INSERT function only handles a single record at a time. Therefore, a Zend\Db\Sql\Sql solution would require 1) a group of statements to select the data from the source table, 2) perhaps a function to convert that data object to an array, and 3) a foreach function to cycle through the data array and individually add each record to the target table via 4) a group of insert statements. This seems cumbersome compared to the single statement that’s possible in SQL.
Is there a better way?
If its TableGateway then it definitely works with the latest version of ZF2 i.e. 2.3.2
With reference with the Album module -
Created a duplicate table of album and named it album_old.
Emptied the album table.
So now album table is empty and album_old has the values.
To copy the records from album_old to album, did this -
use Zend\Db\Sql\Select;
....
use Zend\Db\Sql\Insert;
$select = new Select('album_old');
$select->columns(array('artist', 'title', 'cover', 'extra'));
$insert = new Insert();
$insert->into('album');
$insert->columns(array('artist', 'title', 'cover', 'extra'));
$insert->values($select);
//OR
$insert->select($select);
$this->tableGateway->insertWith($insert);
All the values from album_old were inserted into album table.
Ref: https://github.com/zendframework/zf2/blob/master/library/Zend/Db/Sql/Insert.php
Here you will find that the function values() can take array or instanceof Select
and there is a new select() function too.

Get next available auto_increment ID in PostgreSQL - A better approach?

I'm new to postgreSQL, so would really appreciate any pointers from the community.
I am updating some functionality in the CMS of a pretty old site I've just inherited. Basically, I need the ID of an article before it is inserted into the database. Is there anyway anyway to check the next value that will be used by a sequence before a database session (insert) has begun?
At first I thought I could use SELECT max(id) from tbl_name, however as the id is auto incremented from a sequence and articles are often deleted, it obviously won't return a correct id for the next value in the sequence.
As the article isn't in the database yet, and a database session hasn't started, it seems I can't use the currval() functionality of postgreSQL. Furthermore if I use nextval() it auto increments the sequence before the data is inserted (the insert also auto-incrementing the sequence ending up with the sequence being doubly incremented).
The way I am getting around it at the moment is as follows:
function get_next_id()
{
$SQL = "select nextval('table_id_seq')";
$response = $this->db_query($SQL);
$arr = pg_fetch_array($query_response, NULL, PGSQL_ASSOC);
$id = (empty($arr['nextval'])) ? 'NULL' : intval($arr['nextval']);
$new_id = $id-1;
$SQL = "select setval('table_id_seq', {$new_id})";
$this->db_query($SQL);
return $id;
}
I use SELECT nextval('table_id_seq') to get the next ID in the sequence. As this increments the sequence I then immediately use SELECT setval('table_id_seq',$id) to set the sequence back to it's original value. That way when the user submits the data and the code finally hits the INSERT statement, it auto increments and the ID before the insert and after the insert are identical.
While this works for me, I'm not too hot on postgreSQL and wonder if it could cause any problems down the line, or if their isn't a better method? Is there no way to check the next value of a sequence without auto-incrementing it?
If it helps I'm using postgresql 7.2
Folks - there are reasons to get the ID before inserting a record. For example, I have an application that stores the ID as part of the text that is inserted into another field. There are only two ways to do this.
1) Regardless of the method, get the ID before inserting to include in my INSERT statement
2) INSERT, get the the ID (again, regardless of how (SELECT ... or from INSERT ... RETURNING id;)), update the record's text field that includes the ID
Many of the comments and answers assumed the OP was doing something wrong... which is... wrong. The OP clearly stated "Basically, I need the ID of an article before it is inserted into the database". It should not matter why the OP wants/needs to do this - just answer the question.
My solution opted to get the ID up front; so I do nextval() and setval() as necessary to achieve my needed result.
Disclaimer: Not sure about 7.2 as I have never used that.
Apparently your ID column is defined to get its default value from the sequence (probably because it's defined as serial although I don't know if that was available in 7.x).
If you remove the default but keep the sequence, then you can retrieve the next ID using nextval() before inserting the new row.
Removing the default value for the column will require you to always provide an ID during insert (by retrieving it from the sequence). If you are doing that anyway, then I don't see a problem. If you want to cater for both scenarios, create a before insert trigger (does 7.x have them?) that checks if the ID column has a value, if not retrieve a new value from the sequence otherwise leave it alone.
The real question though is: why do you need the ID before insert. You could simply send the row to the server and then get the generated id by calling curval()
But again: you should really (I mean really) talk to the customer to upgrade to a recent version of Postgres