1

有人可以告诉我如何知道该save()方法是否成功。我试图找出它是否返回真/假,但它返回null.

例如:

$contact_obj->save();

怎么知道它有效?

4

2 回答 2

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;
        }
    }
}
于 2013-08-30T15:36:55.893 回答
0

您可以通过 $contact_obj->getId() 函数进行检查,它会在成功时返回插入的 id。

于 2013-09-02T11:10:43.623 回答