Hi,
I extented the Zend_Db_Table class with similar functionalities, the code below:
if $insert is null, enables auto detect of inserting.
I hope that is usefull.
Thanks.
class customTable extends Zend_Db_Table
{
public function saver($insert, array $cols, $ignoreUnknownCols = true, $unsetPrimaryKeysOnInsert = true)
{
if (is_null($insert)){
$primary = (array) $this->_primary;
$pkIdentity = $primary[(int)$this->_identity];
$insert = is_null($cols[$pkIdentity]) || !is_numeric($cols[$pkIdentity]);
}
if (!$insert){
$keys = array();
foreach ($this->_primary as $key)
$keys[] = $cols[$key];
$rowset = call_user_func_array(array($this, 'find'), $keys);
if ($rowset && $rowset->count())
$row = $rowset->current();
else {
require_once "Zend/Db/Table/Exception.php";
throw new Zend_Db_Table_Exception("Record not found");
}
} else {
$row = $this->createRow();
if ($unsetPrimaryKeysOnInsert)
foreach ($this->_primary as $key)
unset($cols[$key]);
}
if ($ignoreUnknownCols)
foreach ($cols as $col => $value)
if (!in_array($col, $this->_cols))
unset($cols[$col]);
$row->setFromArray($cols);
$row->save();
return $row;
}
}
I extented the Zend_Db_Table class with similar functionalities, the code below:
if $insert is null, enables auto detect of inserting.
I hope that is usefull.
Thanks.
class customTable extends Zend_Db_Table
{
public function saver($insert, array $cols, $ignoreUnknownCols = true, $unsetPrimaryKeysOnInsert = true)
{
if (is_null($insert)){
$primary = (array) $this->_primary;
$pkIdentity = $primary[(int)$this->_identity];
$insert = is_null($cols[$pkIdentity]) || !is_numeric($cols[$pkIdentity]);
}
if (!$insert){
$keys = array();
foreach ($this->_primary as $key)
$keys[] = $cols[$key];
$rowset = call_user_func_array(array($this, 'find'), $keys);
if ($rowset && $rowset->count())
$row = $rowset->current();
else {
require_once "Zend/Db/Table/Exception.php";
throw new Zend_Db_Table_Exception("Record not found");
}
} else {
$row = $this->createRow();
if ($unsetPrimaryKeysOnInsert)
foreach ($this->_primary as $key)
unset($cols[$key]);
}
if ($ignoreUnknownCols)
foreach ($cols as $col => $value)
if (!in_array($col, $this->_cols))
unset($cols[$col]);
$row->setFromArray($cols);
$row->save();
return $row;
}
}
----- Mensagem original ----
De: Bill Karwin <bill@karwin.com>
Para: fw-db@lists.zend.com
Enviadas: Segunda-feira, 4 de Agosto de 2008 17:30:05
Assunto: Re: [fw-db] Zend_Db_Table / Row / Rowset
Xavier Vidal Piera wrote:
>
> So, in your code you can do the following (this example could be more
> secure...)
>
Indeed yes. You should not assume that just because an id value was passed
as a param, that it corresponds to a primary key value that exists in the
database. The fetchRow() method could return null if no row is found.
Here's an alternative code fragment, that I think is more secure and simpler
too:
$table = new My_Table();
// Creating an empty Row object is inexpensive,
// so do this up front and it'll be the default.
$row = $table->createRow();
// Coerce the param into an integer for safety reasons.
// Default to zero, which isn't typically used in auto-generated primary
keys.
$id = (int) $this->_request->getParam('id', 0);
if ($id) {
// Use the find() method for primary key lookups,
// so you don't have to play with quoteInto().
$rowset = $table->find($id);
// If no row exists with that PK value, the rowset will be empty.
// If row exists, use it to replace the empty row you created earlier.
if (count($rowset)) {
$row = $rowset->current();
}
}
// Regardless of whether the row is empty or fetched from the DB,
// now you can set fields and save.
$row->bla = 'bla';
$row->save();
Regards,
Bill Karwin
--
View this message in context: http://www.nabble.com/Zend_Db_Table---Row---Rowset-tp18817752p18819145.html
Sent from the Zend DB mailing list archive at Nabble.com.
De: Bill Karwin <bill@karwin.com>
Para: fw-db@lists.zend.com
Enviadas: Segunda-feira, 4 de Agosto de 2008 17:30:05
Assunto: Re: [fw-db] Zend_Db_Table / Row / Rowset
Xavier Vidal Piera wrote:
>
> So, in your code you can do the following (this example could be more
> secure...)
>
Indeed yes. You should not assume that just because an id value was passed
as a param, that it corresponds to a primary key value that exists in the
database. The fetchRow() method could return null if no row is found.
Here's an alternative code fragment, that I think is more secure and simpler
too:
$table = new My_Table();
// Creating an empty Row object is inexpensive,
// so do this up front and it'll be the default.
$row = $table->createRow();
// Coerce the param into an integer for safety reasons.
// Default to zero, which isn't typically used in auto-generated primary
keys.
$id = (int) $this->_request->getParam('id', 0);
if ($id) {
// Use the find() method for primary key lookups,
// so you don't have to play with quoteInto().
$rowset = $table->find($id);
// If no row exists with that PK value, the rowset will be empty.
// If row exists, use it to replace the empty row you created earlier.
if (count($rowset)) {
$row = $rowset->current();
}
}
// Regardless of whether the row is empty or fetched from the DB,
// now you can set fields and save.
$row->bla = 'bla';
$row->save();
Regards,
Bill Karwin
--
View this message in context: http://www.nabble.com/Zend_Db_Table---Row---Rowset-tp18817752p18819145.html
Sent from the Zend DB mailing list archive at Nabble.com.
Novos endereços, o Yahoo! que você conhece. Crie um email novo com a sua cara @ymail.com ou @rocketmail.com.
没有评论:
发表评论