Overview

Packages

  • currencysymbol
  • MAbout
  • Mage
    • Admin
    • Adminhtml
    • AdminNotification
    • Api
    • Api2
    • Authorizenet
    • Backup
    • Bundle
    • Captcha
    • Catalog
    • CatalogIndex
    • CatalogInventory
    • CatalogRule
    • CatalogSearch
    • Centinel
    • Checkout
    • Cms
    • Compiler
    • Connect
    • Contacts
    • Core
    • Cron
    • CurrencySymbol
    • Customer
    • Dataflow
    • Directory
    • DirtectPost
    • Downloadable
    • Eav
    • GiftMessage
    • GoogleAnalytics
    • GoogleBase
    • GoogleCheckout
    • ImportExport
    • Index
    • Install
    • Log
    • Media
    • Newsletter
    • Oauth
    • Page
    • PageCache
    • Paygate
    • Payment
    • Paypal
    • PaypalUk
    • Persistent
    • Poll
    • ProductAlert
    • Rating
    • Reports
    • Review
    • Rss
    • Rule
    • Sales
    • SalesRule
    • Sedfriend
    • Sendfriend
    • Shipping
    • Sitemap
    • Tag
    • Tax
    • Usa
    • Weee
    • Widget
    • Wishlist
    • XmlConnect
  • None
  • Phoenix
    • Moneybookers
  • PHP
  • Zend
    • Date
    • Mime
    • XmlRpc

