# File core/lib/Thelia/Model/om/BaseCouponOrder.php | thelia

       1: <?php
       2:
       3: namespace Thelia\Model\om;
       4:
       5: use \BaseObject;
       6: use \BasePeer;
       7: use \Criteria;
       8: use \DateTime;
       9: use \Exception;
      10: use \PDO;
      11: use \Persistent;
      12: use \Propel;
      13: use \PropelDateTime;
      14: use \PropelException;
      15: use \PropelPDO;
      16: use Thelia\Model\CouponOrder;
      17: use Thelia\Model\CouponOrderPeer;
      18: use Thelia\Model\CouponOrderQuery;
      19: use Thelia\Model\Order;
      20: use Thelia\Model\OrderQuery;
      21:
      22: /**
      23:  * Base class that represents a row from the 'coupon_order' table.
      24:  *
      25:  *
      26:  *
      27:  * @package    propel.generator.Thelia.Model.om
      28:  */
      29: abstract class BaseCouponOrder extends BaseObject implements Persistent
      30: {
      31:     /**
      32:      * Peer class name
      33:      */
      34:     const PEER = 'Thelia\\Model\\CouponOrderPeer';
      35:
      36:     /**
      37:      * The Peer class.
      38:      * Instance provides a convenient way of calling static methods on a class
      39:      * that calling code may not be able to identify.
      40:      * @var        CouponOrderPeer
      41:      */
      42:     protected static $peer;
      43:
      44:     /**
      45:      * The flag var to prevent infinit loop in deep copy
      46:      * @var       boolean
      47:      */
      48:     protected $startCopy = false;
      49:
      50:     /**
      51:      * The value for the id field.
      52:      * @var        int
      53:      */
      54:     protected $id;
      55:
      56:     /**
      57:      * The value for the order_id field.
      58:      * @var        int
      59:      */
      60:     protected $order_id;
      61:
      62:     /**
      63:      * The value for the code field.
      64:      * @var        string
      65:      */
      66:     protected $code;
      67:
      68:     /**
      69:      * The value for the value field.
      70:      * @var        double
      71:      */
      72:     protected $value;
      73:
      74:     /**
      75:      * The value for the created_at field.
      76:      * @var        string
      77:      */
      78:     protected $created_at;
      79:
      80:     /**
      81:      * The value for the updated_at field.
      82:      * @var        string
      83:      */
      84:     protected $updated_at;
      85:
      86:     /**
      87:      * @var        Order
      88:      */
      89:     protected $aOrder;
      90:
      91:     /**
      92:      * Flag to prevent endless save loop, if this object is referenced
      93:      * by another object which falls in this transaction.
      94:      * @var        boolean
      95:      */
      96:     protected $alreadyInSave = false;
      97:
      98:     /**
      99:      * Flag to prevent endless validation loop, if this object is referenced
     100:      * by another object which falls in this transaction.
     101:      * @var        boolean
     102:      */
     103:     protected $alreadyInValidation = false;
     104:
     105:     /**
     106:      * Get the [id] column value.
     107:      *
     108:      * @return int
     109:      */
     110:     public function getId()
     111:     {
     112:         return $this->id;
     113:     }
     114:
     115:     /**
     116:      * Get the [order_id] column value.
     117:      *
     118:      * @return int
     119:      */
     120:     public function getOrderId()
     121:     {
     122:         return $this->order_id;
     123:     }
     124:
     125:     /**
     126:      * Get the [code] column value.
     127:      *
     128:      * @return string
     129:      */
     130:     public function getCode()
     131:     {
     132:         return $this->code;
     133:     }
     134:
     135:     /**
     136:      * Get the [value] column value.
     137:      *
     138:      * @return double
     139:      */
     140:     public function getValue()
     141:     {
     142:         return $this->value;
     143:     }
     144:
     145:     /**
     146:      * Get the [optionally formatted] temporal [created_at] column value.
     147:      *
     148:      *
     149:      * @param string $format The date/time format string (either date()-style or strftime()-style).
     150:      *               If format is null, then the raw DateTime object will be returned.
     151:      * @return mixed Formatted date/time value as string or DateTime object (if format is null), null if column is null, and 0 if column value is 0000-00-00 00:00:00
     152:      * @throws PropelException - if unable to parse/validate the date/time value.
     153:      */
     154:     public function getCreatedAt($format = 'Y-m-d H:i:s')
     155:     {
     156:         if ($this->created_at === null) {
     157:             return null;
     158:         }
     159:
     160:         if ($this->created_at === '0000-00-00 00:00:00') {
     161:             // while technically this is not a default value of null,
     162:             // this seems to be closest in meaning.
     163:             return null;
     164:         } else {
     165:             try {
     166:                 $dt = new DateTime($this->created_at);
     167:             } catch (Exception $x) {
     168:                 throw new PropelException("Internally stored date/time/timestamp value could not be converted to DateTime: " . var_export($this->created_at, true), $x);
     169:             }
     170:         }
     171:
     172:         if ($format === null) {
     173:             // Because propel.useDateTimeClass is true, we return a DateTime object.
     174:             return $dt;
     175:         } elseif (strpos($format, '%') !== false) {
     176:             return strftime($format, $dt->format('U'));
     177:         } else {
     178:             return $dt->format($format);
     179:         }
     180:     }
     181:
     182:     /**
     183:      * Get the [optionally formatted] temporal [updated_at] column value.
     184:      *
     185:      *
     186:      * @param string $format The date/time format string (either date()-style or strftime()-style).
     187:      *               If format is null, then the raw DateTime object will be returned.
     188:      * @return mixed Formatted date/time value as string or DateTime object (if format is null), null if column is null, and 0 if column value is 0000-00-00 00:00:00
     189:      * @throws PropelException - if unable to parse/validate the date/time value.
     190:      */
     191:     public function getUpdatedAt($format = 'Y-m-d H:i:s')
     192:     {
     193:         if ($this->updated_at === null) {
     194:             return null;
     195:         }
     196:
     197:         if ($this->updated_at === '0000-00-00 00:00:00') {
     198:             // while technically this is not a default value of null,
     199:             // this seems to be closest in meaning.
     200:             return null;
     201:         } else {
     202:             try {
     203:                 $dt = new DateTime($this->updated_at);
     204:             } catch (Exception $x) {
     205:                 throw new PropelException("Internally stored date/time/timestamp value could not be converted to DateTime: " . var_export($this->updated_at, true), $x);
     206:             }
     207:         }
     208:
     209:         if ($format === null) {
     210:             // Because propel.useDateTimeClass is true, we return a DateTime object.
     211:             return $dt;
     212:         } elseif (strpos($format, '%') !== false) {
     213:             return strftime($format, $dt->format('U'));
     214:         } else {
     215:             return $dt->format($format);
     216:         }
     217:     }
     218:
     219:     /**
     220:      * Set the value of [id] column.
     221:      *
     222:      * @param int $v new value
     223:      * @return CouponOrder The current object (for fluent API support)
     224:      */
     225:     public function setId($v)
     226:     {
     227:         if ($v !== null) {
     228:             $v = (int) $v;
     229:         }
     230:
     231:         if ($this->id !== $v) {
     232:             $this->id = $v;
     233:             $this->modifiedColumns[] = CouponOrderPeer::ID;
     234:         }
     235:
     236:
     237:         return $this;
     238:     } // setId()
     239:
     240:     /**
     241:      * Set the value of [order_id] column.
     242:      *
     243:      * @param int $v new value
     244:      * @return CouponOrder The current object (for fluent API support)
     245:      */
     246:     public function setOrderId($v)
     247:     {
     248:         if ($v !== null) {
     249:             $v = (int) $v;
     250:         }
     251:
     252:         if ($this->order_id !== $v) {
     253:             $this->order_id = $v;
     254:             $this->modifiedColumns[] = CouponOrderPeer::ORDER_ID;
     255:         }
     256:
     257:         if ($this->aOrder !== null && $this->aOrder->getId() !== $v) {
     258:             $this->aOrder = null;
     259:         }
     260:
     261:
     262:         return $this;
     263:     } // setOrderId()
     264:
     265:     /**
     266:      * Set the value of [code] column.
     267:      *
     268:      * @param string $v new value
     269:      * @return CouponOrder The current object (for fluent API support)
     270:      */
     271:     public function setCode($v)
     272:     {
     273:         if ($v !== null) {
     274:             $v = (string) $v;
     275:         }
     276:
     277:         if ($this->code !== $v) {
     278:             $this->code = $v;
     279:             $this->modifiedColumns[] = CouponOrderPeer::CODE;
     280:         }
     281:
     282:
     283:         return $this;
     284:     } // setCode()
     285:
     286:     /**
     287:      * Set the value of [value] column.
     288:      *
     289:      * @param double $v new value
     290:      * @return CouponOrder The current object (for fluent API support)
     291:      */
     292:     public function setValue($v)
     293:     {
     294:         if ($v !== null) {
     295:             $v = (double) $v;
     296:         }
     297:
     298:         if ($this->value !== $v) {
     299:             $this->value = $v;
     300:             $this->modifiedColumns[] = CouponOrderPeer::VALUE;
     301:         }
     302:
     303:
     304:         return $this;
     305:     } // setValue()
     306:
     307:     /**
     308:      * Sets the value of [created_at] column to a normalized version of the date/time value specified.
     309:      *
     310:      * @param mixed $v string, integer (timestamp), or DateTime value.
     311:      *               Empty strings are treated as null.
     312:      * @return CouponOrder The current object (for fluent API support)
     313:      */
     314:     public function setCreatedAt($v)
     315:     {
     316:         $dt = PropelDateTime::newInstance($v, null, 'DateTime');
     317:         if ($this->created_at !== null || $dt !== null) {
     318:             $currentDateAsString = ($this->created_at !== null && $tmpDt = new DateTime($this->created_at)) ? $tmpDt->format('Y-m-d H:i:s') : null;
     319:             $newDateAsString = $dt ? $dt->format('Y-m-d H:i:s') : null;
     320:             if ($currentDateAsString !== $newDateAsString) {
     321:                 $this->created_at = $newDateAsString;
     322:                 $this->modifiedColumns[] = CouponOrderPeer::CREATED_AT;
     323:             }
     324:         } // if either are not null
     325:
     326:
     327:         return $this;
     328:     } // setCreatedAt()
     329:
     330:     /**
     331:      * Sets the value of [updated_at] column to a normalized version of the date/time value specified.
     332:      *
     333:      * @param mixed $v string, integer (timestamp), or DateTime value.
     334:      *               Empty strings are treated as null.
     335:      * @return CouponOrder The current object (for fluent API support)
     336:      */
     337:     public function setUpdatedAt($v)
     338:     {
     339:         $dt = PropelDateTime::newInstance($v, null, 'DateTime');
     340:         if ($this->updated_at !== null || $dt !== null) {
     341:             $currentDateAsString = ($this->updated_at !== null && $tmpDt = new DateTime($this->updated_at)) ? $tmpDt->format('Y-m-d H:i:s') : null;
     342:             $newDateAsString = $dt ? $dt->format('Y-m-d H:i:s') : null;
     343:             if ($currentDateAsString !== $newDateAsString) {
     344:                 $this->updated_at = $newDateAsString;
     345:                 $this->modifiedColumns[] = CouponOrderPeer::UPDATED_AT;
     346:             }
     347:         } // if either are not null
     348:
     349:
     350:         return $this;
     351:     } // setUpdatedAt()
     352:
     353:     /**
     354:      * Indicates whether the columns in this object are only set to default values.
     355:      *
     356:      * This method can be used in conjunction with isModified() to indicate whether an object is both
     357:      * modified _and_ has some values set which are non-default.
     358:      *
     359:      * @return boolean Whether the columns in this object are only been set with default values.
     360:      */
     361:     public function hasOnlyDefaultValues()
     362:     {
     363:         // otherwise, everything was equal, so return true
     364:         return true;
     365:     } // hasOnlyDefaultValues()
     366:
     367:     /**
     368:      * Hydrates (populates) the object variables with values from the database resultset.
     369:      *
     370:      * An offset (0-based "start column") is specified so that objects can be hydrated
     371:      * with a subset of the columns in the resultset rows.  This is needed, for example,
     372:      * for results of JOIN queries where the resultset row includes columns from two or
     373:      * more tables.
     374:      *
     375:      * @param array $row The row returned by PDOStatement->fetch(PDO::FETCH_NUM)
     376:      * @param int $startcol 0-based offset column which indicates which restultset column to start with.
     377:      * @param boolean $rehydrate Whether this object is being re-hydrated from the database.
     378:      * @return int             next starting column
     379:      * @throws PropelException - Any caught Exception will be rewrapped as a PropelException.
     380:      */
     381:     public function hydrate($row, $startcol = 0, $rehydrate = false)
     382:     {
     383:         try {
     384:
     385:             $this->id = ($row[$startcol + 0] !== null) ? (int) $row[$startcol + 0] : null;
     386:             $this->order_id = ($row[$startcol + 1] !== null) ? (int) $row[$startcol + 1] : null;
     387:             $this->code = ($row[$startcol + 2] !== null) ? (string) $row[$startcol + 2] : null;
     388:             $this->value = ($row[$startcol + 3] !== null) ? (double) $row[$startcol + 3] : null;
     389:             $this->created_at = ($row[$startcol + 4] !== null) ? (string) $row[$startcol + 4] : null;
     390:             $this->updated_at = ($row[$startcol + 5] !== null) ? (string) $row[$startcol + 5] : null;
     391:             $this->resetModified();
     392:
     393:             $this->setNew(false);
     394:
     395:             if ($rehydrate) {
     396:                 $this->ensureConsistency();
     397:             }
     398:
     399:             return $startcol + 6; // 6 = CouponOrderPeer::NUM_HYDRATE_COLUMNS.
     400:
     401:         } catch (Exception $e) {
     402:             throw new PropelException("Error populating CouponOrder object", $e);
     403:         }
     404:     }
     405:
     406:     /**
     407:      * Checks and repairs the internal consistency of the object.
     408:      *
     409:      * This method is executed after an already-instantiated object is re-hydrated
     410:      * from the database.  It exists to check any foreign keys to make sure that
     411:      * the objects related to the current object are correct based on foreign key.
     412:      *
     413:      * You can override this method in the stub class, but you should always invoke
     414:      * the base method from the overridden method (i.e. parent::ensureConsistency()),
     415:      * in case your model changes.
     416:      *
     417:      * @throws PropelException
     418:      */
     419:     public function ensureConsistency()
     420:     {
     421:
     422:         if ($this->aOrder !== null && $this->order_id !== $this->aOrder->getId()) {
     423:             $this->aOrder = null;
     424:         }
     425:     } // ensureConsistency
     426:
     427:     /**
     428:      * Reloads this object from datastore based on primary key and (optionally) resets all associated objects.
     429:      *
     430:      * This will only work if the object has been saved and has a valid primary key set.
     431:      *
     432:      * @param boolean $deep (optional) Whether to also de-associated any related objects.
     433:      * @param PropelPDO $con (optional) The PropelPDO connection to use.
     434:      * @return void
     435:      * @throws PropelException - if this object is deleted, unsaved or doesn't have pk match in db
     436:      */
     437:     public function reload($deep = false, PropelPDO $con = null)
     438:     {
     439:         if ($this->isDeleted()) {
     440:             throw new PropelException("Cannot reload a deleted object.");
     441:         }
     442:
     443:         if ($this->isNew()) {
     444:             throw new PropelException("Cannot reload an unsaved object.");
     445:         }
     446:
     447:         if ($con === null) {
     448:             $con = Propel::getConnection(CouponOrderPeer::DATABASE_NAME, Propel::CONNECTION_READ);
     449:         }
     450:
     451:         // We don't need to alter the object instance pool; we're just modifying this instance
     452:         // already in the pool.
     453:
     454:         $stmt = CouponOrderPeer::doSelectStmt($this->buildPkeyCriteria(), $con);
     455:         $row = $stmt->fetch(PDO::FETCH_NUM);
     456:         $stmt->closeCursor();
     457:         if (!$row) {
     458:             throw new PropelException('Cannot find matching row in the database to reload object values.');
     459:         }
     460:         $this->hydrate($row, 0, true); // rehydrate
     461:
     462:         if ($deep) {  // also de-associate any related objects?
     463:
     464:             $this->aOrder = null;
     465:         } // if (deep)
     466:     }
     467:
     468:     /**
     469:      * Removes this object from datastore and sets delete attribute.
     470:      *
     471:      * @param PropelPDO $con
     472:      * @return void
     473:      * @throws PropelException
     474:      * @throws Exception
     475:      * @see        BaseObject::setDeleted()
     476:      * @see        BaseObject::isDeleted()
     477:      */
     478:     public function delete(PropelPDO $con = null)
     479:     {
     480:         if ($this->isDeleted()) {
     481:             throw new PropelException("This object has already been deleted.");
     482:         }
     483:
     484:         if ($con === null) {
     485:             $con = Propel::getConnection(CouponOrderPeer::DATABASE_NAME, Propel::CONNECTION_WRITE);
     486:         }
     487:
     488:         $con->beginTransaction();
     489:         try {
     490:             $deleteQuery = CouponOrderQuery::create()
     491:                 ->filterByPrimaryKey($this->getPrimaryKey());
     492:             $ret = $this->preDelete($con);
     493:             if ($ret) {
     494:                 $deleteQuery->delete($con);
     495:                 $this->postDelete($con);
     496:                 $con->commit();
     497:                 $this->setDeleted(true);
     498:             } else {
     499:                 $con->commit();
     500:             }
     501:         } catch (Exception $e) {
     502:             $con->rollBack();
     503:             throw $e;
     504:         }
     505:     }
     506:
     507:     /**
     508:      * Persists this object to the database.
     509:      *
     510:      * If the object is new, it inserts it; otherwise an update is performed.
     511:      * All modified related objects will also be persisted in the doSave()
     512:      * method.  This method wraps all precipitate database operations in a
     513:      * single transaction.
     514:      *
     515:      * @param PropelPDO $con
     516:      * @return int             The number of rows affected by this insert/update and any referring fk objects' save() operations.
     517:      * @throws PropelException
     518:      * @throws Exception
     519:      * @see        doSave()
     520:      */
     521:     public function save(PropelPDO $con = null)
     522:     {
     523:         if ($this->isDeleted()) {
     524:             throw new PropelException("You cannot save an object that has been deleted.");
     525:         }
     526:
     527:         if ($con === null) {
     528:             $con = Propel::getConnection(CouponOrderPeer::DATABASE_NAME, Propel::CONNECTION_WRITE);
     529:         }
     530:
     531:         $con->beginTransaction();
     532:         $isInsert = $this->isNew();
     533:         try {
     534:             $ret = $this->preSave($con);
     535:             if ($isInsert) {
     536:                 $ret = $ret && $this->preInsert($con);
     537:                 // timestampable behavior
     538:                 if (!$this->isColumnModified(CouponOrderPeer::CREATED_AT)) {
     539:                     $this->setCreatedAt(time());
     540:                 }
     541:                 if (!$this->isColumnModified(CouponOrderPeer::UPDATED_AT)) {
     542:                     $this->setUpdatedAt(time());
     543:                 }
     544:             } else {
     545:                 $ret = $ret && $this->preUpdate($con);
     546:                 // timestampable behavior
     547:                 if ($this->isModified() && !$this->isColumnModified(CouponOrderPeer::UPDATED_AT)) {
     548:                     $this->setUpdatedAt(time());
     549:                 }
     550:             }
     551:             if ($ret) {
     552:                 $affectedRows = $this->doSave($con);
     553:                 if ($isInsert) {
     554:                     $this->postInsert($con);
     555:                 } else {
     556:                     $this->postUpdate($con);
     557:                 }
     558:                 $this->postSave($con);
     559:                 CouponOrderPeer::addInstanceToPool($this);
     560:             } else {
     561:                 $affectedRows = 0;
     562:             }
     563:             $con->commit();
     564:
     565:             return $affectedRows;
     566:         } catch (Exception $e) {
     567:             $con->rollBack();
     568:             throw $e;
     569:         }
     570:     }
     571:
     572:     /**
     573:      * Performs the work of inserting or updating the row in the database.
     574:      *
     575:      * If the object is new, it inserts it; otherwise an update is performed.
     576:      * All related objects are also updated in this method.
     577:      *
     578:      * @param PropelPDO $con
     579:      * @return int             The number of rows affected by this insert/update and any referring fk objects' save() operations.
     580:      * @throws PropelException
     581:      * @see        save()
     582:      */
     583:     protected function doSave(PropelPDO $con)
     584:     {
     585:         $affectedRows = 0; // initialize var to track total num of affected rows
     586:         if (!$this->alreadyInSave) {
     587:             $this->alreadyInSave = true;
     588:
     589:             // We call the save method on the following object(s) if they
     590:             // were passed to this object by their coresponding set
     591:             // method.  This object relates to these object(s) by a
     592:             // foreign key reference.
     593:
     594:             if ($this->aOrder !== null) {
     595:                 if ($this->aOrder->isModified() || $this->aOrder->isNew()) {
     596:                     $affectedRows += $this->aOrder->save($con);
     597:                 }
     598:                 $this->setOrder($this->aOrder);
     599:             }
     600:
     601:             if ($this->isNew() || $this->isModified()) {
     602:                 // persist changes
     603:                 if ($this->isNew()) {
     604:                     $this->doInsert($con);
     605:                 } else {
     606:                     $this->doUpdate($con);
     607:                 }
     608:                 $affectedRows += 1;
     609:                 $this->resetModified();
     610:             }
     611:
     612:             $this->alreadyInSave = false;
     613:
     614:         }
     615:
     616:         return $affectedRows;
     617:     } // doSave()
     618:
     619:     /**
     620:      * Insert the row in the database.
     621:      *
     622:      * @param PropelPDO $con
     623:      *
     624:      * @throws PropelException
     625:      * @see        doSave()
     626:      */
     627:     protected function doInsert(PropelPDO $con)
     628:     {
     629:         $modifiedColumns = array();
     630:         $index = 0;
     631:
     632:         $this->modifiedColumns[] = CouponOrderPeer::ID;
     633:         if (null !== $this->id) {
     634:             throw new PropelException('Cannot insert a value for auto-increment primary key (' . CouponOrderPeer::ID . ')');
     635:         }
     636:
     637:          // check the columns in natural order for more readable SQL queries
     638:         if ($this->isColumnModified(CouponOrderPeer::ID)) {
     639:             $modifiedColumns[':p' . $index++]  = '`ID`';
     640:         }
     641:         if ($this->isColumnModified(CouponOrderPeer::ORDER_ID)) {
     642:             $modifiedColumns[':p' . $index++]  = '`ORDER_ID`';
     643:         }
     644:         if ($this->isColumnModified(CouponOrderPeer::CODE)) {
     645:             $modifiedColumns[':p' . $index++]  = '`CODE`';
     646:         }
     647:         if ($this->isColumnModified(CouponOrderPeer::VALUE)) {
     648:             $modifiedColumns[':p' . $index++]  = '`VALUE`';
     649:         }
     650:         if ($this->isColumnModified(CouponOrderPeer::CREATED_AT)) {
     651:             $modifiedColumns[':p' . $index++]  = '`CREATED_AT`';
     652:         }
     653:         if ($this->isColumnModified(CouponOrderPeer::UPDATED_AT)) {
     654:             $modifiedColumns[':p' . $index++]  = '`UPDATED_AT`';
     655:         }
     656:
     657:         $sql = sprintf(
     658:             'INSERT INTO `coupon_order` (%s) VALUES (%s)',
     659:             implode(', ', $modifiedColumns),
     660:             implode(', ', array_keys($modifiedColumns))
     661:         );
     662:
     663:         try {
     664:             $stmt = $con->prepare($sql);
     665:             foreach ($modifiedColumns as $identifier => $columnName) {
     666:                 switch ($columnName) {
     667:                     case '`ID`':
     668:                         $stmt->bindValue($identifier, $this->id, PDO::PARAM_INT);
     669:                         break;
     670:                     case '`ORDER_ID`':
     671:                         $stmt->bindValue($identifier, $this->order_id, PDO::PARAM_INT);
     672:                         break;
     673:                     case '`CODE`':
     674:                         $stmt->bindValue($identifier, $this->code, PDO::PARAM_STR);
     675:                         break;
     676:                     case '`VALUE`':
     677:                         $stmt->bindValue($identifier, $this->value, PDO::PARAM_STR);
     678:                         break;
     679:                     case '`CREATED_AT`':
     680:                         $stmt->bindValue($identifier, $this->created_at, PDO::PARAM_STR);
     681:                         break;
     682:                     case '`UPDATED_AT`':
     683:                         $stmt->bindValue($identifier, $this->updated_at, PDO::PARAM_STR);
     684:                         break;
     685:                 }
     686:             }
     687:             $stmt->execute();
     688:         } catch (Exception $e) {
     689:             Propel::log($e->getMessage(), Propel::LOG_ERR);
     690:             throw new PropelException(sprintf('Unable to execute INSERT statement [%s]', $sql), $e);
     691:         }
     692:
     693:         try {
     694:             $pk = $con->lastInsertId();
     695:         } catch (Exception $e) {
     696:             throw new PropelException('Unable to get autoincrement id.', $e);
     697:         }
     698:         $this->setId($pk);
     699:
     700:         $this->setNew(false);
     701:     }
     702:
     703:     /**
     704:      * Update the row in the database.
     705:      *
     706:      * @param PropelPDO $con
     707:      *
     708:      * @see        doSave()
     709:      */
     710:     protected function doUpdate(PropelPDO $con)
     711:     {
     712:         $selectCriteria = $this->buildPkeyCriteria();
     713:         $valuesCriteria = $this->buildCriteria();
     714:         BasePeer::doUpdate($selectCriteria, $valuesCriteria, $con);
     715:     }
     716:
     717:     /**
     718:      * Array of ValidationFailed objects.
     719:      * @var        array ValidationFailed[]
     720:      */
     721:     protected $validationFailures = array();
     722:
     723:     /**
     724:      * Gets any ValidationFailed objects that resulted from last call to validate().
     725:      *
     726:      *
     727:      * @return array ValidationFailed[]
     728:      * @see        validate()
     729:      */
     730:     public function getValidationFailures()
     731:     {
     732:         return $this->validationFailures;
     733:     }
     734:
     735:     /**
     736:      * Validates the objects modified field values and all objects related to this table.
     737:      *
     738:      * If $columns is either a column name or an array of column names
     739:      * only those columns are validated.
     740:      *
     741:      * @param mixed $columns Column name or an array of column names.
     742:      * @return boolean Whether all columns pass validation.
     743:      * @see        doValidate()
     744:      * @see        getValidationFailures()
     745:      */
     746:     public function validate($columns = null)
     747:     {
     748:         $res = $this->doValidate($columns);
     749:         if ($res === true) {
     750:             $this->validationFailures = array();
     751:
     752:             return true;
     753:         } else {
     754:             $this->validationFailures = $res;
     755:
     756:             return false;
     757:         }
     758:     }
     759:
     760:     /**
     761:      * This function performs the validation work for complex object models.
     762:      *
     763:      * In addition to checking the current object, all related objects will
     764:      * also be validated.  If all pass then <code>true</code> is returned; otherwise
     765:      * an aggreagated array of ValidationFailed objects will be returned.
     766:      *
     767:      * @param array $columns Array of column names to validate.
     768:      * @return mixed <code>true</code> if all validations pass; array of <code>ValidationFailed</code> objets otherwise.
     769:      */
     770:     protected function doValidate($columns = null)
     771:     {
     772:         if (!$this->alreadyInValidation) {
     773:             $this->alreadyInValidation = true;
     774:             $retval = null;
     775:
     776:             $failureMap = array();
     777:
     778:
     779:             // We call the validate method on the following object(s) if they
     780:             // were passed to this object by their coresponding set
     781:             // method.  This object relates to these object(s) by a
     782:             // foreign key reference.
     783:
     784:             if ($this->aOrder !== null) {
     785:                 if (!$this->aOrder->validate($columns)) {
     786:                     $failureMap = array_merge($failureMap, $this->aOrder->getValidationFailures());
     787:                 }
     788:             }
     789:
     790:
     791:             if (($retval = CouponOrderPeer::doValidate($this, $columns)) !== true) {
     792:                 $failureMap = array_merge($failureMap, $retval);
     793:             }
     794:
     795:
     796:
     797:             $this->alreadyInValidation = false;
     798:         }
     799:
     800:         return (!empty($failureMap) ? $failureMap : true);
     801:     }
     802:
     803:     /**
     804:      * Retrieves a field from the object by name passed in as a string.
     805:      *
     806:      * @param string $name name
     807:      * @param string $type The type of fieldname the $name is of:
     808:      *               one of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME
     809:      *               BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM.
     810:      *               Defaults to BasePeer::TYPE_PHPNAME
     811:      * @return mixed Value of field.
     812:      */
     813:     public function getByName($name, $type = BasePeer::TYPE_PHPNAME)
     814:     {
     815:         $pos = CouponOrderPeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
     816:         $field = $this->getByPosition($pos);
     817:
     818:         return $field;
     819:     }
     820:
     821:     /**
     822:      * Retrieves a field from the object by Position as specified in the xml schema.
     823:      * Zero-based.
     824:      *
     825:      * @param int $pos position in xml schema
     826:      * @return mixed Value of field at $pos
     827:      */
     828:     public function getByPosition($pos)
     829:     {
     830:         switch ($pos) {
     831:             case 0:
     832:                 return $this->getId();
     833:                 break;
     834:             case 1:
     835:                 return $this->getOrderId();
     836:                 break;
     837:             case 2:
     838:                 return $this->getCode();
     839:                 break;
     840:             case 3:
     841:                 return $this->getValue();
     842:                 break;
     843:             case 4:
     844:                 return $this->getCreatedAt();
     845:                 break;
     846:             case 5:
     847:                 return $this->getUpdatedAt();
     848:                 break;
     849:             default:
     850:                 return null;
     851:                 break;
     852:         } // switch()
     853:     }
     854:
     855:     /**
     856:      * Exports the object as an array.
     857:      *
     858:      * You can specify the key type of the array by passing one of the class
     859:      * type constants.
     860:      *
     861:      * @param     string  $keyType (optional) One of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME,
     862:      *                    BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM.
     863:      *                    Defaults to BasePeer::TYPE_PHPNAME.
     864:      * @param     boolean $includeLazyLoadColumns (optional) Whether to include lazy loaded columns. Defaults to true.
     865:      * @param     array $alreadyDumpedObjects List of objects to skip to avoid recursion
     866:      * @param     boolean $includeForeignObjects (optional) Whether to include hydrated related objects. Default to FALSE.
     867:      *
     868:      * @return array an associative array containing the field names (as keys) and field values
     869:      */
     870:     public function toArray($keyType = BasePeer::TYPE_PHPNAME, $includeLazyLoadColumns = true, $alreadyDumpedObjects = array(), $includeForeignObjects = false)
     871:     {
     872:         if (isset($alreadyDumpedObjects['CouponOrder'][$this->getPrimaryKey()])) {
     873:             return '*RECURSION*';
     874:         }
     875:         $alreadyDumpedObjects['CouponOrder'][$this->getPrimaryKey()] = true;
     876:         $keys = CouponOrderPeer::getFieldNames($keyType);
     877:         $result = array(
     878:             $keys[0] => $this->getId(),
     879:             $keys[1] => $this->getOrderId(),
     880:             $keys[2] => $this->getCode(),
     881:             $keys[3] => $this->getValue(),
     882:             $keys[4] => $this->getCreatedAt(),
     883:             $keys[5] => $this->getUpdatedAt(),
     884:         );
     885:         if ($includeForeignObjects) {
     886:             if (null !== $this->aOrder) {
     887:                 $result['Order'] = $this->aOrder->toArray($keyType, $includeLazyLoadColumns,  $alreadyDumpedObjects, true);
     888:             }
     889:         }
     890:
     891:         return $result;
     892:     }
     893:
     894:     /**
     895:      * Sets a field from the object by name passed in as a string.
     896:      *
     897:      * @param string $name peer name
     898:      * @param mixed $value field value
     899:      * @param string $type The type of fieldname the $name is of:
     900:      *                     one of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME
     901:      *                     BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM.
     902:      *                     Defaults to BasePeer::TYPE_PHPNAME
     903:      * @return void
     904:      */
     905:     public function setByName($name, $value, $type = BasePeer::TYPE_PHPNAME)
     906:     {
     907:         $pos = CouponOrderPeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
     908:
     909:         $this->setByPosition($pos, $value);
     910:     }
     911:
     912:     /**
     913:      * Sets a field from the object by Position as specified in the xml schema.
     914:      * Zero-based.
     915:      *
     916:      * @param int $pos position in xml schema
     917:      * @param mixed $value field value
     918:      * @return void
     919:      */
     920:     public function setByPosition($pos, $value)
     921:     {
     922:         switch ($pos) {
     923:             case 0:
     924:                 $this->setId($value);
     925:                 break;
     926:             case 1:
     927:                 $this->setOrderId($value);
     928:                 break;
     929:             case 2:
     930:                 $this->setCode($value);
     931:                 break;
     932:             case 3:
     933:                 $this->setValue($value);
     934:                 break;
     935:             case 4:
     936:                 $this->setCreatedAt($value);
     937:                 break;
     938:             case 5:
     939:                 $this->setUpdatedAt($value);
     940:                 break;
     941:         } // switch()
     942:     }
     943:
     944:     /**
     945:      * Populates the object using an array.
     946:      *
     947:      * This is particularly useful when populating an object from one of the
     948:      * request arrays (e.g. $_POST).  This method goes through the column
     949:      * names, checking to see whether a matching key exists in populated
     950:      * array. If so the setByName() method is called for that column.
     951:      *
     952:      * You can specify the key type of the array by additionally passing one
     953:      * of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME,
     954:      * BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM.
     955:      * The default key type is the column's BasePeer::TYPE_PHPNAME
     956:      *
     957:      * @param array  $arr     An array to populate the object from.
     958:      * @param string $keyType The type of keys the array uses.
     959:      * @return void
     960:      */
     961:     public function fromArray($arr, $keyType = BasePeer::TYPE_PHPNAME)
     962:     {
     963:         $keys = CouponOrderPeer::getFieldNames($keyType);
     964:
     965:         if (array_key_exists($keys[0], $arr)) $this->setId($arr[$keys[0]]);
     966:         if (array_key_exists($keys[1], $arr)) $this->setOrderId($arr[$keys[1]]);
     967:         if (array_key_exists($keys[2], $arr)) $this->setCode($arr[$keys[2]]);
     968:         if (array_key_exists($keys[3], $arr)) $this->setValue($arr[$keys[3]]);
     969:         if (array_key_exists($keys[4], $arr)) $this->setCreatedAt($arr[$keys[4]]);
     970:         if (array_key_exists($keys[5], $arr)) $this->setUpdatedAt($arr[$keys[5]]);
     971:     }
     972:
     973:     /**
     974:      * Build a Criteria object containing the values of all modified columns in this object.
     975:      *
     976:      * @return Criteria The Criteria object containing all modified values.
     977:      */
     978:     public function buildCriteria()
     979:     {
     980:         $criteria = new Criteria(CouponOrderPeer::DATABASE_NAME);
     981:
     982:         if ($this->isColumnModified(CouponOrderPeer::ID)) $criteria->add(CouponOrderPeer::ID, $this->id);
     983:         if ($this->isColumnModified(CouponOrderPeer::ORDER_ID)) $criteria->add(CouponOrderPeer::ORDER_ID, $this->order_id);
     984:         if ($this->isColumnModified(CouponOrderPeer::CODE)) $criteria->add(CouponOrderPeer::CODE, $this->code);
     985:         if ($this->isColumnModified(CouponOrderPeer::VALUE)) $criteria->add(CouponOrderPeer::VALUE, $this->value);
     986:         if ($this->isColumnModified(CouponOrderPeer::CREATED_AT)) $criteria->add(CouponOrderPeer::CREATED_AT, $this->created_at);
     987:         if ($this->isColumnModified(CouponOrderPeer::UPDATED_AT)) $criteria->add(CouponOrderPeer::UPDATED_AT, $this->updated_at);
     988:
     989:         return $criteria;
     990:     }
     991:
     992:     /**
     993:      * Builds a Criteria object containing the primary key for this object.
     994:      *
     995:      * Unlike buildCriteria() this method includes the primary key values regardless
     996:      * of whether or not they have been modified.
     997:      *
     998:      * @return Criteria The Criteria object containing value(s) for primary key(s).
     999:      */
    1000:     public function buildPkeyCriteria()
    1001:     {
    1002:         $criteria = new Criteria(CouponOrderPeer::DATABASE_NAME);
    1003:         $criteria->add(CouponOrderPeer::ID, $this->id);
    1004:
    1005:         return $criteria;
    1006:     }
    1007:
    1008:     /**
    1009:      * Returns the primary key for this object (row).
    1010:      * @return int
    1011:      */
    1012:     public function getPrimaryKey()
    1013:     {
    1014:         return $this->getId();
    1015:     }
    1016:
    1017:     /**
    1018:      * Generic method to set the primary key (id column).
    1019:      *
    1020:      * @param  int $key Primary key.
    1021:      * @return void
    1022:      */
    1023:     public function setPrimaryKey($key)
    1024:     {
    1025:         $this->setId($key);
    1026:     }
    1027:
    1028:     /**
    1029:      * Returns true if the primary key for this object is null.
    1030:      * @return boolean
    1031:      */
    1032:     public function isPrimaryKeyNull()
    1033:     {
    1034:
    1035:         return null === $this->getId();
    1036:     }
    1037:
    1038:     /**
    1039:      * Sets contents of passed object to values from current object.
    1040:      *
    1041:      * If desired, this method can also make copies of all associated (fkey referrers)
    1042:      * objects.
    1043:      *
    1044:      * @param object $copyObj An object of CouponOrder (or compatible) type.
    1045:      * @param boolean $deepCopy Whether to also copy all rows that refer (by fkey) to the current row.
    1046:      * @param boolean $makeNew Whether to reset autoincrement PKs and make the object new.
    1047:      * @throws PropelException
    1048:      */
    1049:     public function copyInto($copyObj, $deepCopy = false, $makeNew = true)
    1050:     {
    1051:         $copyObj->setOrderId($this->getOrderId());
    1052:         $copyObj->setCode($this->getCode());
    1053:         $copyObj->setValue($this->getValue());
    1054:         $copyObj->setCreatedAt($this->getCreatedAt());
    1055:         $copyObj->setUpdatedAt($this->getUpdatedAt());
    1056:
    1057:         if ($deepCopy && !$this->startCopy) {
    1058:             // important: temporarily setNew(false) because this affects the behavior of
    1059:             // the getter/setter methods for fkey referrer objects.
    1060:             $copyObj->setNew(false);
    1061:             // store object hash to prevent cycle
    1062:             $this->startCopy = true;
    1063:
    1064:             //unflag object copy
    1065:             $this->startCopy = false;
    1066:         } // if ($deepCopy)
    1067:
    1068:         if ($makeNew) {
    1069:             $copyObj->setNew(true);
    1070:             $copyObj->setId(NULL); // this is a auto-increment column, so set to default value
    1071:         }
    1072:     }
    1073:
    1074:     /**
    1075:      * Makes a copy of this object that will be inserted as a new row in table when saved.
    1076:      * It creates a new object filling in the simple attributes, but skipping any primary
    1077:      * keys that are defined for the table.
    1078:      *
    1079:      * If desired, this method can also make copies of all associated (fkey referrers)
    1080:      * objects.
    1081:      *
    1082:      * @param boolean $deepCopy Whether to also copy all rows that refer (by fkey) to the current row.
    1083:      * @return CouponOrder Clone of current object.
    1084:      * @throws PropelException
    1085:      */
    1086:     public function copy($deepCopy = false)
    1087:     {
    1088:         // we use get_class(), because this might be a subclass
    1089:         $clazz = get_class($this);
    1090:         $copyObj = new $clazz();
    1091:         $this->copyInto($copyObj, $deepCopy);
    1092:
    1093:         return $copyObj;
    1094:     }
    1095:
    1096:     /**
    1097:      * Returns a peer instance associated with this om.
    1098:      *
    1099:      * Since Peer classes are not to have any instance attributes, this method returns the
    1100:      * same instance for all member of this class. The method could therefore
    1101:      * be static, but this would prevent one from overriding the behavior.
    1102:      *
    1103:      * @return CouponOrderPeer
    1104:      */
    1105:     public function getPeer()
    1106:     {
    1107:         if (self::$peer === null) {
    1108:             self::$peer = new CouponOrderPeer();
    1109:         }
    1110:
    1111:         return self::$peer;
    1112:     }
    1113:
    1114:     /**
    1115:      * Declares an association between this object and a Order object.
    1116:      *
    1117:      * @param             Order $v
    1118:      * @return CouponOrder The current object (for fluent API support)
    1119:      * @throws PropelException
    1120:      */
    1121:     public function setOrder(Order $v = null)
    1122:     {
    1123:         if ($v === null) {
    1124:             $this->setOrderId(NULL);
    1125:         } else {
    1126:             $this->setOrderId($v->getId());
    1127:         }
    1128:
    1129:         $this->aOrder = $v;
    1130:
    1131:         // Add binding for other direction of this n:n relationship.
    1132:         // If this object has already been added to the Order object, it will not be re-added.
    1133:         if ($v !== null) {
    1134:             $v->addCouponOrder($this);
    1135:         }
    1136:
    1137:
    1138:         return $this;
    1139:     }
    1140:
    1141:
    1142:     /**
    1143:      * Get the associated Order object
    1144:      *
    1145:      * @param PropelPDO $con Optional Connection object.
    1146:      * @return Order The associated Order object.
    1147:      * @throws PropelException
    1148:      */
    1149:     public function getOrder(PropelPDO $con = null)
    1150:     {
    1151:         if ($this->aOrder === null && ($this->order_id !== null)) {
    1152:             $this->aOrder = OrderQuery::create()->findPk($this->order_id, $con);
    1153:             /* The following can be used additionally to
    1154:                 guarantee the related object contains a reference
    1155:                 to this object.  This level of coupling may, however, be
    1156:                 undesirable since it could result in an only partially populated collection
    1157:                 in the referenced object.
    1158:                 $this->aOrder->addCouponOrders($this);
    1159:              */
    1160:         }
    1161:
    1162:         return $this->aOrder;
    1163:     }
    1164:
    1165:     /**
    1166:      * Clears the current object and sets all attributes to their default values
    1167:      */
    1168:     public function clear()
    1169:     {
    1170:         $this->id = null;
    1171:         $this->order_id = null;
    1172:         $this->code = null;
    1173:         $this->value = null;
    1174:         $this->created_at = null;
    1175:         $this->updated_at = null;
    1176:         $this->alreadyInSave = false;
    1177:         $this->alreadyInValidation = false;
    1178:         $this->clearAllReferences();
    1179:         $this->resetModified();
    1180:         $this->setNew(true);
    1181:         $this->setDeleted(false);
    1182:     }
    1183:
    1184:     /**
    1185:      * Resets all references to other model objects or collections of model objects.
    1186:      *
    1187:      * This method is a user-space workaround for PHP's inability to garbage collect
    1188:      * objects with circular references (even in PHP 5.3). This is currently necessary
    1189:      * when using Propel in certain daemon or large-volumne/high-memory operations.
    1190:      *
    1191:      * @param boolean $deep Whether to also clear the references on all referrer objects.
    1192:      */
    1193:     public function clearAllReferences($deep = false)
    1194:     {
    1195:         if ($deep) {
    1196:         } // if ($deep)
    1197:
    1198:         $this->aOrder = null;
    1199:     }
    1200:
    1201:     /**
    1202:      * return the string representation of this object
    1203:      *
    1204:      * @return string
    1205:      */
    1206:     public function __toString()
    1207:     {
    1208:         return (string) $this->exportTo(CouponOrderPeer::DEFAULT_STRING_FORMAT);
    1209:     }
    1210:
    1211:     /**
    1212:      * return true is the object is in saving state
    1213:      *
    1214:      * @return boolean
    1215:      */
    1216:     public function isAlreadyInSave()
    1217:     {
    1218:         return $this->alreadyInSave;
    1219:     }
    1220:
    1221:     // timestampable behavior
    1222:
    1223:     /**
    1224:      * Mark the current object so that the update date doesn't get updated during next save
    1225:      *
    1226:      * @return     CouponOrder The current object (for fluent API support)
    1227:      */
    1228:     public function keepUpdateDateUnchanged()
    1229:     {
    1230:         $this->modifiedColumns[] = CouponOrderPeer::UPDATED_AT;
    1231:
    1232:         return $this;
    1233:     }
    1234:
    1235: }
    1236: