       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 \PropelCollection;
      14: use \PropelDateTime;
      15: use \PropelException;
      16: use \PropelObjectCollection;
      17: use \PropelPDO;
      18: use Thelia\Model\Coupon;
      19: use Thelia\Model\CouponPeer;
      20: use Thelia\Model\CouponQuery;
      21: use Thelia\Model\CouponRule;
      22: use Thelia\Model\CouponRuleQuery;
      23:
      24: /**
      25:  * Base class that represents a row from the 'coupon' table.
      26:  *
      27:  *
      28:  *
      29:  * @package    propel.generator.Thelia.Model.om
      30:  */
      31: abstract class BaseCoupon extends BaseObject implements Persistent
      32: {
      33:     /**
      34:      * Peer class name
      35:      */
      36:     const PEER = 'Thelia\\Model\\CouponPeer';
      37:
      38:     /**
      39:      * The Peer class.
      40:      * Instance provides a convenient way of calling static methods on a class
      41:      * that calling code may not be able to identify.
      42:      * @var        CouponPeer
      43:      */
      44:     protected static $peer;
      45:
      46:     /**
      47:      * The flag var to prevent infinit loop in deep copy
      48:      * @var       boolean
      49:      */
      50:     protected $startCopy = false;
      51:
      52:     /**
      53:      * The value for the id field.
      54:      * @var        int
      55:      */
      56:     protected $id;
      57:
      58:     /**
      59:      * The value for the code field.
      60:      * @var        string
      61:      */
      62:     protected $code;
      63:
      64:     /**
      65:      * The value for the action field.
      66:      * @var        string
      67:      */
      68:     protected $action;
      69:
      70:     /**
      71:      * The value for the value field.
      72:      * @var        double
      73:      */
      74:     protected $value;
      75:
      76:     /**
      77:      * The value for the used field.
      78:      * @var        int
      79:      */
      80:     protected $used;
      81:
      82:     /**
      83:      * The value for the available_since field.
      84:      * @var        string
      85:      */
      86:     protected $available_since;
      87:
      88:     /**
      89:      * The value for the date_limit field.
      90:      * @var        string
      91:      */
      92:     protected $date_limit;
      93:
      94:     /**
      95:      * The value for the activate field.
      96:      * @var        int
      97:      */
      98:     protected $activate;
      99:
     100:     /**
     101:      * The value for the created_at field.
     102:      * @var        string
     103:      */
     104:     protected $created_at;
     105:
     106:     /**
     107:      * The value for the updated_at field.
     108:      * @var        string
     109:      */
     110:     protected $updated_at;
     111:
     112:     /**
     113:      * @var        PropelObjectCollection|CouponRule[] Collection to store aggregation of CouponRule objects.
     114:      */
     115:     protected $collCouponRules;
     116:     protected $collCouponRulesPartial;
     117:
     118:     /**
     119:      * Flag to prevent endless save loop, if this object is referenced
     120:      * by another object which falls in this transaction.
     121:      * @var        boolean
     122:      */
     123:     protected $alreadyInSave = false;
     124:
     125:     /**
     126:      * Flag to prevent endless validation loop, if this object is referenced
     127:      * by another object which falls in this transaction.
     128:      * @var        boolean
     129:      */
     130:     protected $alreadyInValidation = false;
     131:
     132:     /**
     133:      * Flag to prevent endless clearAllReferences($deep=true) loop, if this object is referenced
     134:      * @var        boolean
     135:      */
     136:     protected $alreadyInClearAllReferencesDeep = false;
     137:
     138:     /**
     139:      * An array of objects scheduled for deletion.
     140:      * @var     PropelObjectCollection
     141:      */
     142:     protected $couponRulesScheduledForDeletion = null;
     143:
     144:     /**
     145:      * Get the [id] column value.
     146:      *
     147:      * @return int
     148:      */
     149:     public function getId()
     150:     {
     151:         return $this->id;
     152:     }
     153:
     154:     /**
     155:      * Get the [code] column value.
     156:      *
     157:      * @return string
     158:      */
     159:     public function getCode()
     160:     {
     161:         return $this->code;
     162:     }
     163:
     164:     /**
     165:      * Get the [action] column value.
     166:      *
     167:      * @return string
     168:      */
     169:     public function getAction()
     170:     {
     171:         return $this->action;
     172:     }
     173:
     174:     /**
     175:      * Get the [value] column value.
     176:      *
     177:      * @return double
     178:      */
     179:     public function getValue()
     180:     {
     181:         return $this->value;
     182:     }
     183:
     184:     /**
     185:      * Get the [used] column value.
     186:      *
     187:      * @return int
     188:      */
     189:     public function getUsed()
     190:     {
     191:         return $this->used;
     192:     }
     193:
     194:     /**
     195:      * Get the [optionally formatted] temporal [available_since] column value.
     196:      *
     197:      *
     198:      * @param string $format The date/time format string (either date()-style or strftime()-style).
     199:      *               If format is null, then the raw DateTime object will be returned.
     200:      * @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
     201:      * @throws PropelException - if unable to parse/validate the date/time value.
     202:      */
     203:     public function getAvailableSince($format = 'Y-m-d H:i:s')
     204:     {
     205:         if ($this->available_since === null) {
     206:             return null;
     207:         }
     208:
     209:         if ($this->available_since === '0000-00-00 00:00:00') {
     210:             // while technically this is not a default value of null,
     211:             // this seems to be closest in meaning.
     212:             return null;
     213:         }
     214:
     215:         try {
     216:             $dt = new DateTime($this->available_since);
     217:         } catch (Exception $x) {
     218:             throw new PropelException("Internally stored date/time/timestamp value could not be converted to DateTime: " . var_export($this->available_since, true), $x);
     219:         }
     220:
     221:         if ($format === null) {
     222:             // Because propel.useDateTimeClass is true, we return a DateTime object.
     223:             return $dt;
     224:         }
     225:
     226:         if (strpos($format, '%') !== false) {
     227:             return strftime($format, $dt->format('U'));
     228:         }
     229:
     230:         return $dt->format($format);
     231:
     232:     }
     233:
     234:     /**
     235:      * Get the [optionally formatted] temporal [date_limit] column value.
     236:      *
     237:      *
     238:      * @param string $format The date/time format string (either date()-style or strftime()-style).
     239:      *               If format is null, then the raw DateTime object will be returned.
     240:      * @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
     241:      * @throws PropelException - if unable to parse/validate the date/time value.
     242:      */
     243:     public function getDateLimit($format = 'Y-m-d H:i:s')
     244:     {
     245:         if ($this->date_limit === null) {
     246:             return null;
     247:         }
     248:
     249:         if ($this->date_limit === '0000-00-00 00:00:00') {
     250:             // while technically this is not a default value of null,
     251:             // this seems to be closest in meaning.
     252:             return null;
     253:         }
     254:
     255:         try {
     256:             $dt = new DateTime($this->date_limit);
     257:         } catch (Exception $x) {
     258:             throw new PropelException("Internally stored date/time/timestamp value could not be converted to DateTime: " . var_export($this->date_limit, true), $x);
     259:         }
     260:
     261:         if ($format === null) {
     262:             // Because propel.useDateTimeClass is true, we return a DateTime object.
     263:             return $dt;
     264:         }
     265:
     266:         if (strpos($format, '%') !== false) {
     267:             return strftime($format, $dt->format('U'));
     268:         }
     269:
     270:         return $dt->format($format);
     271:
     272:     }
     273:
     274:     /**
     275:      * Get the [activate] column value.
     276:      *
     277:      * @return int
     278:      */
     279:     public function getActivate()
     280:     {
     281:         return $this->activate;
     282:     }
     283:
     284:     /**
     285:      * Get the [optionally formatted] temporal [created_at] column value.
     286:      *
     287:      *
     288:      * @param string $format The date/time format string (either date()-style or strftime()-style).
     289:      *               If format is null, then the raw DateTime object will be returned.
     290:      * @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
     291:      * @throws PropelException - if unable to parse/validate the date/time value.
     292:      */
     293:     public function getCreatedAt($format = 'Y-m-d H:i:s')
     294:     {
     295:         if ($this->created_at === null) {
     296:             return null;
     297:         }
     298:
     299:         if ($this->created_at === '0000-00-00 00:00:00') {
     300:             // while technically this is not a default value of null,
     301:             // this seems to be closest in meaning.
     302:             return null;
     303:         }
     304:
     305:         try {
     306:             $dt = new DateTime($this->created_at);
     307:         } catch (Exception $x) {
     308:             throw new PropelException("Internally stored date/time/timestamp value could not be converted to DateTime: " . var_export($this->created_at, true), $x);
     309:         }
     310:
     311:         if ($format === null) {
     312:             // Because propel.useDateTimeClass is true, we return a DateTime object.
     313:             return $dt;
     314:         }
     315:
     316:         if (strpos($format, '%') !== false) {
     317:             return strftime($format, $dt->format('U'));
     318:         }
     319:
     320:         return $dt->format($format);
     321:
     322:     }
     323:
     324:     /**
     325:      * Get the [optionally formatted] temporal [updated_at] column value.
     326:      *
     327:      *
     328:      * @param string $format The date/time format string (either date()-style or strftime()-style).
     329:      *               If format is null, then the raw DateTime object will be returned.
     330:      * @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
     331:      * @throws PropelException - if unable to parse/validate the date/time value.
     332:      */
     333:     public function getUpdatedAt($format = 'Y-m-d H:i:s')
     334:     {
     335:         if ($this->updated_at === null) {
     336:             return null;
     337:         }
     338:
     339:         if ($this->updated_at === '0000-00-00 00:00:00') {
     340:             // while technically this is not a default value of null,
     341:             // this seems to be closest in meaning.
     342:             return null;
     343:         }
     344:
     345:         try {
     346:             $dt = new DateTime($this->updated_at);
     347:         } catch (Exception $x) {
     348:             throw new PropelException("Internally stored date/time/timestamp value could not be converted to DateTime: " . var_export($this->updated_at, true), $x);
     349:         }
     350:
     351:         if ($format === null) {
     352:             // Because propel.useDateTimeClass is true, we return a DateTime object.
     353:             return $dt;
     354:         }
     355:
     356:         if (strpos($format, '%') !== false) {
     357:             return strftime($format, $dt->format('U'));
     358:         }
     359:
     360:         return $dt->format($format);
     361:
     362:     }
     363:
     364:     /**
     365:      * Set the value of [id] column.
     366:      *
     367:      * @param int $v new value
     368:      * @return Coupon The current object (for fluent API support)
     369:      */
     370:     public function setId($v)
     371:     {
     372:         if ($v !== null && is_numeric($v)) {
     373:             $v = (int) $v;
     374:         }
     375:
     376:         if ($this->id !== $v) {
     377:             $this->id = $v;
     378:             $this->modifiedColumns[] = CouponPeer::ID;
     379:         }
     380:
     381:
     382:         return $this;
     383:     } // setId()
     384:
     385:     /**
     386:      * Set the value of [code] column.
     387:      *
     388:      * @param string $v new value
     389:      * @return Coupon The current object (for fluent API support)
     390:      */
     391:     public function setCode($v)
     392:     {
     393:         if ($v !== null && is_numeric($v)) {
     394:             $v = (string) $v;
     395:         }
     396:
     397:         if ($this->code !== $v) {
     398:             $this->code = $v;
     399:             $this->modifiedColumns[] = CouponPeer::CODE;
     400:         }
     401:
     402:
     403:         return $this;
     404:     } // setCode()
     405:
     406:     /**
     407:      * Set the value of [action] column.
     408:      *
     409:      * @param string $v new value
     410:      * @return Coupon The current object (for fluent API support)
     411:      */
     412:     public function setAction($v)
     413:     {
     414:         if ($v !== null && is_numeric($v)) {
     415:             $v = (string) $v;
     416:         }
     417:
     418:         if ($this->action !== $v) {
     419:             $this->action = $v;
     420:             $this->modifiedColumns[] = CouponPeer::ACTION;
     421:         }
     422:
     423:
     424:         return $this;
     425:     } // setAction()
     426:
     427:     /**
     428:      * Set the value of [value] column.
     429:      *
     430:      * @param double $v new value
     431:      * @return Coupon The current object (for fluent API support)
     432:      */
     433:     public function setValue($v)
     434:     {
     435:         if ($v !== null && is_numeric($v)) {
     436:             $v = (double) $v;
     437:         }
     438:
     439:         if ($this->value !== $v) {
     440:             $this->value = $v;
     441:             $this->modifiedColumns[] = CouponPeer::VALUE;
     442:         }
     443:
     444:
     445:         return $this;
     446:     } // setValue()
     447:
     448:     /**
     449:      * Set the value of [used] column.
     450:      *
     451:      * @param int $v new value
     452:      * @return Coupon The current object (for fluent API support)
     453:      */
     454:     public function setUsed($v)
     455:     {
     456:         if ($v !== null && is_numeric($v)) {
     457:             $v = (int) $v;
     458:         }
     459:
     460:         if ($this->used !== $v) {
     461:             $this->used = $v;
     462:             $this->modifiedColumns[] = CouponPeer::USED;
     463:         }
     464:
     465:
     466:         return $this;
     467:     } // setUsed()
     468:
     469:     /**
     470:      * Sets the value of [available_since] column to a normalized version of the date/time value specified.
     471:      *
     472:      * @param mixed $v string, integer (timestamp), or DateTime value.
     473:      *               Empty strings are treated as null.
     474:      * @return Coupon The current object (for fluent API support)
     475:      */
     476:     public function setAvailableSince($v)
     477:     {
     478:         $dt = PropelDateTime::newInstance($v, null, 'DateTime');
     479:         if ($this->available_since !== null || $dt !== null) {
     480:             $currentDateAsString = ($this->available_since !== null && $tmpDt = new DateTime($this->available_since)) ? $tmpDt->format('Y-m-d H:i:s') : null;
     481:             $newDateAsString = $dt ? $dt->format('Y-m-d H:i:s') : null;
     482:             if ($currentDateAsString !== $newDateAsString) {
     483:                 $this->available_since = $newDateAsString;
     484:                 $this->modifiedColumns[] = CouponPeer::AVAILABLE_SINCE;
     485:             }
     486:         } // if either are not null
     487:
     488:
     489:         return $this;
     490:     } // setAvailableSince()
     491:
     492:     /**
     493:      * Sets the value of [date_limit] column to a normalized version of the date/time value specified.
     494:      *
     495:      * @param mixed $v string, integer (timestamp), or DateTime value.
     496:      *               Empty strings are treated as null.
     497:      * @return Coupon The current object (for fluent API support)
     498:      */
     499:     public function setDateLimit($v)
     500:     {
     501:         $dt = PropelDateTime::newInstance($v, null, 'DateTime');
     502:         if ($this->date_limit !== null || $dt !== null) {
     503:             $currentDateAsString = ($this->date_limit !== null && $tmpDt = new DateTime($this->date_limit)) ? $tmpDt->format('Y-m-d H:i:s') : null;
     504:             $newDateAsString = $dt ? $dt->format('Y-m-d H:i:s') : null;
     505:             if ($currentDateAsString !== $newDateAsString) {
     506:                 $this->date_limit = $newDateAsString;
     507:                 $this->modifiedColumns[] = CouponPeer::DATE_LIMIT;
     508:             }
     509:         } // if either are not null
     510:
     511:
     512:         return $this;
     513:     } // setDateLimit()
     514:
     515:     /**
     516:      * Set the value of [activate] column.
     517:      *
     518:      * @param int $v new value
     519:      * @return Coupon The current object (for fluent API support)
     520:      */
     521:     public function setActivate($v)
     522:     {
     523:         if ($v !== null && is_numeric($v)) {
     524:             $v = (int) $v;
     525:         }
     526:
     527:         if ($this->activate !== $v) {
     528:             $this->activate = $v;
     529:             $this->modifiedColumns[] = CouponPeer::ACTIVATE;
     530:         }
     531:
     532:
     533:         return $this;
     534:     } // setActivate()
     535:
     536:     /**
     537:      * Sets the value of [created_at] column to a normalized version of the date/time value specified.
     538:      *
     539:      * @param mixed $v string, integer (timestamp), or DateTime value.
     540:      *               Empty strings are treated as null.
     541:      * @return Coupon The current object (for fluent API support)
     542:      */
     543:     public function setCreatedAt($v)
     544:     {
     545:         $dt = PropelDateTime::newInstance($v, null, 'DateTime');
     546:         if ($this->created_at !== null || $dt !== null) {
     547:             $currentDateAsString = ($this->created_at !== null && $tmpDt = new DateTime($this->created_at)) ? $tmpDt->format('Y-m-d H:i:s') : null;
     548:             $newDateAsString = $dt ? $dt->format('Y-m-d H:i:s') : null;
     549:             if ($currentDateAsString !== $newDateAsString) {
     550:                 $this->created_at = $newDateAsString;
     551:                 $this->modifiedColumns[] = CouponPeer::CREATED_AT;
     552:             }
     553:         } // if either are not null
     554:
     555:
     556:         return $this;
     557:     } // setCreatedAt()
     558:
     559:     /**
     560:      * Sets the value of [updated_at] column to a normalized version of the date/time value specified.
     561:      *
     562:      * @param mixed $v string, integer (timestamp), or DateTime value.
     563:      *               Empty strings are treated as null.
     564:      * @return Coupon The current object (for fluent API support)
     565:      */
     566:     public function setUpdatedAt($v)
     567:     {
     568:         $dt = PropelDateTime::newInstance($v, null, 'DateTime');
     569:         if ($this->updated_at !== null || $dt !== null) {
     570:             $currentDateAsString = ($this->updated_at !== null && $tmpDt = new DateTime($this->updated_at)) ? $tmpDt->format('Y-m-d H:i:s') : null;
     571:             $newDateAsString = $dt ? $dt->format('Y-m-d H:i:s') : null;
     572:             if ($currentDateAsString !== $newDateAsString) {
     573:                 $this->updated_at = $newDateAsString;
     574:                 $this->modifiedColumns[] = CouponPeer::UPDATED_AT;
     575:             }
     576:         } // if either are not null
     577:
     578:
     579:         return $this;
     580:     } // setUpdatedAt()
     581:
     582:     /**
     583:      * Indicates whether the columns in this object are only set to default values.
     584:      *
     585:      * This method can be used in conjunction with isModified() to indicate whether an object is both
     586:      * modified _and_ has some values set which are non-default.
     587:      *
     588:      * @return boolean Whether the columns in this object are only been set with default values.
     589:      */
     590:     public function hasOnlyDefaultValues()
     591:     {
     592:         // otherwise, everything was equal, so return true
     593:         return true;
     594:     } // hasOnlyDefaultValues()
     595:
     596:     /**
     597:      * Hydrates (populates) the object variables with values from the database resultset.
     598:      *
     599:      * An offset (0-based "start column") is specified so that objects can be hydrated
     600:      * with a subset of the columns in the resultset rows.  This is needed, for example,
     601:      * for results of JOIN queries where the resultset row includes columns from two or
     602:      * more tables.
     603:      *
     604:      * @param array $row The row returned by PDOStatement->fetch(PDO::FETCH_NUM)
     605:      * @param int $startcol 0-based offset column which indicates which restultset column to start with.
     606:      * @param boolean $rehydrate Whether this object is being re-hydrated from the database.
     607:      * @return int             next starting column
     608:      * @throws PropelException - Any caught Exception will be rewrapped as a PropelException.
     609:      */
     610:     public function hydrate($row, $startcol = 0, $rehydrate = false)
     611:     {
     612:         try {
     613:
     614:             $this->id = ($row[$startcol + 0] !== null) ? (int) $row[$startcol + 0] : null;
     615:             $this->code = ($row[$startcol + 1] !== null) ? (string) $row[$startcol + 1] : null;
     616:             $this->action = ($row[$startcol + 2] !== null) ? (string) $row[$startcol + 2] : null;
     617:             $this->value = ($row[$startcol + 3] !== null) ? (double) $row[$startcol + 3] : null;
     618:             $this->used = ($row[$startcol + 4] !== null) ? (int) $row[$startcol + 4] : null;
     619:             $this->available_since = ($row[$startcol + 5] !== null) ? (string) $row[$startcol + 5] : null;
     620:             $this->date_limit = ($row[$startcol + 6] !== null) ? (string) $row[$startcol + 6] : null;
     621:             $this->activate = ($row[$startcol + 7] !== null) ? (int) $row[$startcol + 7] : null;
     622:             $this->created_at = ($row[$startcol + 8] !== null) ? (string) $row[$startcol + 8] : null;
     623:             $this->updated_at = ($row[$startcol + 9] !== null) ? (string) $row[$startcol + 9] : null;
     624:             $this->resetModified();
     625:
     626:             $this->setNew(false);
     627:
     628:             if ($rehydrate) {
     629:                 $this->ensureConsistency();
     630:             }
     631:             $this->postHydrate($row, $startcol, $rehydrate);
     632:             return $startcol + 10; // 10 = CouponPeer::NUM_HYDRATE_COLUMNS.
     633:
     634:         } catch (Exception $e) {
     635:             throw new PropelException("Error populating Coupon object", $e);
     636:         }
     637:     }
     638:
     639:     /**
     640:      * Checks and repairs the internal consistency of the object.
     641:      *
     642:      * This method is executed after an already-instantiated object is re-hydrated
     643:      * from the database.  It exists to check any foreign keys to make sure that
     644:      * the objects related to the current object are correct based on foreign key.
     645:      *
     646:      * You can override this method in the stub class, but you should always invoke
     647:      * the base method from the overridden method (i.e. parent::ensureConsistency()),
     648:      * in case your model changes.
     649:      *
     650:      * @throws PropelException
     651:      */
     652:     public function ensureConsistency()
     653:     {
     654:
     655:     } // ensureConsistency
     656:
     657:     /**
     658:      * Reloads this object from datastore based on primary key and (optionally) resets all associated objects.
     659:      *
     660:      * This will only work if the object has been saved and has a valid primary key set.
     661:      *
     662:      * @param boolean $deep (optional) Whether to also de-associated any related objects.
     663:      * @param PropelPDO $con (optional) The PropelPDO connection to use.
     664:      * @return void
     665:      * @throws PropelException - if this object is deleted, unsaved or doesn't have pk match in db
     666:      */
     667:     public function reload($deep = false, PropelPDO $con = null)
     668:     {
     669:         if ($this->isDeleted()) {
     670:             throw new PropelException("Cannot reload a deleted object.");
     671:         }
     672:
     673:         if ($this->isNew()) {
     674:             throw new PropelException("Cannot reload an unsaved object.");
     675:         }
     676:
     677:         if ($con === null) {
     678:             $con = Propel::getConnection(CouponPeer::DATABASE_NAME, Propel::CONNECTION_READ);
     679:         }
     680:
     681:         // We don't need to alter the object instance pool; we're just modifying this instance
     682:         // already in the pool.
     683:
     684:         $stmt = CouponPeer::doSelectStmt($this->buildPkeyCriteria(), $con);
     685:         $row = $stmt->fetch(PDO::FETCH_NUM);
     686:         $stmt->closeCursor();
     687:         if (!$row) {
     688:             throw new PropelException('Cannot find matching row in the database to reload object values.');
     689:         }
     690:         $this->hydrate($row, 0, true); // rehydrate
     691:
     692:         if ($deep) {  // also de-associate any related objects?
     693:
     694:             $this->collCouponRules = null;
     695:
     696:         } // if (deep)
     697:     }
     698:
     699:     /**
     700:      * Removes this object from datastore and sets delete attribute.
     701:      *
     702:      * @param PropelPDO $con
     703:      * @return void
     704:      * @throws PropelException
     705:      * @throws Exception
     706:      * @see        BaseObject::setDeleted()
     707:      * @see        BaseObject::isDeleted()
     708:      */
     709:     public function delete(PropelPDO $con = null)
     710:     {
     711:         if ($this->isDeleted()) {
     712:             throw new PropelException("This object has already been deleted.");
     713:         }
     714:
     715:         if ($con === null) {
     716:             $con = Propel::getConnection(CouponPeer::DATABASE_NAME, Propel::CONNECTION_WRITE);
     717:         }
     718:
     719:         $con->beginTransaction();
     720:         try {
     721:             $deleteQuery = CouponQuery::create()
     722:                 ->filterByPrimaryKey($this->getPrimaryKey());
     723:             $ret = $this->preDelete($con);
     724:             if ($ret) {
     725:                 $deleteQuery->delete($con);
     726:                 $this->postDelete($con);
     727:                 $con->commit();
     728:                 $this->setDeleted(true);
     729:             } else {
     730:                 $con->commit();
     731:             }
     732:         } catch (Exception $e) {
     733:             $con->rollBack();
     734:             throw $e;
     735:         }
     736:     }
     737:
     738:     /**
     739:      * Persists this object to the database.
     740:      *
     741:      * If the object is new, it inserts it; otherwise an update is performed.
     742:      * All modified related objects will also be persisted in the doSave()
     743:      * method.  This method wraps all precipitate database operations in a
     744:      * single transaction.
     745:      *
     746:      * @param PropelPDO $con
     747:      * @return int             The number of rows affected by this insert/update and any referring fk objects' save() operations.
     748:      * @throws PropelException
     749:      * @throws Exception
     750:      * @see        doSave()
     751:      */
     752:     public function save(PropelPDO $con = null)
     753:     {
     754:         if ($this->isDeleted()) {
     755:             throw new PropelException("You cannot save an object that has been deleted.");
     756:         }
     757:
     758:         if ($con === null) {
     759:             $con = Propel::getConnection(CouponPeer::DATABASE_NAME, Propel::CONNECTION_WRITE);
     760:         }
     761:
     762:         $con->beginTransaction();
     763:         $isInsert = $this->isNew();
     764:         try {
     765:             $ret = $this->preSave($con);
     766:             if ($isInsert) {
     767:                 $ret = $ret && $this->preInsert($con);
     768:                 // timestampable behavior
     769:                 if (!$this->isColumnModified(CouponPeer::CREATED_AT)) {
     770:                     $this->setCreatedAt(time());
     771:                 }
     772:                 if (!$this->isColumnModified(CouponPeer::UPDATED_AT)) {
     773:                     $this->setUpdatedAt(time());
     774:                 }
     775:             } else {
     776:                 $ret = $ret && $this->preUpdate($con);
     777:                 // timestampable behavior
     778:                 if ($this->isModified() && !$this->isColumnModified(CouponPeer::UPDATED_AT)) {
     779:                     $this->setUpdatedAt(time());
     780:                 }
     781:             }
     782:             if ($ret) {
     783:                 $affectedRows = $this->doSave($con);
     784:                 if ($isInsert) {
     785:                     $this->postInsert($con);
     786:                 } else {
     787:                     $this->postUpdate($con);
     788:                 }
     789:                 $this->postSave($con);
     790:                 CouponPeer::addInstanceToPool($this);
     791:             } else {
     792:                 $affectedRows = 0;
     793:             }
     794:             $con->commit();
     795:
     796:             return $affectedRows;
     797:         } catch (Exception $e) {
     798:             $con->rollBack();
     799:             throw $e;
     800:         }
     801:     }
     802:
     803:     /**
     804:      * Performs the work of inserting or updating the row in the database.
     805:      *
     806:      * If the object is new, it inserts it; otherwise an update is performed.
     807:      * All related objects are also updated in this method.
     808:      *
     809:      * @param PropelPDO $con
     810:      * @return int             The number of rows affected by this insert/update and any referring fk objects' save() operations.
     811:      * @throws PropelException
     812:      * @see        save()
     813:      */
     814:     protected function doSave(PropelPDO $con)
     815:     {
     816:         $affectedRows = 0; // initialize var to track total num of affected rows
     817:         if (!$this->alreadyInSave) {
     818:             $this->alreadyInSave = true;
     819:
     820:             if ($this->isNew() || $this->isModified()) {
     821:                 // persist changes
     822:                 if ($this->isNew()) {
     823:                     $this->doInsert($con);
     824:                 } else {
     825:                     $this->doUpdate($con);
     826:                 }
     827:                 $affectedRows += 1;
     828:                 $this->resetModified();
     829:             }
     830:
     831:             if ($this->couponRulesScheduledForDeletion !== null) {
     832:                 if (!$this->couponRulesScheduledForDeletion->isEmpty()) {
     833:                     CouponRuleQuery::create()
     834:                         ->filterByPrimaryKeys($this->couponRulesScheduledForDeletion->getPrimaryKeys(false))
     835:                         ->delete($con);
     836:                     $this->couponRulesScheduledForDeletion = null;
     837:                 }
     838:             }
     839:
     840:             if ($this->collCouponRules !== null) {
     841:                 foreach ($this->collCouponRules as $referrerFK) {
     842:                     if (!$referrerFK->isDeleted() && ($referrerFK->isNew() || $referrerFK->isModified())) {
     843:                         $affectedRows += $referrerFK->save($con);
     844:                     }
     845:                 }
     846:             }
     847:
     848:             $this->alreadyInSave = false;
     849:
     850:         }
     851:
     852:         return $affectedRows;
     853:     } // doSave()
     854:
     855:     /**
     856:      * Insert the row in the database.
     857:      *
     858:      * @param PropelPDO $con
     859:      *
     860:      * @throws PropelException
     861:      * @see        doSave()
     862:      */
     863:     protected function doInsert(PropelPDO $con)
     864:     {
     865:         $modifiedColumns = array();
     866:         $index = 0;
     867:
     868:         $this->modifiedColumns[] = CouponPeer::ID;
     869:         if (null !== $this->id) {
     870:             throw new PropelException('Cannot insert a value for auto-increment primary key (' . CouponPeer::ID . ')');
     871:         }
     872:
     873:          // check the columns in natural order for more readable SQL queries
     874:         if ($this->isColumnModified(CouponPeer::ID)) {
     875:             $modifiedColumns[':p' . $index++]  = '`id`';
     876:         }
     877:         if ($this->isColumnModified(CouponPeer::CODE)) {
     878:             $modifiedColumns[':p' . $index++]  = '`code`';
     879:         }
     880:         if ($this->isColumnModified(CouponPeer::ACTION)) {
     881:             $modifiedColumns[':p' . $index++]  = '`action`';
     882:         }
     883:         if ($this->isColumnModified(CouponPeer::VALUE)) {
     884:             $modifiedColumns[':p' . $index++]  = '`value`';
     885:         }
     886:         if ($this->isColumnModified(CouponPeer::USED)) {
     887:             $modifiedColumns[':p' . $index++]  = '`used`';
     888:         }
     889:         if ($this->isColumnModified(CouponPeer::AVAILABLE_SINCE)) {
     890:             $modifiedColumns[':p' . $index++]  = '`available_since`';
     891:         }
     892:         if ($this->isColumnModified(CouponPeer::DATE_LIMIT)) {
     893:             $modifiedColumns[':p' . $index++]  = '`date_limit`';
     894:         }
     895:         if ($this->isColumnModified(CouponPeer::ACTIVATE)) {
     896:             $modifiedColumns[':p' . $index++]  = '`activate`';
     897:         }
     898:         if ($this->isColumnModified(CouponPeer::CREATED_AT)) {
     899:             $modifiedColumns[':p' . $index++]  = '`created_at`';
     900:         }
     901:         if ($this->isColumnModified(CouponPeer::UPDATED_AT)) {
     902:             $modifiedColumns[':p' . $index++]  = '`updated_at`';
     903:         }
     904:
     905:         $sql = sprintf(
     906:             'INSERT INTO `coupon` (%s) VALUES (%s)',
     907:             implode(', ', $modifiedColumns),
     908:             implode(', ', array_keys($modifiedColumns))
     909:         );
     910:
     911:         try {
     912:             $stmt = $con->prepare($sql);
     913:             foreach ($modifiedColumns as $identifier => $columnName) {
     914:                 switch ($columnName) {
     915:                     case '`id`':
     916:                         $stmt->bindValue($identifier, $this->id, PDO::PARAM_INT);
     917:                         break;
     918:                     case '`code`':
     919:                         $stmt->bindValue($identifier, $this->code, PDO::PARAM_STR);
     920:                         break;
     921:                     case '`action`':
     922:                         $stmt->bindValue($identifier, $this->action, PDO::PARAM_STR);
     923:                         break;
     924:                     case '`value`':
     925:                         $stmt->bindValue($identifier, $this->value, PDO::PARAM_STR);
     926:                         break;
     927:                     case '`used`':
     928:                         $stmt->bindValue($identifier, $this->used, PDO::PARAM_INT);
     929:                         break;
     930:                     case '`available_since`':
     931:                         $stmt->bindValue($identifier, $this->available_since, PDO::PARAM_STR);
     932:                         break;
     933:                     case '`date_limit`':
     934:                         $stmt->bindValue($identifier, $this->date_limit, PDO::PARAM_STR);
     935:                         break;
     936:                     case '`activate`':
     937:                         $stmt->bindValue($identifier, $this->activate, PDO::PARAM_INT);
     938:                         break;
     939:                     case '`created_at`':
     940:                         $stmt->bindValue($identifier, $this->created_at, PDO::PARAM_STR);
     941:                         break;
     942:                     case '`updated_at`':
     943:                         $stmt->bindValue($identifier, $this->updated_at, PDO::PARAM_STR);
     944:                         break;
     945:                 }
     946:             }
     947:             $stmt->execute();
     948:         } catch (Exception $e) {
     949:             Propel::log($e->getMessage(), Propel::LOG_ERR);
     950:             throw new PropelException(sprintf('Unable to execute INSERT statement [%s]', $sql), $e);
     951:         }
     952:
     953:         try {
     954:             $pk = $con->lastInsertId();
     955:         } catch (Exception $e) {
     956:             throw new PropelException('Unable to get autoincrement id.', $e);
     957:         }
     958:         $this->setId($pk);
     959:
     960:         $this->setNew(false);
     961:     }
     962:
     963:     /**
     964:      * Update the row in the database.
     965:      *
     966:      * @param PropelPDO $con
     967:      *
     968:      * @see        doSave()
     969:      */
     970:     protected function doUpdate(PropelPDO $con)
     971:     {
     972:         $selectCriteria = $this->buildPkeyCriteria();
     973:         $valuesCriteria = $this->buildCriteria();
     974:         BasePeer::doUpdate($selectCriteria, $valuesCriteria, $con);
     975:     }
     976:
     977:     /**
     978:      * Array of ValidationFailed objects.
     979:      * @var        array ValidationFailed[]
     980:      */
     981:     protected $validationFailures = array();
     982:
     983:     /**
     984:      * Gets any ValidationFailed objects that resulted from last call to validate().
     985:      *
     986:      *
     987:      * @return array ValidationFailed[]
     988:      * @see        validate()
     989:      */
     990:     public function getValidationFailures()
     991:     {
     992:         return $this->validationFailures;
     993:     }
     994:
     995:     /**
     996:      * Validates the objects modified field values and all objects related to this table.
     997:      *
     998:      * If $columns is either a column name or an array of column names
     999:      * only those columns are validated.
    1000:      *
    1001:      * @param mixed $columns Column name or an array of column names.
    1002:      * @return boolean Whether all columns pass validation.
    1003:      * @see        doValidate()
    1004:      * @see        getValidationFailures()
    1005:      */
    1006:     public function validate($columns = null)
    1007:     {
    1008:         $res = $this->doValidate($columns);
    1009:         if ($res === true) {
    1010:             $this->validationFailures = array();
    1011:
    1012:             return true;
    1013:         }
    1014:
    1015:         $this->validationFailures = $res;
    1016:
    1017:         return false;
    1018:     }
    1019:
    1020:     /**
    1021:      * This function performs the validation work for complex object models.
    1022:      *
    1023:      * In addition to checking the current object, all related objects will
    1024:      * also be validated.  If all pass then <code>true</code> is returned; otherwise
    1025:      * an aggreagated array of ValidationFailed objects will be returned.
    1026:      *
    1027:      * @param array $columns Array of column names to validate.
    1028:      * @return mixed <code>true</code> if all validations pass; array of <code>ValidationFailed</code> objets otherwise.
    1029:      */
    1030:     protected function doValidate($columns = null)
    1031:     {
    1032:         if (!$this->alreadyInValidation) {
    1033:             $this->alreadyInValidation = true;
    1034:             $retval = null;
    1035:
    1036:             $failureMap = array();
    1037:
    1038:
    1039:             if (($retval = CouponPeer::doValidate($this, $columns)) !== true) {
    1040:                 $failureMap = array_merge($failureMap, $retval);
    1041:             }
    1042:
    1043:
    1044:                 if ($this->collCouponRules !== null) {
    1045:                     foreach ($this->collCouponRules as $referrerFK) {
    1046:                         if (!$referrerFK->validate($columns)) {
    1047:                             $failureMap = array_merge($failureMap, $referrerFK->getValidationFailures());
    1048:                         }
    1049:                     }
    1050:                 }
    1051:
    1052:
    1053:             $this->alreadyInValidation = false;
    1054:         }
    1055:
    1056:         return (!empty($failureMap) ? $failureMap : true);
    1057:     }
    1058:
    1059:     /**
    1060:      * Retrieves a field from the object by name passed in as a string.
    1061:      *
    1062:      * @param string $name name
    1063:      * @param string $type The type of fieldname the $name is of:
    1064:      *               one of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME
    1065:      *               BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM.
    1066:      *               Defaults to BasePeer::TYPE_PHPNAME
    1067:      * @return mixed Value of field.
    1068:      */
    1069:     public function getByName($name, $type = BasePeer::TYPE_PHPNAME)
    1070:     {
    1071:         $pos = CouponPeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
    1072:         $field = $this->getByPosition($pos);
    1073:
    1074:         return $field;
    1075:     }
    1076:
    1077:     /**
    1078:      * Retrieves a field from the object by Position as specified in the xml schema.
    1079:      * Zero-based.
    1080:      *
    1081:      * @param int $pos position in xml schema
    1082:      * @return mixed Value of field at $pos
    1083:      */
    1084:     public function getByPosition($pos)
    1085:     {
    1086:         switch ($pos) {
    1087:             case 0:
    1088:                 return $this->getId();
    1089:                 break;
    1090:             case 1:
    1091:                 return $this->getCode();
    1092:                 break;
    1093:             case 2:
    1094:                 return $this->getAction();
    1095:                 break;
    1096:             case 3:
    1097:                 return $this->getValue();
    1098:                 break;
    1099:             case 4:
    1100:                 return $this->getUsed();
    1101:                 break;
    1102:             case 5:
    1103:                 return $this->getAvailableSince();
    1104:                 break;
    1105:             case 6:
    1106:                 return $this->getDateLimit();
    1107:                 break;
    1108:             case 7:
    1109:                 return $this->getActivate();
    1110:                 break;
    1111:             case 8:
    1112:                 return $this->getCreatedAt();
    1113:                 break;
    1114:             case 9:
    1115:                 return $this->getUpdatedAt();
    1116:                 break;
    1117:             default:
    1118:                 return null;
    1119:                 break;
    1120:         } // switch()
    1121:     }
    1122:
    1123:     /**
    1124:      * Exports the object as an array.
    1125:      *
    1126:      * You can specify the key type of the array by passing one of the class
    1127:      * type constants.
    1128:      *
    1129:      * @param     string  $keyType (optional) One of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME,
    1130:      *                    BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM.
    1131:      *                    Defaults to BasePeer::TYPE_PHPNAME.
    1132:      * @param     boolean $includeLazyLoadColumns (optional) Whether to include lazy loaded columns. Defaults to true.
    1133:      * @param     array $alreadyDumpedObjects List of objects to skip to avoid recursion
    1134:      * @param     boolean $includeForeignObjects (optional) Whether to include hydrated related objects. Default to FALSE.
    1135:      *
    1136:      * @return array an associative array containing the field names (as keys) and field values
    1137:      */
    1138:     public function toArray($keyType = BasePeer::TYPE_PHPNAME, $includeLazyLoadColumns = true, $alreadyDumpedObjects = array(), $includeForeignObjects = false)
    1139:     {
    1140:         if (isset($alreadyDumpedObjects['Coupon'][$this->getPrimaryKey()])) {
    1141:             return '*RECURSION*';
    1142:         }
    1143:         $alreadyDumpedObjects['Coupon'][$this->getPrimaryKey()] = true;
    1144:         $keys = CouponPeer::getFieldNames($keyType);
    1145:         $result = array(
    1146:             $keys[0] => $this->getId(),
    1147:             $keys[1] => $this->getCode(),
    1148:             $keys[2] => $this->getAction(),
    1149:             $keys[3] => $this->getValue(),
    1150:             $keys[4] => $this->getUsed(),
    1151:             $keys[5] => $this->getAvailableSince(),
    1152:             $keys[6] => $this->getDateLimit(),
    1153:             $keys[7] => $this->getActivate(),
    1154:             $keys[8] => $this->getCreatedAt(),
    1155:             $keys[9] => $this->getUpdatedAt(),
    1156:         );
    1157:         if ($includeForeignObjects) {
    1158:             if (null !== $this->collCouponRules) {
    1159:                 $result['CouponRules'] = $this->collCouponRules->toArray(null, true, $keyType, $includeLazyLoadColumns, $alreadyDumpedObjects);
    1160:             }
    1161:         }
    1162:
    1163:         return $result;
    1164:     }
    1165:
    1166:     /**
    1167:      * Sets a field from the object by name passed in as a string.
    1168:      *
    1169:      * @param string $name peer name
    1170:      * @param mixed $value field value
    1171:      * @param string $type The type of fieldname the $name is of:
    1172:      *                     one of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME
    1173:      *                     BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM.
    1174:      *                     Defaults to BasePeer::TYPE_PHPNAME
    1175:      * @return void
    1176:      */
    1177:     public function setByName($name, $value, $type = BasePeer::TYPE_PHPNAME)
    1178:     {
    1179:         $pos = CouponPeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
    1180:
    1181:         $this->setByPosition($pos, $value);
    1182:     }
    1183:
    1184:     /**
    1185:      * Sets a field from the object by Position as specified in the xml schema.
    1186:      * Zero-based.
    1187:      *
    1188:      * @param int $pos position in xml schema
    1189:      * @param mixed $value field value
    1190:      * @return void
    1191:      */
    1192:     public function setByPosition($pos, $value)
    1193:     {
    1194:         switch ($pos) {
    1195:             case 0:
    1196:                 $this->setId($value);
    1197:                 break;
    1198:             case 1:
    1199:                 $this->setCode($value);
    1200:                 break;
    1201:             case 2:
    1202:                 $this->setAction($value);
    1203:                 break;
    1204:             case 3:
    1205:                 $this->setValue($value);
    1206:                 break;
    1207:             case 4:
    1208:                 $this->setUsed($value);
    1209:                 break;
    1210:             case 5:
    1211:                 $this->setAvailableSince($value);
    1212:                 break;
    1213:             case 6:
    1214:                 $this->setDateLimit($value);
    1215:                 break;
    1216:             case 7:
    1217:                 $this->setActivate($value);
    1218:                 break;
    1219:             case 8:
    1220:                 $this->setCreatedAt($value);
    1221:                 break;
    1222:             case 9:
    1223:                 $this->setUpdatedAt($value);
    1224:                 break;
    1225:         } // switch()
    1226:     }
    1227:
    1228:     /**
    1229:      * Populates the object using an array.
    1230:      *
    1231:      * This is particularly useful when populating an object from one of the
    1232:      * request arrays (e.g. $_POST).  This method goes through the column
    1233:      * names, checking to see whether a matching key exists in populated
    1234:      * array. If so the setByName() method is called for that column.
    1235:      *
    1236:      * You can specify the key type of the array by additionally passing one
    1237:      * of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME,
    1238:      * BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM.
    1239:      * The default key type is the column's BasePeer::TYPE_PHPNAME
    1240:      *
    1241:      * @param array  $arr     An array to populate the object from.
    1242:      * @param string $keyType The type of keys the array uses.
    1243:      * @return void
    1244:      */
    1245:     public function fromArray($arr, $keyType = BasePeer::TYPE_PHPNAME)
    1246:     {
    1247:         $keys = CouponPeer::getFieldNames($keyType);
    1248:
    1249:         if (array_key_exists($keys[0], $arr)) $this->setId($arr[$keys[0]]);
    1250:         if (array_key_exists($keys[1], $arr)) $this->setCode($arr[$keys[1]]);
    1251:         if (array_key_exists($keys[2], $arr)) $this->setAction($arr[$keys[2]]);
    1252:         if (array_key_exists($keys[3], $arr)) $this->setValue($arr[$keys[3]]);
    1253:         if (array_key_exists($keys[4], $arr)) $this->setUsed($arr[$keys[4]]);
    1254:         if (array_key_exists($keys[5], $arr)) $this->setAvailableSince($arr[$keys[5]]);
    1255:         if (array_key_exists($keys[6], $arr)) $this->setDateLimit($arr[$keys[6]]);
    1256:         if (array_key_exists($keys[7], $arr)) $this->setActivate($arr[$keys[7]]);
    1257:         if (array_key_exists($keys[8], $arr)) $this->setCreatedAt($arr[$keys[8]]);
    1258:         if (array_key_exists($keys[9], $arr)) $this->setUpdatedAt($arr[$keys[9]]);
    1259:     }
    1260:
    1261:     /**
    1262:      * Build a Criteria object containing the values of all modified columns in this object.
    1263:      *
    1264:      * @return Criteria The Criteria object containing all modified values.
    1265:      */
    1266:     public function buildCriteria()
    1267:     {
    1268:         $criteria = new Criteria(CouponPeer::DATABASE_NAME);
    1269:
    1270:         if ($this->isColumnModified(CouponPeer::ID)) $criteria->add(CouponPeer::ID, $this->id);
    1271:         if ($this->isColumnModified(CouponPeer::CODE)) $criteria->add(CouponPeer::CODE, $this->code);
    1272:         if ($this->isColumnModified(CouponPeer::ACTION)) $criteria->add(CouponPeer::ACTION, $this->action);
    1273:         if ($this->isColumnModified(CouponPeer::VALUE)) $criteria->add(CouponPeer::VALUE, $this->value);
    1274:         if ($this->isColumnModified(CouponPeer::USED)) $criteria->add(CouponPeer::USED, $this->used);
    1275:         if ($this->isColumnModified(CouponPeer::AVAILABLE_SINCE)) $criteria->add(CouponPeer::AVAILABLE_SINCE, $this->available_since);
    1276:         if ($this->isColumnModified(CouponPeer::DATE_LIMIT)) $criteria->add(CouponPeer::DATE_LIMIT, $this->date_limit);
    1277:         if ($this->isColumnModified(CouponPeer::ACTIVATE)) $criteria->add(CouponPeer::ACTIVATE, $this->activate);
    1278:         if ($this->isColumnModified(CouponPeer::CREATED_AT)) $criteria->add(CouponPeer::CREATED_AT, $this->created_at);
    1279:         if ($this->isColumnModified(CouponPeer::UPDATED_AT)) $criteria->add(CouponPeer::UPDATED_AT, $this->updated_at);
    1280:
    1281:         return $criteria;
    1282:     }
    1283:
    1284:     /**
    1285:      * Builds a Criteria object containing the primary key for this object.
    1286:      *
    1287:      * Unlike buildCriteria() this method includes the primary key values regardless
    1288:      * of whether or not they have been modified.
    1289:      *
    1290:      * @return Criteria The Criteria object containing value(s) for primary key(s).
    1291:      */
    1292:     public function buildPkeyCriteria()
    1293:     {
    1294:         $criteria = new Criteria(CouponPeer::DATABASE_NAME);
    1295:         $criteria->add(CouponPeer::ID, $this->id);
    1296:
    1297:         return $criteria;
    1298:     }
    1299:
    1300:     /**
    1301:      * Returns the primary key for this object (row).
    1302:      * @return int
    1303:      */
    1304:     public function getPrimaryKey()
    1305:     {
    1306:         return $this->getId();
    1307:     }
    1308:
    1309:     /**
    1310:      * Generic method to set the primary key (id column).
    1311:      *
    1312:      * @param  int $key Primary key.
    1313:      * @return void
    1314:      */
    1315:     public function setPrimaryKey($key)
    1316:     {
    1317:         $this->setId($key);
    1318:     }
    1319:
    1320:     /**
    1321:      * Returns true if the primary key for this object is null.
    1322:      * @return boolean
    1323:      */
    1324:     public function isPrimaryKeyNull()
    1325:     {
    1326:
    1327:         return null === $this->getId();
    1328:     }
    1329:
    1330:     /**
    1331:      * Sets contents of passed object to values from current object.
    1332:      *
    1333:      * If desired, this method can also make copies of all associated (fkey referrers)
    1334:      * objects.
    1335:      *
    1336:      * @param object $copyObj An object of Coupon (or compatible) type.
    1337:      * @param boolean $deepCopy Whether to also copy all rows that refer (by fkey) to the current row.
    1338:      * @param boolean $makeNew Whether to reset autoincrement PKs and make the object new.
    1339:      * @throws PropelException
    1340:      */
    1341:     public function copyInto($copyObj, $deepCopy = false, $makeNew = true)
    1342:     {
    1343:         $copyObj->setCode($this->getCode());
    1344:         $copyObj->setAction($this->getAction());
    1345:         $copyObj->setValue($this->getValue());
    1346:         $copyObj->setUsed($this->getUsed());
    1347:         $copyObj->setAvailableSince($this->getAvailableSince());
    1348:         $copyObj->setDateLimit($this->getDateLimit());
    1349:         $copyObj->setActivate($this->getActivate());
    1350:         $copyObj->setCreatedAt($this->getCreatedAt());
    1351:         $copyObj->setUpdatedAt($this->getUpdatedAt());
    1352:
    1353:         if ($deepCopy && !$this->startCopy) {
    1354:             // important: temporarily setNew(false) because this affects the behavior of
    1355:             // the getter/setter methods for fkey referrer objects.
    1356:             $copyObj->setNew(false);
    1357:             // store object hash to prevent cycle
    1358:             $this->startCopy = true;
    1359:
    1360:             foreach ($this->getCouponRules() as $relObj) {
    1361:                 if ($relObj !== $this) {  // ensure that we don't try to copy a reference to ourselves
    1362:                     $copyObj->addCouponRule($relObj->copy($deepCopy));
    1363:                 }
    1364:             }
    1365:
    1366:             //unflag object copy
    1367:             $this->startCopy = false;
    1368:         } // if ($deepCopy)
    1369:
    1370:         if ($makeNew) {
    1371:             $copyObj->setNew(true);
    1372:             $copyObj->setId(NULL); // this is a auto-increment column, so set to default value
    1373:         }
    1374:     }
    1375:
    1376:     /**
    1377:      * Makes a copy of this object that will be inserted as a new row in table when saved.
    1378:      * It creates a new object filling in the simple attributes, but skipping any primary
    1379:      * keys that are defined for the table.
    1380:      *
    1381:      * If desired, this method can also make copies of all associated (fkey referrers)
    1382:      * objects.
    1383:      *
    1384:      * @param boolean $deepCopy Whether to also copy all rows that refer (by fkey) to the current row.
    1385:      * @return Coupon Clone of current object.
    1386:      * @throws PropelException
    1387:      */
    1388:     public function copy($deepCopy = false)
    1389:     {
    1390:         // we use get_class(), because this might be a subclass
    1391:         $clazz = get_class($this);
    1392:         $copyObj = new $clazz();
    1393:         $this->copyInto($copyObj, $deepCopy);
    1394:
    1395:         return $copyObj;
    1396:     }
    1397:
    1398:     /**
    1399:      * Returns a peer instance associated with this om.
    1400:      *
    1401:      * Since Peer classes are not to have any instance attributes, this method returns the
    1402:      * same instance for all member of this class. The method could therefore
    1403:      * be static, but this would prevent one from overriding the behavior.
    1404:      *
    1405:      * @return CouponPeer
    1406:      */
    1407:     public function getPeer()
    1408:     {
    1409:         if (self::$peer === null) {
    1410:             self::$peer = new CouponPeer();
    1411:         }
    1412:
    1413:         return self::$peer;
    1414:     }
    1415:
    1416:
    1417:     /**
    1418:      * Initializes a collection based on the name of a relation.
    1419:      * Avoids crafting an 'init[$relationName]s' method name
    1420:      * that wouldn't work when StandardEnglishPluralizer is used.
    1421:      *
    1422:      * @param string $relationName The name of the relation to initialize
    1423:      * @return void
    1424:      */
    1425:     public function initRelation($relationName)
    1426:     {
    1427:         if ('CouponRule' == $relationName) {
    1428:             $this->initCouponRules();
    1429:         }
    1430:     }
    1431:
    1432:     /**
    1433:      * Clears out the collCouponRules collection
    1434:      *
    1435:      * This does not modify the database; however, it will remove any associated objects, causing
    1436:      * them to be refetched by subsequent calls to accessor method.
    1437:      *
    1438:      * @return Coupon The current object (for fluent API support)
    1439:      * @see        addCouponRules()
    1440:      */
    1441:     public function clearCouponRules()
    1442:     {
    1443:         $this->collCouponRules = null; // important to set this to null since that means it is uninitialized
    1444:         $this->collCouponRulesPartial = null;
    1445:
    1446:         return $this;
    1447:     }
    1448:
    1449:     /**
    1450:      * reset is the collCouponRules collection loaded partially
    1451:      *
    1452:      * @return void
    1453:      */
    1454:     public function resetPartialCouponRules($v = true)
    1455:     {
    1456:         $this->collCouponRulesPartial = $v;
    1457:     }
    1458:
    1459:     /**
    1460:      * Initializes the collCouponRules collection.
    1461:      *
    1462:      * By default this just sets the collCouponRules collection to an empty array (like clearcollCouponRules());
    1463:      * however, you may wish to override this method in your stub class to provide setting appropriate
    1464:      * to your application -- for example, setting the initial array to the values stored in database.
    1465:      *
    1466:      * @param boolean $overrideExisting If set to true, the method call initializes
    1467:      *                                        the collection even if it is not empty
    1468:      *
    1469:      * @return void
    1470:      */
    1471:     public function initCouponRules($overrideExisting = true)
    1472:     {
    1473:         if (null !== $this->collCouponRules && !$overrideExisting) {
    1474:             return;
    1475:         }
    1476:         $this->collCouponRules = new PropelObjectCollection();
    1477:         $this->collCouponRules->setModel('CouponRule');
    1478:     }
    1479:
    1480:     /**
    1481:      * Gets an array of CouponRule objects which contain a foreign key that references this object.
    1482:      *
    1483:      * If the $criteria is not null, it is used to always fetch the results from the database.
    1484:      * Otherwise the results are fetched from the database the first time, then cached.
    1485:      * Next time the same method is called without $criteria, the cached collection is returned.
    1486:      * If this Coupon is new, it will return
    1487:      * an empty collection or the current collection; the criteria is ignored on a new object.
    1488:      *
    1489:      * @param Criteria $criteria optional Criteria object to narrow the query
    1490:      * @param PropelPDO $con optional connection object
    1491:      * @return PropelObjectCollection|CouponRule[] List of CouponRule objects
    1492:      * @throws PropelException
    1493:      */
    1494:     public function getCouponRules($criteria = null, PropelPDO $con = null)
    1495:     {
    1496:         $partial = $this->collCouponRulesPartial && !$this->isNew();
    1497:         if (null === $this->collCouponRules || null !== $criteria  || $partial) {
    1498:             if ($this->isNew() && null === $this->collCouponRules) {
    1499:                 // return empty collection
    1500:                 $this->initCouponRules();
    1501:             } else {
    1502:                 $collCouponRules = CouponRuleQuery::create(null, $criteria)
    1503:                     ->filterByCoupon($this)
    1504:                     ->find($con);
    1505:                 if (null !== $criteria) {
    1506:                     if (false !== $this->collCouponRulesPartial && count($collCouponRules)) {
    1507:                       $this->initCouponRules(false);
    1508:
    1509:                       foreach($collCouponRules as $obj) {
    1510:                         if (false == $this->collCouponRules->contains($obj)) {
    1511:                           $this->collCouponRules->append($obj);
    1512:                         }
    1513:                       }
    1514:
    1515:                       $this->collCouponRulesPartial = true;
    1516:                     }
    1517:
    1518:                     $collCouponRules->getInternalIterator()->rewind();
    1519:                     return $collCouponRules;
    1520:                 }
    1521:
    1522:                 if($partial && $this->collCouponRules) {
    1523:                     foreach($this->collCouponRules as $obj) {
    1524:                         if($obj->isNew()) {
    1525:                             $collCouponRules[] = $obj;
    1526:                         }
    1527:                     }
    1528:                 }
    1529:
    1530:                 $this->collCouponRules = $collCouponRules;
    1531:                 $this->collCouponRulesPartial = false;
    1532:             }
    1533:         }
    1534:
    1535:         return $this->collCouponRules;
    1536:     }
    1537:
    1538:     /**
    1539:      * Sets a collection of CouponRule objects related by a one-to-many relationship
    1540:      * to the current object.
    1541:      * It will also schedule objects for deletion based on a diff between old objects (aka persisted)
    1542:      * and new objects from the given Propel collection.
    1543:      *
    1544:      * @param PropelCollection $couponRules A Propel collection.
    1545:      * @param PropelPDO $con Optional connection object
    1546:      * @return Coupon The current object (for fluent API support)
    1547:      */
    1548:     public function setCouponRules(PropelCollection $couponRules, PropelPDO $con = null)
    1549:     {
    1550:         $couponRulesToDelete = $this->getCouponRules(new Criteria(), $con)->diff($couponRules);
    1551:
    1552:         $this->couponRulesScheduledForDeletion = unserialize(serialize($couponRulesToDelete));
    1553:
    1554:         foreach ($couponRulesToDelete as $couponRuleRemoved) {
    1555:             $couponRuleRemoved->setCoupon(null);
    1556:         }
    1557:
    1558:         $this->collCouponRules = null;
    1559:         foreach ($couponRules as $couponRule) {
    1560:             $this->addCouponRule($couponRule);
    1561:         }
    1562:
    1563:         $this->collCouponRules = $couponRules;
    1564:         $this->collCouponRulesPartial = false;
    1565:
    1566:         return $this;
    1567:     }
    1568:
    1569:     /**
    1570:      * Returns the number of related CouponRule objects.
    1571:      *
    1572:      * @param Criteria $criteria
    1573:      * @param boolean $distinct
    1574:      * @param PropelPDO $con
    1575:      * @return int             Count of related CouponRule objects.
    1576:      * @throws PropelException
    1577:      */
    1578:     public function countCouponRules(Criteria $criteria = null, $distinct = false, PropelPDO $con = null)
    1579:     {
    1580:         $partial = $this->collCouponRulesPartial && !$this->isNew();
    1581:         if (null === $this->collCouponRules || null !== $criteria || $partial) {
    1582:             if ($this->isNew() && null === $this->collCouponRules) {
    1583:                 return 0;
    1584:             }
    1585:
    1586:             if($partial && !$criteria) {
    1587:                 return count($this->getCouponRules());
    1588:             }
    1589:             $query = CouponRuleQuery::create(null, $criteria);
    1590:             if ($distinct) {
    1591:                 $query->distinct();
    1592:             }
    1593:
    1594:             return $query
    1595:                 ->filterByCoupon($this)
    1596:                 ->count($con);
    1597:         }
    1598:
    1599:         return count($this->collCouponRules);
    1600:     }
    1601:
    1602:     /**
    1603:      * Method called to associate a CouponRule object to this object
    1604:      * through the CouponRule foreign key attribute.
    1605:      *
    1606:      * @param    CouponRule $l CouponRule
    1607:      * @return Coupon The current object (for fluent API support)
    1608:      */
    1609:     public function addCouponRule(CouponRule $l)
    1610:     {
    1611:         if ($this->collCouponRules === null) {
    1612:             $this->initCouponRules();
    1613:             $this->collCouponRulesPartial = true;
    1614:         }
    1615:         if (!in_array($l, $this->collCouponRules->getArrayCopy(), true)) { // only add it if the **same** object is not already associated
    1616:             $this->doAddCouponRule($l);
    1617:         }
    1618:
    1619:         return $this;
    1620:     }
    1621:
    1622:     /**
    1623:      * @param   CouponRule $couponRule The couponRule object to add.
    1624:      */
    1625:     protected function doAddCouponRule($couponRule)
    1626:     {
    1627:         $this->collCouponRules[]= $couponRule;
    1628:         $couponRule->setCoupon($this);
    1629:     }
    1630:
    1631:     /**
    1632:      * @param   CouponRule $couponRule The couponRule object to remove.
    1633:      * @return Coupon The current object (for fluent API support)
    1634:      */
    1635:     public function removeCouponRule($couponRule)
    1636:     {
    1637:         if ($this->getCouponRules()->contains($couponRule)) {
    1638:             $this->collCouponRules->remove($this->collCouponRules->search($couponRule));
    1639:             if (null === $this->couponRulesScheduledForDeletion) {
    1640:                 $this->couponRulesScheduledForDeletion = clone $this->collCouponRules;
    1641:                 $this->couponRulesScheduledForDeletion->clear();
    1642:             }
    1643:             $this->couponRulesScheduledForDeletion[]= clone $couponRule;
    1644:             $couponRule->setCoupon(null);
    1645:         }
    1646:
    1647:         return $this;
    1648:     }
    1649:
    1650:     /**
    1651:      * Clears the current object and sets all attributes to their default values
    1652:      */
    1653:     public function clear()
    1654:     {
    1655:         $this->id = null;
    1656:         $this->code = null;
    1657:         $this->action = null;
    1658:         $this->value = null;
    1659:         $this->used = null;
    1660:         $this->available_since = null;
    1661:         $this->date_limit = null;
    1662:         $this->activate = null;
    1663:         $this->created_at = null;
    1664:         $this->updated_at = null;
    1665:         $this->alreadyInSave = false;
    1666:         $this->alreadyInValidation = false;
    1667:         $this->alreadyInClearAllReferencesDeep = false;
    1668:         $this->clearAllReferences();
    1669:         $this->resetModified();
    1670:         $this->setNew(true);
    1671:         $this->setDeleted(false);
    1672:     }
    1673:
    1674:     /**
    1675:      * Resets all references to other model objects or collections of model objects.
    1676:      *
    1677:      * This method is a user-space workaround for PHP's inability to garbage collect
    1678:      * objects with circular references (even in PHP 5.3). This is currently necessary
    1679:      * when using Propel in certain daemon or large-volumne/high-memory operations.
    1680:      *
    1681:      * @param boolean $deep Whether to also clear the references on all referrer objects.
    1682:      */
    1683:     public function clearAllReferences($deep = false)
    1684:     {
    1685:         if ($deep && !$this->alreadyInClearAllReferencesDeep) {
    1686:             $this->alreadyInClearAllReferencesDeep = true;
    1687:             if ($this->collCouponRules) {
    1688:                 foreach ($this->collCouponRules as $o) {
    1689:                     $o->clearAllReferences($deep);
    1690:                 }
    1691:             }
    1692:
    1693:             $this->alreadyInClearAllReferencesDeep = false;
    1694:         } // if ($deep)
    1695:
    1696:         if ($this->collCouponRules instanceof PropelCollection) {
    1697:             $this->collCouponRules->clearIterator();
    1698:         }
    1699:         $this->collCouponRules = null;
    1700:     }
    1701:
    1702:     /**
    1703:      * return the string representation of this object
    1704:      *
    1705:      * @return string
    1706:      */
    1707:     public function __toString()
    1708:     {
    1709:         return (string) $this->exportTo(CouponPeer::DEFAULT_STRING_FORMAT);
    1710:     }
    1711:
    1712:     /**
    1713:      * return true is the object is in saving state
    1714:      *
    1715:      * @return boolean
    1716:      */
    1717:     public function isAlreadyInSave()
    1718:     {
    1719:         return $this->alreadyInSave;
    1720:     }
    1721:
    1722:     // timestampable behavior
    1723:
    1724:     /**
    1725:      * Mark the current object so that the update date doesn't get updated during next save
    1726:      *
    1727:      * @return     Coupon The current object (for fluent API support)
    1728:      */
    1729:     public function keepUpdateDateUnchanged()
    1730:     {
    1731:         $this->modifiedColumns[] = CouponPeer::UPDATED_AT;
    1732:
    1733:         return $this;
    1734:     }
    1735:
    1736: }
    1737: