Skip to content

return values for Model::create #38

Open
@xadhoom

Description

@xadhoom

Hi!

according to Model.php, in function create, we always have a model even if
the save has failed:

public static function create($attributes, $validate=true)
    {
        $class_name = get_called_class();
        $model = new $class_name($attributes);
        $model->save($validate);
        return $model;
    }

what is the best way to handle a failed save? I'm using is_new_record attribute to check if the model has been saved or not.
Is this the correct way ?
Or better not use create and use initialize+save directly ?

Or, is reasonable to change the create function to:

public static function create($attributes, $validate=true)
    {
        $class_name = get_called_class();
        $model = new $class_name($attributes);
        $res = $model->save($validate);
        if ( $res )
            return $model;
        else
            return $res;
    }

regards,
mat

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions