【php开源代码栏目提醒】:网学会员php开源代码为您提供Row.php参考,解决您在Row.php学习中工作中的难题,参考学习。
<?php
/**
* +----------------------------------------------------------------------+
* | Zend Framework |
* +----------------------------------------------------------------------+
* | Copyright (c) 2005-2006 Zend Technologies Inc. (http://www.zend.com) |
* +----------------------------------------------------------------------+
* | This source file is subject to version 1.0 of the Zend Framework |
* | license, that is bundled with this package in the file LICENSE, and |
* | is available through the world-wide-web at the following url: |
* | http://www.zend.com/license/framework/1_0.txt. |
* | If you did not receive a copy of the Zend license and are unable to |
* | obtain it through the world-wide-
web, please send a note to |
* | license@zend.com so we can mail you a copy immediately. |
* +----------------------------------------------------------------------+
*
* @package Zend_Db
* @subpackage Table
* @copyright Copyright (c) 2005-2006 Zend Technologies Inc. (http://www.zend.com)
* @license Zend Framework License version 1.0
*/
/**
* Zend_Db_Table_Row_Exception
*/
require_once 'Zend/Db/Table/Row/Exception.php';
/**
* @package Zend_Db
* @subpackage Table
* @copyright Copyright (c) 2005-2006 Zend Technologies Inc. (http://www.zend.com)
* @license Zend Framework License version 1.0
*/
class Zend_Db_Table_Row
{
/**
* The data for each column in the row (underscore_words => value).
*
* @var array
*/
protected $_data = array();
/**
* Zend_Db_Adapter object from the table interface.
*
* @var Zend_Db_Adapter
*/
protected $_db;
/**
* Zend_Db_Table interface (the row "parent").
*
* @var Zend_Db_Table
*/
protected $_table;
/**
* Zend_Db_Table info (name, cols, primary, etc).
*
* @var array
*/
protected $_info = array();
/**
* Constructor.
*/
public function __construct($config = array())
{
$this->_db = $config['db'];
$this->_table = $config['table'];
$this->_info = $this->_table->info();
if ($config['data'] === false) {
// empty row, use blanks
$cols = array_keys($this->_info['cols']);
$data = array_fill(0, count($cols), null);
$this->_data = array_combine ($cols, $data);
} else {
$this->_data = (array) $config['data'];
}
}
/**
* Getter for camelCaps properties mapped to underscore_
word columns.
*
* @param string $camel The camelCaps property name; e.g., 'columnName'
* maps to 'column_name'.
* @return string The mapped column value.
*/
public function __get($camel)
{
$under = array_search($camel, $this->_info['cols']);
if ($under) {
return $this->_data[$under];
} else {
throw new Zend_Db_Table_Row_Exception("column '$camel' not in row");
}
}
/**
* Setter for camelCaps properties mapped to underscore_word columns.
*
* @param string $camel The camelCaps property name; e.g., 'columnName'
* maps to 'column_name'.
* @param mixed $value The value for the property.
* @return void
*/
public function __set($camel, $value)
{
$under = array_search($camel, $this->_info['cols']);
if ($under == $this->_info['primary']) {
throw new Zend_Db_Table_Row_Exception("not allowed to change primary key value");
} elseif ($under === false) {
throw new Zend_Db_Table_Row_Exception("column '$camel' not in row");
} else {
$this->_data[$under] = $value;
}
}
/**
* Saves the properties to the database.
*
* This performs an intelligent insert/update, and reloads the
* properties with fresh data from the table on success.
*
* @return int 0 on failure, 1 on success.
*/
public function save()
{
// convenience var for the primary key name
$primary = $this->_info['primary'];
// check the primary key value for insert/update
if (empty($this->_data[$primary])) {
// no primary key value, must be an insert.
// make sure it's null.
$this->_data[$primary] = null;
// attempt the insert.
$result = $this->_table->insert($this->_data);
if (is_numeric($result)) {
// insert worked, refresh with data from the table
$this->_data[$primary] = $result;
$this->_refresh();
}
// regardless of success return the result
return $result;
} else {
// has a primary key value, update only that key.
$where = $this->_db->quoteInto(
"$primary = ?",
$this->_data[$primary]
);
// return the result of the update attempt,
// no need to update the row object.
$result = $this->_table->update($this->_data, $where);
if (is_int($result)) {
// update worked, refresh with data from the table
$this->_refresh();
}
}
}
/**
* Returns the column/value data as an array.
*
* @return array
*/
public function toArray()
{
return $this->_data;
}
/**