有人可以告诉我如何知道save()
方法是否成功。我试图找出它是否返回true / false,但它返回null
。
例如:
$contact_obj->save();
如何知道它有效?
答案 0 :(得分:3)
如果您使用Doctrine,请尝试trySave()
:
abstract class Doctrine_Record {
...
/**
* tries to save the object and all its related components.
* In contrast to Doctrine_Record::save(), this method does not
* throw an exception when validation fails but returns TRUE on
* success or FALSE on failure.
*
* @param Doctrine_Connection $conn optional connection parameter
* @return TRUE if the record was saved sucessfully without errors, FALSE otherwise.
*/
public function trySave(Doctrine_Connection $conn = null) {
try {
$this->save($conn);
return true;
} catch (Doctrine_Validator_Exception $ignored) {
return false;
}
}
}
答案 1 :(得分:0)
您可以通过$ contact_obj-> getId()函数进行检查,它会在成功时返回插入的ID。