how to get last inserted record in symfony1.4 - php

how to get last inserted record in symfony1.4
i do not have much knowledge of symfony.
and i need the the id of last inserted record in symfony1.4, so If you have any idea please let me know.
public function executeAddquestion(sfWebRequest $request)
{
$q=$this->getUser()->getAttribute('quiz');
$qs=$this->getUser()->getAttribute('questions');
$t = new test();
$t->setTestName($q);
$t->setTestQuestions($qs);
$t->save();
$t->getId();
}
the above code, which can i tried. but not got anything.
so please help me.

You are getting the id but not storing it anywhere?
Try the following:
$lastId = $t->getId();

Related

how to get ID without inserting any data into DB

I wrote a bot. And it scrapes the data and inserting it into DB. When I run the bot first time there is no problem. But when I run the bot second time error pops. Let me explain what I meant.
Here, I am inserting data into DB.
if (Menu::where("url", "=", $link."menu")->first()) {
$insertedMenuId = count($url);
} else {
$insertedMenuId = Menu::insertGetId($database);
$this->line("Menu Inserted.");
}
Using this $insertedMenuId to insert connected data into another table. But the problem here, if I already inserted the data. Then it's not giving me the ID. And $insertedMenuId being empty. So second piece of code is not starting. Because there is no id given.
I tried to solve the problem by counting the url. If the url is already inserted. But this time it's giving the wrong id.
If the record exists, just use its id.
$row = Menu::where("url", "=", $link."menu")->first()
if ($row) {
$insertedMenuId = $row->id;
} else {
$insertedMenuId = Menu::insertGetId($database);
$this->line("Menu Inserted.");
}

Getting null when using getLastInsertID in cakephp2.10

Getting empty value when calling getLastInsertID() in cakephp2.10
Mycode is
$this->query("INSERT INTO $savedtbl(ref_name,productdetails_id,users_id,type,image,saved_date,template_name) VALUES('$ref_name',{$paramsArray['Customimage']['productdetail_id']},'$uid','{$paramsArray['Customimage']['front_rear']}','$editedImg','$date','$template_name')");
$lastid = $this->getLastInsertID();
How to fix this? please help
As far as I know, Model::getLastInsertID() in Cake 2.x will return last inserted id only if that insert was made via Model methods, not plain SQL query. You should try this approach:
$this->Model->create();
$this->Model->set(...); //set your fields as needed
$this->Model->save();
$lastId = $this->Model->getLastInsertID();

PDO Insert Query Not Working For Integer

I have this code to insert a new row into MySQL DB using PDO:
$query = INSERT INTO asset_positions (pos_asset_id, pos_latitude, pos_longitude, pos_timestamp) VALUES (:pos_asset_id, :pos_latitude, :pos_longitude, :pos_timestamp)
$statement = $pdo->prepare($query);
$array = [':pos_asset_id' => 1, ':pos_latitude' => -8.5, ':pos_longitude' => 125.5, ':pos_timestamp' => 1398160487];
$statement->execute($array);
echo $pdo->lastInsertId();
The query runs without any error shown. The newly inserted row ID is echoed. However, when i look in the DB, it only insert the latitude, longitude and timestamp. The pos_asset_id field in the newly inserted row is empty.
Could somebody point out where is the problem? There is no error message displayed. I've been trying to solve this for hours without avail.
Ps. This is my first time using PDO, so please bear with me. Thanks.
EDIT
Solved! I didn't notice that there's a FK relation between asset_positions.pos_asset_id and asset.asset_id. Once i remove this relationship constrains, the INSERT works properly now, the pos_asset_id value is inserted to the record.
Anyway, thanks all! :)
try running with error catching, it will give you better understanding of what is happening.
try {
$stmt->execute();
} catch (PDOException $p) {
echo $p->getMessage();
} catch (Exception $e) {
echo $e->getMessage();
}
But your common sense and user3540050 are right... it's a column related issue probably

Incorrect value written to mysql database

I am trying to write a value to a database. Everything seems to be fine except one value is mysteriously incorrect. I just can't figure this out.
Using codeigniter here is my controller:
$sample_id = $this->input->post('sample_id');
$culture_id = $this->input->post('culture_id');
$sample_name = $this->vial_model->get_name($culture_id);
$box_id = $this->input->post('boxid');
$db_data['boxid'] = $box_id;
$db_data['taskid'] = $sample_id;
$db_data['projectid'] = $culture_id;
$vial_id = $this->vial_model->create($db_data);
$vial_link = '<a href="'.base_url('freezer_vial/view/'.$culture_id.'/'.$sample_id.'/'.$vial_id).'" >'.$sample_name.'</a>';
Lets imagine that the value for $sample_id is 158 (or any number really) and that I can echo this value to the view to confirm.
The anchor link that is output to the view is as expected and contains 158. However, the value for $db_data['taskid'] is always 127 and is inserted into the database as this. I have no idea why. Everything else works fine.
Here is the model:
public function create($data)
{
$insert = $this->db->insert('vial', $data);
if($insert)
return $this->db->insert_id();
else
return false;
}
The column in your database table holding the sample id value might have been defined as a tinyint. See also https://stackoverflow.com/a/16684301/282073.
You might want to alter the column type to ensure no data is altered for each new insertion or update. Instances can be found in another answer https://stackoverflow.com/a/13772308/282073
Official documentation to alter tables can be read from
http://dev.mysql.com/doc/refman/5.1/en/alter-table.html

Get the last id before insert ZF2

I am trying to implement an action that can get me the last inserted id before I insert a record.
The action is basically supposed to get the last inserted id, then i add 1 to it then the value will be used in the current data been inserted.
This how far I have gotten and the error am getting
//the action to get the last inserted id
public function getLastID(){
$lastcourseid = $this->tableGateway->select(function (Select $select){
$select->columns(array('id'));
$select->order('id ASC')->limit(1);
});
var_dump($lastcourseid);
return $lastcourseid;
}
I call the function here before saving
if($id == 0){
$data['course_code'] = $this->getLastID();
$this->tableGateway->insert($data);
}else{
if($this->getAlbum($id)){
$this->tableGateway->update($data, array('id' => $id));
}else{
throw new \Exception("Form id does not exist");//an error is thrown in case the id is not found
}
}
This is the error am getting
Catchable fatal error: Object of class Zend\Db\ResultSet\ResultSet
could not be converted to string
I do not know where am going wrong.
Any help will be appreciated. Thanks
There is no such thing like "last id before insert".
And you don't need it.
First insert a record and then get your id. This is how it works.
You do not mention the underlying database (postgressql, mysql, etc.). I am more familiar with MySQL. The Perl and php AIP's have "last row id" functions. For example, php has "mysqli_insert_id()". This assumes that your rowID is an AUTO_INCREMENT column. Other DBs may have different requirements.

Categories