ZF last inserted id - zend-framework

does this code gives me the last inserted id of the record, even on a heavy load page?
db = Zend_Db_Table::getDefaultAdapter();
$db->insert($this->_name, $fields_values);
$idAddress = $db->lastInsertId($this->_name);
Regards
Andrea

i am using this...
db = Zend_Db_Table::getDefaultAdapter();
$lastInsertId = $db->insert($this->_name, $fields_values);

The Zend_Db_Adapter_Abstract::lastInsertId() method is a proxy to PDO::lastInsertId(). According to the documentation:
PDO::lastInsertId — Returns the ID of the last inserted row or sequence value
Note:
This method may not return a meaningful or consistent result across different PDO drivers, because the underlying database may not even support the notion of auto-increment fields or sequences.
Now you know. Use it as your own risk!

public function saveData(array $data) {
$this->_setName('user');
// this will return all field name for user table in $field variable
$fields = $this->info($this->getConstCol());
// By this foreach loop we will set data in $data array
foreach ($data as $field => $value)
{
if (!in_array($field, $fields))
{
unset($data[$field]);
}
}
// It will call insert function of (Zend_Db_Table_Abstract Parent of Zend_Db_Table)
// It will return $pkData which is primary key for user table
return $this->insert($data); // It will return last insert ID
}

Related

Is there a way to use create() method on laravel eloquent model where data will call there corresponding set{field}Attribute method?

I'm trying to use create() method to create an object say 'user' from laravel-excel Import class say 'UserImport'. In the collection method, I grabbed the first row as the properties of the user and every cell in the subsequent row bears the data for the user on each row.
Using the create method will ensure that field that is not in my fillable will not be inserted as fields are dynamically gotten. I need the create method to use setGenderAttribute defined on the User model so as to transform 'male', 'm' in the excel gender column to constant User::GENDER_MALE and 'female', 'f' to constant User::GENDER_FEMALE.
public function collection(Collection $rows)
{
$keys = [];
// Extract spreadsheet head
foreach ($rows[0] as $key) {
$keys[] = Str::snake($key);
}
$j = 0;
foreach ($rows as $row) {
// Offset the head from the data set
if ($j == 0) {
$j++;
continue;
}
// get data from each row
$data = [];
$i = 0;
foreach ($keys as $key) {
$data[$key] = $row[$i];
$i++;
}
// Create user from each row
$user = User::create($data);
event(new MemberAdded($user));
}
}
It throws the following error integer value: 'male' for column users.gender
I've gotten to the answer. Laravel create method on eloquent actually call all the set{field}Attribute before actually doing database insertion. The problem was in one of the 'set' method. There was a bug in it and laravel fall back to the original field which was a string whose column was defined as integer in the database table schema.

Get last insert id in store function

How to get the last insert id in backpack for laravel 4 in the store function?
public function store()
{
$response = $this->traitStore();
//need the last id for operations
return $response;
}
This code doesn't work for me:
$id = $this->crud->entry->id; // <-- SHOULD WORK
You should have the last inserted item available as $this->data['entry'], so you should be able to get the ID using $this->data['entry']->id

TYPO3 hook for modifing TCE data from database

I'm looking for a hook that let's me modify the database output, when editing my extension in the backend, before it's printed to the TCE fields.
I tried getSingleField_preProcess in class.t3lib_tceforms.php but that did not contain any relevant data to my extension.
getSingleField_preProcess should be the correct hook for what you want. I think the problem is that your function is called for records of every table, not just yours. You have to differentiate when to do any processing based on the table that is being rendered. The name of the table is passed to your getSingleField_preProcess() method.
Your ext_localconf.php should register your hook:
$GLOBALS ['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tceforms.php']['getSingleFieldClass'][] = 'tx_yourextension_be';
...where tx_yourextension_be is the name of your class designated for the backend processing. This class then must contain getSingleField_preProcess() method:
public function getSingleField_preProcess($table, $field, &$row, $altName, $palette, $extra, $pal, &$pObj) {
// ...processing...
}
As you see, several variables are passed to your method. $table contains the name of the table to which the processed record belongs to. $field is the name of the field that is being rendered. $row contains the whole record that you can manipulate.
Probably you want to use TCEmain hook
function processDatamap_preProcessFieldArray(array &$incomingFieldArray, $table, $id, t3lib_TCEmain &$reference) {
if ($table == 'tx_yourext_table') {
$a = $incomingFieldArray['field_a'];
$b = $incomingFieldArray['field_b'];
$incomingFieldArray['field_c'] = $a . ' ' . $b;
}
}
OR/AND
function processDatamap_afterDatabaseOperations($status, $table, $id, $fieldArray, &$reference) {
if ($table == 'tx_yourext_table') {
if ($status == 'update') {
$this->doSomethingWithRecordAfterUpdate($id);
}
}
}
Of course you need to register the hook in ext_localconf.php of your extension, for an example:
$GLOBALS ['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass']['yourext']
= 'EXT:yourext/class.tx_yourext_tcemain.php:tx_yourext_tcemain';

Zend Paginate - find a specific record within the result

I appreciate that this may not be possible, but is there a way to make Zend Paginate go to a specific item (record)?
The result I would like would allow me to seek a specific record in a tabled list of results, and display the appropriate page (within all available pages) combined with a name anchor tag to display the specific record.
To clarify: If I had the results as a Zend_Db_Table_Rowset_Abstract I would use the seek() method in a similar fashion to $rowset->seek(8); Although I don't believe the result returned by the DbSelect adapter is a SeekableIterator?
The code within my Mapper (using the Table Data Gateway pattern):
public function paginate($where = array(), $order = null)
{
$select = $this->getDbTable()->select()->from($this->getTableName(), $this->getTableFields());
foreach ($where as $key => $value) {
$select->where($key, $value);
}
$select->order($order);
$adapter = new Zend_Paginator_Adapter_DbSelect($select);
$paginator = new Zend_Paginator($adapter);
return $paginator;
}
Within my controller:
$cache_id = sha1('list');
$mapper = new Application_Model_Galleries_Mapper();
if(!($data = Zend_Registry::get('cache')->load($cache_id))) {
$data = $mapper->paginate(array(), $sort);
Zend_Registry::get('cache')->save($data, $cache_id, array('list'), 7200);
}
$data->setCurrentPageNumber($this->_getParam('page'));
$data->setItemCountPerPage(30);
$this->view->paginator = $data;
To return a Zend_Paginator with a seekable iterator (Zend_Db_Table_Rowset) use the Zend_Paginator_Adapter_DbTableSelect() as it returns a rowset object, as opposed to Zend_Paginator_Adaoter_DbSelect() which returns an array().
Zend_Paginator

Zend Db query to select all IDs

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'); )