Doctrine_Record::isValid PHP Метод

isValid() публичный Метод

tests validity of the record using the current data.
public isValid ( boolean $deep = false, boolean $hooks = true ) : boolean
$deep boolean run the validation process on the relations
$hooks boolean invoke save hooks before start
Результат boolean whether or not this record is valid
    public function isValid($deep = false, $hooks = true)
    {
        if (!$this->_table->getAttribute(Doctrine_Core::ATTR_VALIDATE)) {
            return true;
        }
        if ($this->_state == self::STATE_LOCKED || $this->_state == self::STATE_TLOCKED) {
            return true;
        }
        if ($hooks) {
            $this->invokeSaveHooks('pre', 'save');
            $this->invokeSaveHooks('pre', $this->exists() ? 'update' : 'insert');
        }
        // Clear the stack from any previous errors.
        $this->getErrorStack()->clear();
        // Run validation process
        $event = new Doctrine_Event($this, Doctrine_Event::RECORD_VALIDATE);
        $this->preValidate($event);
        $this->getTable()->getRecordListener()->preValidate($event);
        if (!$event->skipOperation) {
            $validator = new Doctrine_Validator();
            $validator->validateRecord($this);
            $this->validate();
            if ($this->_state == self::STATE_TDIRTY || $this->_state == self::STATE_TCLEAN) {
                $this->validateOnInsert();
            } else {
                $this->validateOnUpdate();
            }
        }
        $this->getTable()->getRecordListener()->postValidate($event);
        $this->postValidate($event);
        $valid = $this->getErrorStack()->count() == 0 ? true : false;
        if ($valid && $deep) {
            $stateBeforeLock = $this->_state;
            $this->_state = $this->exists() ? self::STATE_LOCKED : self::STATE_TLOCKED;
            foreach ($this->_references as $reference) {
                if ($reference instanceof Doctrine_Record) {
                    if (!($valid = $reference->isValid($deep))) {
                        break;
                    }
                } else {
                    if ($reference instanceof Doctrine_Collection) {
                        foreach ($reference as $record) {
                            if (!($valid = $record->isValid($deep))) {
                                break;
                            }
                        }
                    }
                }
            }
            $this->_state = $stateBeforeLock;
        }
        return $valid;
    }

Usage Example

Пример #1
0
 /**
  * Inserts a record into database.
  *
  * This method inserts a transient record in the database, and adds it
  * to the identity map of its correspondent table. It proxies to @see 
  * processSingleInsert(), trigger insert hooks and validation of data
  * if required.
  *
  * @param Doctrine_Record $record   
  * @return boolean                  false if record is not valid
  */
 public function insert(Doctrine_Record $record)
 {
     $event = $record->invokeSaveHooks('pre', 'insert');
     if ($record->isValid(false, false)) {
         $table = $record->getTable();
         if (!$event->skipOperation) {
             if ($table->getOption('joinedParents')) {
                 // just for bc!
                 $this->_insertCTIRecord($table, $record);
                 //--
             } else {
                 $this->processSingleInsert($record);
             }
         }
         $table->addRecord($record);
         $record->invokeSaveHooks('post', 'insert', $event);
         return true;
     }
     return false;
 }
All Usage Examples Of Doctrine_Record::isValid