Classes

  • Mage
  • Mage_Adminhtml_Block_Html_Date
  • Mage_Adminhtml_Block_Html_Select
  • Mage_Api_Model_Wsdl_Config_Element
  • Mage_Captcha_Adminhtml_RefreshController
  • Mage_Captcha_RefreshController
  • Mage_Core_AjaxController
  • Mage_Core_Block_Abstract
  • Mage_Core_Block_Flush
  • Mage_Core_Block_Html_Calendar
  • Mage_Core_Block_Html_Date
  • Mage_Core_Block_Html_Link
  • Mage_Core_Block_Html_Select
  • Mage_Core_Block_Messages
  • Mage_Core_Block_Profiler
  • Mage_Core_Block_Store_Switcher
  • Mage_Core_Block_Template
  • Mage_Core_Block_Template_Facade
  • Mage_Core_Block_Template_Smarty
  • Mage_Core_Block_Template_Zend
  • Mage_Core_Block_Text
  • Mage_Core_Block_Text_List
  • Mage_Core_Block_Text_List_Item
  • Mage_Core_Block_Text_List_Link
  • Mage_Core_Block_Text_Tag
  • Mage_Core_Block_Text_Tag_Css
  • Mage_Core_Block_Text_Tag_Css_Admin
  • Mage_Core_Block_Text_Tag_Debug
  • Mage_Core_Block_Text_Tag_Js
  • Mage_Core_Block_Text_Tag_Meta
  • Mage_Core_Controller_Front_Action
  • Mage_Core_Controller_Front_Router
  • Mage_Core_Controller_Request_Http
  • Mage_Core_Controller_Response_Http
  • Mage_Core_Controller_Varien_Action
  • Mage_Core_Controller_Varien_Front
  • Mage_Core_Controller_Varien_Router_Abstract
  • Mage_Core_Controller_Varien_Router_Admin
  • Mage_Core_Controller_Varien_Router_Default
  • Mage_Core_Controller_Varien_Router_Standard
  • Mage_Core_Helper_Abstract
  • Mage_Core_Helper_Cookie
  • Mage_Core_Helper_Data
  • Mage_Core_Helper_File_Storage
  • Mage_Core_Helper_File_Storage_Database
  • Mage_Core_Helper_Hint
  • Mage_Core_Helper_Http
  • Mage_Core_Helper_Js
  • Mage_Core_Helper_String
  • Mage_Core_Helper_Translate
  • Mage_Core_Helper_Url
  • Mage_Core_Helper_Url_Rewrite
  • Mage_Core_IndexController
  • Mage_Core_Model_Abstract
  • Mage_Core_Model_App
  • Mage_Core_Model_App_Area
  • Mage_Core_Model_App_Emulation
  • Mage_Core_Model_Cache
  • Mage_Core_Model_Calculator
  • Mage_Core_Model_Config
  • Mage_Core_Model_Config_Base
  • Mage_Core_Model_Config_Data
  • Mage_Core_Model_Config_Element
  • Mage_Core_Model_Config_Options
  • Mage_Core_Model_Config_System
  • Mage_Core_Model_Cookie
  • Mage_Core_Model_Date
  • Mage_Core_Model_Design
  • Mage_Core_Model_Design_Package
  • Mage_Core_Model_Design_Source_Design
  • Mage_Core_Model_Email
  • Mage_Core_Model_Email_Info
  • Mage_Core_Model_Email_Template
  • Mage_Core_Model_Email_Template_Filter
  • Mage_Core_Model_Email_Template_Mailer
  • Mage_Core_Model_Email_Transport
  • Mage_Core_Model_Encryption
  • Mage_Core_Model_File_Storage
  • Mage_Core_Model_File_Storage_Abstract
  • Mage_Core_Model_File_Storage_Database
  • Mage_Core_Model_File_Storage_Database_Abstract
  • Mage_Core_Model_File_Storage_Directory_Database
  • Mage_Core_Model_File_Storage_File
  • Mage_Core_Model_File_Storage_Flag
  • Mage_Core_Model_File_Uploader
  • Mage_Core_Model_File_Validator_AvailablePath
  • Mage_Core_Model_File_Validator_NotProtectedExtension
  • Mage_Core_Model_Flag
  • Mage_Core_Model_Input_Filter
  • Mage_Core_Model_Input_Filter_MaliciousCode
  • Mage_Core_Model_Language
  • Mage_Core_Model_Layout
  • Mage_Core_Model_Layout_Data
  • Mage_Core_Model_Layout_Element
  • Mage_Core_Model_Layout_Update
  • Mage_Core_Model_Locale
  • Mage_Core_Model_Locale_Config
  • Mage_Core_Model_Log_Adapter
  • Mage_Core_Model_Magento_Api
  • Mage_Core_Model_Magento_Api_V2
  • Mage_Core_Model_Message
  • Mage_Core_Model_Message_Abstract
  • Mage_Core_Model_Message_Collection
  • Mage_Core_Model_Message_Error
  • Mage_Core_Model_Message_Notice
  • Mage_Core_Model_Message_Success
  • Mage_Core_Model_Message_Warning
  • Mage_Core_Model_Mysql4_Abstract
  • Mage_Core_Model_Mysql4_Cache
  • Mage_Core_Model_Mysql4_Collection_Abstract
  • Mage_Core_Model_Mysql4_Config
  • Mage_Core_Model_Mysql4_Config_Data
  • Mage_Core_Model_Mysql4_Config_Data_Collection
  • Mage_Core_Model_Mysql4_Design
  • Mage_Core_Model_Mysql4_Design_Collection
  • Mage_Core_Model_Mysql4_Design_Package_Collection
  • Mage_Core_Model_Mysql4_Design_Theme
  • Mage_Core_Model_Mysql4_Email_Template
  • Mage_Core_Model_Mysql4_Email_Template_Collection
  • Mage_Core_Model_Mysql4_File_Storage_Abstract
  • Mage_Core_Model_Mysql4_File_Storage_Database
  • Mage_Core_Model_Mysql4_File_Storage_Directory_Database
  • Mage_Core_Model_Mysql4_File_Storage_File
  • Mage_Core_Model_Mysql4_Flag
  • Mage_Core_Model_Mysql4_Language
  • Mage_Core_Model_Mysql4_Language_Collection
  • Mage_Core_Model_Mysql4_Layout
  • Mage_Core_Model_Mysql4_Resource
  • Mage_Core_Model_Mysql4_Session
  • Mage_Core_Model_Mysql4_Store
  • Mage_Core_Model_Mysql4_Store_Collection
  • Mage_Core_Model_Mysql4_Store_Group
  • Mage_Core_Model_Mysql4_Store_Group_Collection
  • Mage_Core_Model_Mysql4_Translate
  • Mage_Core_Model_Mysql4_Translate_String
  • Mage_Core_Model_Mysql4_Url_Rewrite
  • Mage_Core_Model_Mysql4_Url_Rewrite_Collection
  • Mage_Core_Model_Mysql4_Variable
  • Mage_Core_Model_Mysql4_Variable_Collection
  • Mage_Core_Model_Mysql4_Website
  • Mage_Core_Model_Mysql4_Website_Collection
  • Mage_Core_Model_Observer
  • Mage_Core_Model_Resource
  • Mage_Core_Model_Resource_Abstract
  • Mage_Core_Model_Resource_Cache
  • Mage_Core_Model_Resource_Config
  • Mage_Core_Model_Resource_Config_Data
  • Mage_Core_Model_Resource_Config_Data_Collection
  • Mage_Core_Model_Resource_Db_Abstract
  • Mage_Core_Model_Resource_Db_Collection_Abstract
  • Mage_Core_Model_Resource_Design
  • Mage_Core_Model_Resource_Design_Collection
  • Mage_Core_Model_Resource_Design_Package_Collection
  • Mage_Core_Model_Resource_Email_Template
  • Mage_Core_Model_Resource_Email_Template_Collection
  • Mage_Core_Model_Resource_Entity_Abstract
  • Mage_Core_Model_Resource_Entity_Table
  • Mage_Core_Model_Resource_File_Storage_Abstract
  • Mage_Core_Model_Resource_File_Storage_Database
  • Mage_Core_Model_Resource_File_Storage_Directory_Database
  • Mage_Core_Model_Resource_File_Storage_File
  • Mage_Core_Model_Resource_Flag
  • Mage_Core_Model_Resource_Helper_Abstract
  • Mage_Core_Model_Resource_Helper_Mysql4
  • Mage_Core_Model_Resource_Iterator
  • Mage_Core_Model_Resource_Language
  • Mage_Core_Model_Resource_Language_Collection
  • Mage_Core_Model_Resource_Layout
  • Mage_Core_Model_Resource_Resource
  • Mage_Core_Model_Resource_Session
  • Mage_Core_Model_Resource_Setup
  • Mage_Core_Model_Resource_Setup_Query_Modifier
  • Mage_Core_Model_Resource_Store
  • Mage_Core_Model_Resource_Store_Collection
  • Mage_Core_Model_Resource_Store_Group
  • Mage_Core_Model_Resource_Store_Group_Collection
  • Mage_Core_Model_Resource_Transaction
  • Mage_Core_Model_Resource_Translate
  • Mage_Core_Model_Resource_Translate_String
  • Mage_Core_Model_Resource_Type_Abstract
  • Mage_Core_Model_Resource_Type_Db
  • Mage_Core_Model_Resource_Type_Db_Mysqli
  • Mage_Core_Model_Resource_Type_Db_Mysqli_Setup
  • Mage_Core_Model_Resource_Type_Db_Pdo_Mysql
  • Mage_Core_Model_Resource_Url_Rewrite
  • Mage_Core_Model_Resource_Url_Rewrite_Collection
  • Mage_Core_Model_Resource_Variable
  • Mage_Core_Model_Resource_Variable_Collection
  • Mage_Core_Model_Resource_Website
  • Mage_Core_Model_Resource_Website_Collection
  • Mage_Core_Model_Session
  • Mage_Core_Model_Session_Abstract
  • Mage_Core_Model_Session_Abstract_Varien
  • Mage_Core_Model_Session_Abstract_Zend
  • Mage_Core_Model_Source_Email_Variables
  • Mage_Core_Model_Store
  • Mage_Core_Model_Store_Api
  • Mage_Core_Model_Store_Api_V2
  • Mage_Core_Model_Store_Group
  • Mage_Core_Model_Template
  • Mage_Core_Model_Translate
  • Mage_Core_Model_Translate_Expr
  • Mage_Core_Model_Translate_Inline
  • Mage_Core_Model_Translate_String
  • Mage_Core_Model_Url
  • Mage_Core_Model_Url_Rewrite
  • Mage_Core_Model_Url_Validator
  • Mage_Core_Model_Variable
  • Mage_Core_Model_Variable_Config
  • Mage_Core_Model_Variable_Observer
  • Mage_Core_Model_Website
  • Mage_Page_Block_Switch
  • Mage_Page_Block_Template_Container
  • Mage_Page_Block_Template_Links
  • Mage_Page_Block_Template_Links_Block
  • Mage_Sales_Model_Quote_Address_Total_Collector

