예제 #1
0
 /**
  * @param mixed $arg1
  * @param mixed $arg2
  *
  * @return int
  */
 public function delete($arg1 = null, $arg2 = null)
 {
     $result = null;
     if ($this->hasMethod("beforeDelete")) {
         $result = $this->beforeDelete("delete");
     }
     if ($result === null) {
         $condition = $this->getCondition();
         if (!$this->isSelected() && $arg1 === null && $condition->isEmpty()) {
             $stmt = $this->prepareStatement(Sabel_Db_Statement::DELETE);
             $result = $this->prepareDelete($stmt)->execute();
         } else {
             if ($arg1 !== null) {
                 $this->setCondition($arg1, $arg2);
             } elseif ($this->isSelected()) {
                 if (($pkey = $this->metadata->getPrimaryKey()) === null) {
                     $message = __METHOD__ . "() cannot delete model(there is not primary key).";
                     throw new Sabel_Db_Exception($message);
                 } else {
                     foreach (is_string($pkey) ? array($pkey) : $pkey as $key) {
                         $this->setCondition($this->modelName . "." . $key, $this->__get($key));
                     }
                 }
             }
             $stmt = $this->prepareStatement(Sabel_Db_Statement::DELETE);
             $result = $this->prepareDelete($stmt)->execute();
         }
     }
     if ($this->hasMethod("afterDelete")) {
         $afterResult = $this->afterDelete($result, "delete");
         if ($afterResult !== null) {
             $result = $afterResult;
         }
     }
     if ($this->autoReinit) {
         $this->initState();
     }
     return $result;
 }