2013-04-25 1 views
3

열이 설정되지 않은 경우 Propel ORM이 빈 문자열을 삽입하지 못하게하려면 어떻게해야합니까?Propel이 빈 문자열을 삽입하지 못하도록합니다.

CREATE TABLE user ( 
    uid INTEGER PRIMARY KEY AUTO_INCREMENT, 
    email VARCHAR(255) NOT NULL UNIQUE, -- No default value 
    ... 
) Engine InnoDB ... ; 

Propel은 $user = new User(); $user->save();을 허용합니다. SQL_MODE 설정을 시도했지만 도움이되지 않습니다.

+0

NULL 값을 가져올 열은 무엇입니까? – likeitlikeit

+0

아니요 열 :-) 그게 핵심입니다. 이 경우에'email' 칼럼이 명시 적으로 설정되어 있지 않다면 Propel에 의한 INSERT 질의 executen의 일부가되어서는 안된다. – vestol

+0

SQL 제약 조건 위반으로 이어질 것이라는 점을 알고 있습니까? – likeitlikeit

답변

2

올바른 방법은 스키마의 유효성 검사기를 사용하고 코드에서 validate() 메서드를 사용하여 검사하는 것입니다.

class User extends BaseUser { 
    ... 
    public function preSave(PropelPDO $con = null) { 
    // does the object pass all validations? 
    if (!$this->validate()) { 
     $errors = array(); 
     // something failed, go through each failure and capture message: 
     foreach ($this->getValidationFailures() as $failure) { 
     $errors[] = $failure->getMessage(); 
     } 
     // throwing an Exception will stop the save() from occurring 
     throw new InvalidArgumentException(implode("||", $errors)); 
    } 

    return true; // if you get here, go ahead and save 
    } 
} 

스크립트에서 당신과 같이 save()를 부를 것이다 : :

... 
$user = new User(); 
try { 
    // try to save (could fail) 
    $user->save(); 

} catch (InvalidArgumentException $e) { 
    // we have errors, split the exception message to get each one separately 
    $errorMessages = preg_split(/\|\|/, $e->getMessage()); 
    // handle the messages however you need to 
} 

에 대한 추가 읽기

<database ...> 
    <table ...> 
    <!-- the "required" attribute here only sets the DB property --> 
    <column name="email" type="varchar" required="true" /> 
    ... 
    <!-- Adds the unique index in the DB (but nothing in PHP code!) --> 
    <unique> 
     <unique-column name="email" /> 
    </Unique> 
    ... 
    <validator column="email"> 
     <!-- this validator rule makes the $obj->validate() method fail on null --> 
     <rule name="required" message="The email is required!" /> 
     <!-- this validator rule makes the $obj->validate() method fail on empty string --> 
     <rule name="minLength" value="1" message="The email cannot be blank!" /> 
     <!-- you could add a regular expression to only match email addresses here --> 
     <rule name="match" value="/regular expression/" message="Please enter a valid email address!" /> 
     <!-- adds a validation that the field is unique before trying to update DB --> 
     <rule name="unique" message="That email address is not unique!" /> 
    </validator> 
    </table> 
</database> 

이 그런 다음 preSave() 코드에서이 같은 일을 할 수 있습니다 : 다음은 그 예이다 Validators in the Propel documentation.

0

email 열이 설정되어 있지 않으면 실제로 삽입/업데이트를 중지하고 싶다고 생각합니다. 이를 수행하기위한 적절한 방법이 실제로 있으며 hooks을 사용합니다.

는 예를 들어, 다음 코드를 참조하십시오 :

class User extends BaseUser 
{ 
    // rest of code ... 

    public function preSave(PropelPDO $con = null) 
    { 
    if (empty($this->getEmail)) { 
     return false; 
    } 
    return true; 
    } 
} 

당신은 또한 데이터의 유효성을 검사하는 경우에 대한 자세한 제어를 위해 preInsert() 또는 preUpdate()를 사용할 수 있습니다.

+1

사실 그것은 행동이 아니며,'save' 메소드에 대한 후크입니다. 비헤이비어는'Base *'클래스 및/또는 테이블 (보통)에 코드를 추가하고 스키마의''태그에 의해 정의됩니다. 즉, 이것은 효과가있을 것이라고 말했다. – jakerella

+0

네 말이 맞아. 헤드 업에 감사 드리며 답변을 업데이트했습니다. – likeitlikeit

관련 문제