Exceptions

  • Mage_Core_Controller_Varien_Exception
  • Mage_Core_Exception
  • Mage_Core_Model_Session_Exception
  • Mage_Core_Model_Store_Exception

Functions

  • destruct
  • is_dir_writeable
  • is_empty_date
  • mageCoreErrorHandler
  • mageDebugBacktrace
  • mageDelTree
  • mageFindClassFile
  • mageParseCsv
  • mageSendErrorFooter
  • mageSendErrorHeader
  • mageUndoMagicQuotes
  • now
  • sys_get_temp_dir
  • uc_words
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Magento
  4:  *
  5:  * NOTICE OF LICENSE
  6:  *
  7:  * This source file is subject to the Open Software License (OSL 3.0)
  8:  * that is bundled with this package in the file LICENSE.txt.
  9:  * It is also available through the world-wide-web at this URL:
 10:  * http://opensource.org/licenses/osl-3.0.php
 11:  * If you did not receive a copy of the license and are unable to
 12:  * obtain it through the world-wide-web, please send an email
 13:  * to license@magentocommerce.com so we can send you a copy immediately.
 14:  *
 15:  * DISCLAIMER
 16:  *
 17:  * Do not edit or add to this file if you wish to upgrade Magento to newer
 18:  * versions in the future. If you wish to customize Magento for your
 19:  * needs please refer to http://www.magentocommerce.com for more information.
 20:  *
 21:  * @category    Mage
 22:  * @package     Mage_Core
 23:  * @copyright   Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
 24:  * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 25:  */
 26: 
 27: 
 28: /**
 29:  * Abstract resource model class
 30:  *
 31:  * @category    Mage
 32:  * @package     Mage_Core
 33:  * @author      Magento Core Team <core@magentocommerce.com>
 34:  */
 35: abstract class Mage_Core_Model_Resource_Db_Abstract extends Mage_Core_Model_Resource_Abstract
 36: {
 37:     /**
 38:      * @deprecated since 1.5.0.0
 39:      */
 40:     const CHECKSUM_KEY_NAME= 'Checksum';
 41: 
 42:     /**
 43:      * Cached resources singleton
 44:      *
 45:      * @var Mage_Core_Model_Resource
 46:      */
 47:     protected $_resources;
 48: 
 49:     /**
 50:      * Prefix for resources that will be used in this resource model
 51:      *
 52:      * @var string
 53:      */
 54:     protected $_resourcePrefix;
 55: 
 56:     /**
 57:      * Connections cache for this resource model
 58:      *
 59:      * @var array
 60:      */
 61:     protected $_connections          = array();
 62: 
 63:     /**
 64:      * Resource model name that contains entities (names of tables)
 65:      *
 66:      * @var string
 67:      */
 68:     protected $_resourceModel;
 69: 
 70:     /**
 71:      * Tables used in this resource model
 72:      *
 73:      * @var array
 74:      */
 75:     protected $_tables               = array();
 76: 
 77:     /**
 78:      * Main table name
 79:      *
 80:      * @var string
 81:      */
 82:     protected $_mainTable;
 83: 
 84:     /**
 85:      * Main table primary key field name
 86:      *
 87:      * @var string
 88:      */
 89:     protected $_idFieldName;
 90: 
 91:     /**
 92:      * Primery key auto increment flag
 93:      *
 94:      * @var bool
 95:      */
 96:     protected $_isPkAutoIncrement    = true;
 97: 
 98:     /**
 99:      * Use is object new method for save of object
100:      *
101:      * @var boolean
102:      */
103:     protected $_useIsObjectNew       = false;
104: 
105:     /**
106:      * Fields List for update in forsedSave
107:      *
108:      * @var array
109:      */
110:     protected $_fieldsForUpdate      = array();
111: 
112:     /**
113:      * Fields of main table
114:      *
115:      * @var array
116:      */
117:     protected $_mainTableFields;
118: 
119:     /**
120:      * Main table unique keys field names
121:      * could array(
122:      *   array('field' => 'db_field_name1', 'title' => 'Field 1 should be unique')
123:      *   array('field' => 'db_field_name2', 'title' => 'Field 2 should be unique')
124:      *   array(
125:      *      'field' => array('db_field_name3', 'db_field_name3'),
126:      *      'title' => 'Field 3 and Field 4 combination should be unique'
127:      *   )
128:      * )
129:      * or string 'my_field_name' - will be autoconverted to
130:      *      array( array( 'field' => 'my_field_name', 'title' => 'my_field_name' ) )
131:      *
132:      * @var array
133:      */
134:     protected $_uniqueFields         = null;
135: 
136:     /**
137:      * Serializable fields declaration
138:      * Structure: array(
139:      *     <field_name> => array(
140:      *         <default_value_for_serialization>,
141:      *         <default_for_unserialization>,
142:      *         <whether_to_unset_empty_when serializing> // optional parameter
143:      *     ),
144:      * )
145:      *
146:      * @var array
147:      */
148:     protected $_serializableFields   = array();
149: 
150:     /**
151:      * Standard resource model initialization
152:      *
153:      * @param string $mainTable
154:      * @param string $idFieldName
155:      * @return Mage_Core_Model_Resource_Abstract
156:      */
157:     protected function _init($mainTable, $idFieldName)
158:     {
159:         $this->_setMainTable($mainTable, $idFieldName);
160:     }
161: 
162:     /**
163:      * Initialize connections and tables for this resource model
164:      * If one or both arguments are string, will be used as prefix
165:      * If $tables is null and $connections is string, $tables will be the same
166:      *
167:      * @param string|array $connections
168:      * @param string|array|null $tables
169:      * @return Mage_Core_Model_Resource_Abstract
170:      */
171:     protected function _setResource($connections, $tables = null)
172:     {
173:         $this->_resources = Mage::getSingleton('core/resource');
174: 
175:         if (is_array($connections)) {
176:             foreach ($connections as $k=>$v) {
177:                 $this->_connections[$k] = $this->_resources->getConnection($v);
178:             }
179:         } else if (is_string($connections)) {
180:             $this->_resourcePrefix = $connections;
181:         }
182: 
183:         if (is_null($tables) && is_string($connections)) {
184:             $this->_resourceModel = $this->_resourcePrefix;
185:         } else if (is_array($tables)) {
186:             foreach ($tables as $k => $v) {
187:                 $this->_tables[$k] = $this->_resources->getTableName($v);
188:             }
189:         } else if (is_string($tables)) {
190:             $this->_resourceModel = $tables;
191:         }
192:         return $this;
193:     }
194: 
195:     /**
196:      * Set main entity table name and primary key field name
197:      * If field name is ommited {table_name}_id will be used
198:      *
199:      * @param string $mainTable
200:      * @param string|null $idFieldName
201:      * @return Mage_Core_Model_Resource_Db_Abstract
202:      */
203:     protected function _setMainTable($mainTable, $idFieldName = null)
204:     {
205:         $mainTableArr = explode('/', $mainTable);
206: 
207:         if (!empty($mainTableArr[1])) {
208:             if (empty($this->_resourceModel)) {
209:                 $this->_setResource($mainTableArr[0]);
210:             }
211:             $this->_setMainTable($mainTableArr[1], $idFieldName);
212:         } else {
213:             $this->_mainTable = $mainTable;
214:             if (is_null($idFieldName)) {
215:                 $idFieldName = $mainTable . '_id';
216:             }
217:             $this->_idFieldName = $idFieldName;
218:         }
219: 
220:         return $this;
221:     }
222: 
223:     /**
224:      * Get primary key field name
225:      *
226:      * @return string
227:      */
228:     public function getIdFieldName()
229:     {
230:         if (empty($this->_idFieldName)) {
231:             Mage::throwException(Mage::helper('core')->__('Empty identifier field name'));
232:         }
233:         return $this->_idFieldName;
234:     }
235: 
236:     /**
237:      * Returns main table name - extracted from "module/table" style and
238:      * validated by db adapter
239:      *
240:      * @return string
241:      */
242:     public function getMainTable()
243:     {
244:         if (empty($this->_mainTable)) {
245:             Mage::throwException(Mage::helper('core')->__('Empty main table name'));
246:         }
247:         return $this->getTable($this->_mainTable);
248:     }
249: 
250:     /**
251:      * Get table name for the entity, validated by db adapter
252:      *
253:      * @param string $entityName
254:      * @return string
255:      */
256:     public function getTable($entityName)
257:     {
258:         if (is_array($entityName)) {
259:             $cacheName    = join('@', $entityName);
260:             list($entityName, $entitySuffix) = $entityName;
261:         } else {
262:             $cacheName    = $entityName;
263:             $entitySuffix = null;
264:         }
265: 
266:         if (isset($this->_tables[$cacheName])) {
267:             return $this->_tables[$cacheName];
268:         }
269: 
270:         if (strpos($entityName, '/')) {
271:             if (!is_null($entitySuffix)) {
272:                 $modelEntity = array($entityName, $entitySuffix);
273:             } else {
274:                 $modelEntity = $entityName;
275:             }
276:             $this->_tables[$cacheName] = $this->_resources->getTableName($modelEntity);
277:         } else if (!empty($this->_resourceModel)) {
278:             $entityName = sprintf('%s/%s', $this->_resourceModel, $entityName);
279:             if (!is_null($entitySuffix)) {
280:                 $modelEntity = array($entityName, $entitySuffix);
281:             } else {
282:                 $modelEntity = $entityName;
283:             }
284:             $this->_tables[$cacheName] = $this->_resources->getTableName($modelEntity);
285:         } else {
286:             if (!is_null($entitySuffix)) {
287:                 $entityName .= '_' . $entitySuffix;
288:             }
289:             $this->_tables[$cacheName] = $entityName;
290:         }
291:         return $this->_tables[$cacheName];
292:     }
293: 
294: 
295:     /**
296:      * Retrieve table name for the entity separated value
297:      *
298:      * @param string $entityName
299:      * @param string $valueType
300:      * @return string
301:      */
302:     public function getValueTable($entityName, $valueType)
303:     {
304:         return $this->getTable(array($entityName, $valueType));
305:     }
306: 
307:     /**
308:      * Get connection by name or type
309:      *
310:      * @param string $connectionName
311:      * @return Zend_Db_Adapter_Abstract
312:      */
313:     protected function _getConnection($connectionName)
314:     {
315:         if (isset($this->_connections[$connectionName])) {
316:             return $this->_connections[$connectionName];
317:         }
318:         if (!empty($this->_resourcePrefix)) {
319:             $this->_connections[$connectionName] = $this->_resources->getConnection(
320:                 $this->_resourcePrefix . '_' . $connectionName);
321:         } else {
322:             $this->_connections[$connectionName] = $this->_resources->getConnection($connectionName);
323:         }
324: 
325:         return $this->_connections[$connectionName];
326:     }
327: 
328:     /**
329:      * Retrieve connection for read data
330:      *
331:      * @return Varien_Db_Adapter_Interface
332:      */
333:     protected function _getReadAdapter()
334:     {
335:         $writeAdapter = $this->_getWriteAdapter();
336:         if ($writeAdapter && $writeAdapter->getTransactionLevel() > 0) {
337:             // if transaction is started we should use write connection for reading
338:             return $writeAdapter;
339:         }
340:         return $this->_getConnection('read');
341:     }
342: 
343:     /**
344:      * Retrieve connection for write data
345:      *
346:      * @return Varien_Db_Adapter_Interface
347:      */
348:     protected function _getWriteAdapter()
349:     {
350:         return $this->_getConnection('write');
351:     }
352: 
353:     /**
354:      * Temporary resolving collection compatibility
355:      *
356:      * @return Varien_Db_Adapter_Interface
357:      */
358:     public function getReadConnection()
359:     {
360:         return $this->_getReadAdapter();
361:     }
362: 
363:     /**
364:      * Load an object
365:      *
366:      * @param Mage_Core_Model_Abstract $object
367:      * @param mixed $value
368:      * @param string $field field to load by (defaults to model id)
369:      * @return Mage_Core_Model_Resource_Db_Abstract
370:      */
371:     public function load(Mage_Core_Model_Abstract $object, $value, $field = null)
372:     {
373:         if (is_null($field)) {
374:             $field = $this->getIdFieldName();
375:         }
376: 
377:         $read = $this->_getReadAdapter();
378:         if ($read && !is_null($value)) {
379:             $select = $this->_getLoadSelect($field, $value, $object);
380:             $data = $read->fetchRow($select);
381: 
382:             if ($data) {
383:                 $object->setData($data);
384:             }
385:         }
386: 
387:         $this->unserializeFields($object);
388:         $this->_afterLoad($object);
389: 
390:         return $this;
391:     }
392: 
393:     /**
394:      * Retrieve select object for load object data
395:      *
396:      * @param string $field
397:      * @param mixed $value
398:      * @param Mage_Core_Model_Abstract $object
399:      * @return Zend_Db_Select
400:      */
401:     protected function _getLoadSelect($field, $value, $object)
402:     {
403:         $field  = $this->_getReadAdapter()->quoteIdentifier(sprintf('%s.%s', $this->getMainTable(), $field));
404:         $select = $this->_getReadAdapter()->select()
405:             ->from($this->getMainTable())
406:             ->where($field . '=?', $value);
407:         return $select;
408:     }
409: 
410:     /**
411:      * Save object object data
412:      *
413:      * @param Mage_Core_Model_Abstract $object
414:      * @return Mage_Core_Model_Resource_Db_Abstract
415:      */
416:     public function save(Mage_Core_Model_Abstract $object)
417:     {
418:         if ($object->isDeleted()) {
419:             return $this->delete($object);
420:         }
421: 
422:         $this->_serializeFields($object);
423:         $this->_beforeSave($object);
424:         $this->_checkUnique($object);
425:         if (!is_null($object->getId()) && (!$this->_useIsObjectNew || !$object->isObjectNew())) {
426:             $condition = $this->_getWriteAdapter()->quoteInto($this->getIdFieldName().'=?', $object->getId());
427:             /**
428:              * Not auto increment primary key support
429:              */
430:             if ($this->_isPkAutoIncrement) {
431:                 $data = $this->_prepareDataForSave($object);
432:                 unset($data[$this->getIdFieldName()]);
433:                 $this->_getWriteAdapter()->update($this->getMainTable(), $data, $condition);
434:             } else {
435:                 $select = $this->_getWriteAdapter()->select()
436:                     ->from($this->getMainTable(), array($this->getIdFieldName()))
437:                     ->where($condition);
438:                 if ($this->_getWriteAdapter()->fetchOne($select) !== false) {
439:                     $data = $this->_prepareDataForSave($object);
440:                     unset($data[$this->getIdFieldName()]);
441:                     if (!empty($data)) {
442:                         $this->_getWriteAdapter()->update($this->getMainTable(), $data, $condition);
443:                     }
444:                 } else {
445:                     $this->_getWriteAdapter()->insert($this->getMainTable(), $this->_prepareDataForSave($object));
446:                 }
447:             }
448:         } else {
449:             $bind = $this->_prepareDataForSave($object);
450:             if ($this->_isPkAutoIncrement) {
451:                 unset($bind[$this->getIdFieldName()]);
452:             }
453:             $this->_getWriteAdapter()->insert($this->getMainTable(), $bind);
454: 
455:             $object->setId($this->_getWriteAdapter()->lastInsertId($this->getMainTable()));
456: 
457:             if ($this->_useIsObjectNew) {
458:                 $object->isObjectNew(false);
459:             }
460:         }
461: 
462:         $this->unserializeFields($object);
463:         $this->_afterSave($object);
464: 
465:         return $this;
466:     }
467: 
468:     /**
469:      * Forsed save object data
470:      * forsed update If duplicate unique key data
471:      *
472:      * @deprecated
473:      * @param Mage_Core_Model_Abstract $object
474:      * @return Mage_Core_Model_Resource_Db_Abstract
475:      */
476:     public function forsedSave(Mage_Core_Model_Abstract $object)
477:     {
478:         $this->_beforeSave($object);
479:         $bind = $this->_prepareDataForSave($object);
480:         $adapter = $this->_getWriteAdapter();
481:         // update
482:         if (!is_null($object->getId()) && $this->_isPkAutoIncrement) {
483:             unset($bind[$this->getIdFieldName()]);
484:             $condition = $adapter->quoteInto($this->getIdFieldName().'=?', $object->getId());
485:             $adapter->update($this->getMainTable(), $bind, $condition);
486:         } else {
487:             $adapter->insertOnDuplicate($this->getMainTable(), $bind, $this->_fieldsForUpdate);
488:             $object->setId($adapter->lastInsertId($this->getMainTable()));
489:         }
490: 
491:         $this->_afterSave($object);
492: 
493:         return $this;
494:     }
495: 
496:     /**
497:      * Delete the object
498:      *
499:      * @param Varien_Object $object
500:      * @return Mage_Core_Model_Resource_Db_Abstract
501:      */
502:     public function delete(Mage_Core_Model_Abstract $object)
503:     {
504:         $this->_beforeDelete($object);
505:         $this->_getWriteAdapter()->delete(
506:             $this->getMainTable(),
507:             $this->_getWriteAdapter()->quoteInto($this->getIdFieldName() . '=?', $object->getId())
508:         );
509:         $this->_afterDelete($object);
510:         return $this;
511:     }
512: 
513:     /**
514:      * Add unique field restriction
515:      *
516:      * @param array|string $field
517:      * @return Mage_Core_Model_Resource_Db_Abstract
518:      */
519:     public function addUniqueField($field)
520:     {
521:         if (is_null($this->_uniqueFields)) {
522:             $this->_initUniqueFields();
523:         }
524:         if (is_array($this->_uniqueFields) ) {
525:             $this->_uniqueFields[] = $field;
526:         }
527:         return $this;
528:     }
529: 
530:     /**
531:      * Reset unique fields restrictions
532:      *
533:      * @return Mage_Core_Model_Resource_Db_Abstract
534:      */
535:     public function resetUniqueField()
536:     {
537:         $this->_uniqueFields = array();
538:          return $this;
539:     }
540: 
541:     /**
542:      * Unserialize serializeable object fields
543:      *
544:      * @param Mage_Core_Model_Abstract $object
545:      */
546:     public function unserializeFields(Mage_Core_Model_Abstract $object)
547:     {
548:         foreach ($this->_serializableFields as $field => $parameters) {
549:             list($serializeDefault, $unserializeDefault) = $parameters;
550:             $this->_unserializeField($object, $field, $unserializeDefault);
551:         }
552:     }
553: 
554:     /**
555:      * Initialize unique fields
556:      *
557:      * @return Mage_Core_Model_Resource_Db_Abstract
558:      */
559:     protected function _initUniqueFields()
560:     {
561:         $this->_uniqueFields = array();
562:         return $this;
563:     }
564: 
565:     /**
566:      * Get configuration of all unique fields
567:      *
568:      * @return array
569:      */
570:     public function getUniqueFields()
571:     {
572:         if (is_null($this->_uniqueFields)) {
573:             $this->_initUniqueFields();
574:         }
575:         return $this->_uniqueFields;
576:     }
577: 
578:     /**
579:      * Prepare data for save
580:      *
581:      * @param Mage_Core_Model_Abstract $object
582:      * @return array
583:      */
584:     protected function _prepareDataForSave(Mage_Core_Model_Abstract $object)
585:     {
586:         return $this->_prepareDataForTable($object, $this->getMainTable());
587:     }
588: 
589:     /**
590:      * Check that model data fields that can be saved
591:      * has really changed comparing with origData
592:      *
593:      * @param Mage_Core_Model_Abstract $object
594:      * @return boolean
595:      */
596:     public function hasDataChanged($object)
597:     {
598:         if (!$object->getOrigData()) {
599:             return true;
600:         }
601: 
602:         $fields = $this->_getWriteAdapter()->describeTable($this->getMainTable());
603:         foreach (array_keys($fields) as $field) {
604:             if ($object->getOrigData($field) != $object->getData($field)) {
605:                 return true;
606:             }
607:         }
608: 
609:         return false;
610:     }
611: 
612:     /**
613:      * Prepare value for save
614:      *
615:      * @param mixed $value
616:      * @param string $type
617:      * @return mixed
618:      */
619:     protected function _prepareValueForSave($value, $type)
620:     {
621:         return $this->_prepareTableValueForSave($value, $type);
622:     }
623: 
624:     /**
625:      * Check for unique values existence
626:      *
627:      * @param Mage_Core_Model_Abstract $object
628:      * @return Mage_Core_Model_Resource_Db_Abstract
629:      * @throws Mage_Core_Exception
630:      */
631:     protected function _checkUnique(Mage_Core_Model_Abstract $object)
632:     {
633:         $existent = array();
634:         $fields = $this->getUniqueFields();
635:         if (!empty($fields)) {
636:             if (!is_array($fields)) {
637:                 $this->_uniqueFields = array(
638:                     array(
639:                         'field' => $fields,
640:                         'title' => $fields
641:                 ));
642:             }
643: 
644:             $data = new Varien_Object($this->_prepareDataForSave($object));
645:             $select = $this->_getWriteAdapter()->select()
646:                 ->from($this->getMainTable());
647: 
648:             foreach ($fields as $unique) {
649:                 $select->reset(Zend_Db_Select::WHERE);
650: 
651:                 if (is_array($unique['field'])) {
652:                     foreach ($unique['field'] as $field) {
653:                         $select->where($field . '=?', trim($data->getData($field)));
654:                     }
655:                 } else {
656:                     $select->where($unique['field'] . '=?', trim($data->getData($unique['field'])));
657:                 }
658: 
659:                 if ($object->getId() || $object->getId() === '0') {
660:                     $select->where($this->getIdFieldName() . '!=?', $object->getId());
661:                 }
662: 
663:                 $test = $this->_getWriteAdapter()->fetchRow($select);
664:                 if ($test) {
665:                     $existent[] = $unique['title'];
666:                 }
667:             }
668:         }
669: 
670:         if (!empty($existent)) {
671:             if (count($existent) == 1 ) {
672:                 $error = Mage::helper('core')->__('%s already exists.', $existent[0]);
673:             } else {
674:                 $error = Mage::helper('core')->__('%s already exist.', implode(', ', $existent));
675:             }
676:             Mage::throwException($error);
677:         }
678:         return $this;
679:     }
680: 
681:     /**
682:      * After load
683:      *
684:      * @param Mage_Core_Model_Abstract $object
685:      */
686:     public function afterLoad(Mage_Core_Model_Abstract $object)
687:     {
688:         $this->_afterLoad($object);
689:     }
690: 
691:     /**
692:      * Perform actions after object load
693:      *
694:      * @param Varien_Object $object
695:      * @return Mage_Core_Model_Resource_Db_Abstract
696:      */
697:     protected function _afterLoad(Mage_Core_Model_Abstract $object)
698:     {
699:         return $this;
700:     }
701: 
702:     /**
703:      * Perform actions before object save
704:      *
705:      * @param Varien_Object $object
706:      * @return Mage_Core_Model_Resource_Db_Abstract
707:      */
708:     protected function _beforeSave(Mage_Core_Model_Abstract $object)
709:     {
710:         return $this;
711:     }
712: 
713:     /**
714:      * Perform actions after object save
715:      *
716:      * @param Varien_Object $object
717:      * @return Mage_Core_Model_Resource_Db_Abstract
718:      */
719:     protected function _afterSave(Mage_Core_Model_Abstract $object)
720:     {
721:         return $this;
722:     }
723: 
724:     /**
725:      * Perform actions before object delete
726:      *
727:      * @param Varien_Object $object
728:      * @return Mage_Core_Model_Resource_Db_Abstract
729:      */
730:     protected function _beforeDelete(Mage_Core_Model_Abstract $object)
731:     {
732:         return $this;
733:     }
734: 
735:     /**
736:      * Perform actions after object delete
737:      *
738:      * @param Varien_Object $object
739:      * @return Mage_Core_Model_Resource_Db_Abstract
740:      */
741:     protected function _afterDelete(Mage_Core_Model_Abstract $object)
742:     {
743:         return $this;
744:     }
745: 
746:     /**
747:      * Serialize serializeable fields of the object
748:      *
749:      * @param Mage_Core_Model_Abstract $object
750:      */
751:     protected function _serializeFields(Mage_Core_Model_Abstract $object)
752:     {
753:         foreach ($this->_serializableFields as $field => $parameters) {
754:             list($serializeDefault, $unserializeDefault) = $parameters;
755:             $this->_serializeField($object, $field, $serializeDefault, isset($parameters[2]));
756:         }
757:     }
758: 
759:     /**
760:      * Retrieve table checksum
761:      *
762:      * @param string|array $table
763:      * @return int|array
764:      */
765:     public function getChecksum($table)
766:     {
767:         if (!$this->_getReadAdapter()) {
768:             return false;
769:         }
770:         $checksum = $this->_getReadAdapter()->getTablesChecksum($table);
771:         if (count($checksum) == 1) {
772:             return $checksum[$table];
773:         }
774:         return $checksum;
775:     }
776: }
777: 
Magento 1.7.0.2 API documentation generated by ApiGen 2.8.0