web/lib/Zend/Db/Table/Abstract.php
changeset 1230 68c69c656a2c
parent 807 877f952ae2bd
equal deleted inserted replaced
1229:5a6b6e770365 1230:68c69c656a2c
    13  * to license@zend.com so we can send you a copy immediately.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category   Zend
    15  * @category   Zend
    16  * @package    Zend_Db
    16  * @package    Zend_Db
    17  * @subpackage Table
    17  * @subpackage Table
    18  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    18  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    20  * @version    $Id: Abstract.php 24958 2012-06-15 13:44:04Z adamlundrigan $
    20  * @version    $Id$
    21  */
    21  */
    22 
    22 
    23 /**
    23 /**
    24  * @see Zend_Db_Adapter_Abstract
    24  * @see Zend_Db_Adapter_Abstract
    25  */
    25  */
    39  * Class for SQL table interface.
    39  * Class for SQL table interface.
    40  *
    40  *
    41  * @category   Zend
    41  * @category   Zend
    42  * @package    Zend_Db
    42  * @package    Zend_Db
    43  * @subpackage Table
    43  * @subpackage Table
    44  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    44  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
    45  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    45  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    46  */
    46  */
    47 abstract class Zend_Db_Table_Abstract
    47 abstract class Zend_Db_Table_Abstract
    48 {
    48 {
    49 
    49 
  1039          * it's the _first_ column in the compound key.
  1039          * it's the _first_ column in the compound key.
  1040          */
  1040          */
  1041         $primary = (array) $this->_primary;
  1041         $primary = (array) $this->_primary;
  1042         $pkIdentity = $primary[(int)$this->_identity];
  1042         $pkIdentity = $primary[(int)$this->_identity];
  1043 
  1043 
       
  1044 
       
  1045         /**
       
  1046          * If the primary key can be generated automatically, and no value was
       
  1047          * specified in the user-supplied data, then omit it from the tuple.
       
  1048          *
       
  1049          * Note: this checks for sensible values in the supplied primary key
       
  1050          * position of the data.  The following values are considered empty:
       
  1051          *   null, false, true, '', array()
       
  1052          */
       
  1053         if (array_key_exists($pkIdentity, $data)) {
       
  1054             if ($data[$pkIdentity] === null                                        // null
       
  1055                 || $data[$pkIdentity] === ''                                       // empty string
       
  1056                 || is_bool($data[$pkIdentity])                                     // boolean
       
  1057                 || (is_array($data[$pkIdentity]) && empty($data[$pkIdentity]))) {  // empty array
       
  1058                 unset($data[$pkIdentity]);
       
  1059             }
       
  1060         }
       
  1061 
  1044         /**
  1062         /**
  1045          * If this table uses a database sequence object and the data does not
  1063          * If this table uses a database sequence object and the data does not
  1046          * specify a value, then get the next ID from the sequence and add it
  1064          * specify a value, then get the next ID from the sequence and add it
  1047          * to the row.  We assume that only the first column in a compound
  1065          * to the row.  We assume that only the first column in a compound
  1048          * primary key takes a value from a sequence.
  1066          * primary key takes a value from a sequence.
  1049          */
  1067          */
  1050         if (is_string($this->_sequence) && !isset($data[$pkIdentity])) {
  1068         if (is_string($this->_sequence) && !isset($data[$pkIdentity])) {
  1051             $data[$pkIdentity] = $this->_db->nextSequenceId($this->_sequence);
  1069             $data[$pkIdentity] = $this->_db->nextSequenceId($this->_sequence);
  1052             $pkSuppliedBySequence = true;
       
  1053         }
       
  1054 
       
  1055         /**
       
  1056          * If the primary key can be generated automatically, and no value was
       
  1057          * specified in the user-supplied data, then omit it from the tuple.
       
  1058          *
       
  1059          * Note: this checks for sensible values in the supplied primary key
       
  1060          * position of the data.  The following values are considered empty:
       
  1061          *   null, false, true, '', array()
       
  1062          */
       
  1063         if (!isset($pkSuppliedBySequence) && array_key_exists($pkIdentity, $data)) {
       
  1064             if ($data[$pkIdentity] === null                                        // null
       
  1065                 || $data[$pkIdentity] === ''                                       // empty string
       
  1066                 || is_bool($data[$pkIdentity])                                     // boolean
       
  1067                 || (is_array($data[$pkIdentity]) && empty($data[$pkIdentity]))) {  // empty array
       
  1068                 unset($data[$pkIdentity]);
       
  1069             }
       
  1070         }
  1070         }
  1071 
  1071 
  1072         /**
  1072         /**
  1073          * INSERT the new row.
  1073          * INSERT the new row.
  1074          */
  1074          */
  1189                     /**
  1189                     /**
  1190                      * Execute cascading deletes against dependent tables
  1190                      * Execute cascading deletes against dependent tables
  1191                      */
  1191                      */
  1192                     foreach ($depTables as $tableClass) {
  1192                     foreach ($depTables as $tableClass) {
  1193                         $t = self::getTableFromString($tableClass, $this);
  1193                         $t = self::getTableFromString($tableClass, $this);
  1194                         $t->_cascadeDelete($tableClass, $row->getPrimaryKey());
  1194                         $t->_cascadeDelete(
       
  1195                             get_class($this), $row->getPrimaryKey()
       
  1196                         );
  1195                     }
  1197                     }
  1196                 }
  1198                 }
  1197             }
  1199             }
  1198         }
  1200         }
  1199 
  1201 
  1210      */
  1212      */
  1211     public function _cascadeDelete($parentTableClassname, array $primaryKey)
  1213     public function _cascadeDelete($parentTableClassname, array $primaryKey)
  1212     {
  1214     {
  1213         // setup metadata
  1215         // setup metadata
  1214         $this->_setupMetadata();
  1216         $this->_setupMetadata();
  1215         
  1217 
  1216         // get this class name
  1218         // get this class name
  1217         $thisClass = get_class($this);
  1219         $thisClass = get_class($this);
  1218         if ($thisClass === 'Zend_Db_Table') {
  1220         if ($thisClass === 'Zend_Db_Table') {
  1219             $thisClass = $this->_definitionConfigName;
  1221             $thisClass = $this->_definitionConfigName;
  1220         }
  1222         }
  1221         
  1223 
  1222         $rowsAffected = 0;
  1224         $rowsAffected = 0;
  1223         
  1225 
  1224         foreach ($this->_getReferenceMapNormalized() as $map) {
  1226         foreach ($this->_getReferenceMapNormalized() as $map) {
  1225             if ($map[self::REF_TABLE_CLASS] == $parentTableClassname && isset($map[self::ON_DELETE])) {
  1227             if ($map[self::REF_TABLE_CLASS] == $parentTableClassname && isset($map[self::ON_DELETE])) {
  1226                 
  1228 
  1227                 $where = array();
  1229                 $where = array();
  1228                 
  1230 
  1229                 // CASCADE or CASCADE_RECURSE
  1231                 // CASCADE or CASCADE_RECURSE
  1230                 if (in_array($map[self::ON_DELETE], array(self::CASCADE, self::CASCADE_RECURSE))) {
  1232                 if (in_array($map[self::ON_DELETE], array(self::CASCADE, self::CASCADE_RECURSE))) {
  1231                     for ($i = 0; $i < count($map[self::COLUMNS]); ++$i) {
  1233                     for ($i = 0; $i < count($map[self::COLUMNS]); ++$i) {
  1232                         $col = $this->_db->foldCase($map[self::COLUMNS][$i]);
  1234                         $col = $this->_db->foldCase($map[self::COLUMNS][$i]);
  1233                         $refCol = $this->_db->foldCase($map[self::REF_COLUMNS][$i]);
  1235                         $refCol = $this->_db->foldCase($map[self::REF_COLUMNS][$i]);
  1235                         $where[] = $this->_db->quoteInto(
  1237                         $where[] = $this->_db->quoteInto(
  1236                             $this->_db->quoteIdentifier($col, true) . ' = ?',
  1238                             $this->_db->quoteIdentifier($col, true) . ' = ?',
  1237                             $primaryKey[$refCol], $type);
  1239                             $primaryKey[$refCol], $type);
  1238                     }
  1240                     }
  1239                 }
  1241                 }
  1240                 
  1242 
  1241                 // CASCADE_RECURSE
  1243                 // CASCADE_RECURSE
  1242                 if ($map[self::ON_DELETE] == self::CASCADE_RECURSE) {
  1244                 if ($map[self::ON_DELETE] == self::CASCADE_RECURSE) {
  1243                     
  1245 
  1244                     /**
  1246                     /**
  1245                      * Execute cascading deletes against dependent tables
  1247                      * Execute cascading deletes against dependent tables
  1246                      */
  1248                      */
  1247                     $depTables = $this->getDependentTables();
  1249                     $depTables = $this->getDependentTables();
  1248                     if (!empty($depTables)) {
  1250                     if (!empty($depTables)) {
  1257 
  1259 
  1258                 // CASCADE or CASCADE_RECURSE
  1260                 // CASCADE or CASCADE_RECURSE
  1259                 if (in_array($map[self::ON_DELETE], array(self::CASCADE, self::CASCADE_RECURSE))) {
  1261                 if (in_array($map[self::ON_DELETE], array(self::CASCADE, self::CASCADE_RECURSE))) {
  1260                     $rowsAffected += $this->delete($where);
  1262                     $rowsAffected += $this->delete($where);
  1261                 }
  1263                 }
  1262                 
  1264 
  1263             }
  1265             }
  1264         }
  1266         }
  1265         return $rowsAffected;
  1267         return $rowsAffected;
  1266     }
  1268     }
  1267 
  1269 
  1575         $stmt = $this->_db->query($select);
  1577         $stmt = $this->_db->query($select);
  1576         $data = $stmt->fetchAll(Zend_Db::FETCH_ASSOC);
  1578         $data = $stmt->fetchAll(Zend_Db::FETCH_ASSOC);
  1577         return $data;
  1579         return $data;
  1578     }
  1580     }
  1579 
  1581 
       
  1582     /**
       
  1583      * Get table gateway object from string
       
  1584      *
       
  1585      * @param  string                 $tableName
       
  1586      * @param  Zend_Db_Table_Abstract $referenceTable
       
  1587      * @throws Zend_Db_Table_Row_Exception
       
  1588      * @return Zend_Db_Table_Abstract
       
  1589      */
  1580     public static function getTableFromString($tableName, Zend_Db_Table_Abstract $referenceTable = null)
  1590     public static function getTableFromString($tableName, Zend_Db_Table_Abstract $referenceTable = null)
  1581     {
  1591     {
  1582         if ($referenceTable instanceof Zend_Db_Table_Abstract) {
  1592         if ($referenceTable instanceof Zend_Db_Table_Abstract) {
  1583             $tableDefinition = $referenceTable->getDefinition();
  1593             $tableDefinition = $referenceTable->getDefinition();
  1584 
  1594 
  1608             $options[Zend_Db_Table_Abstract::DEFINITION] = $tableDefinition;
  1618             $options[Zend_Db_Table_Abstract::DEFINITION] = $tableDefinition;
  1609         }
  1619         }
  1610 
  1620 
  1611         return new $tableName($options);
  1621         return new $tableName($options);
  1612     }
  1622     }
  1613     
  1623 
  1614 }
  1624 }