Skip to content

State problem #46

@ccazette

Description

@ccazette

There is an issue with how states behaves. Luckily, it is easy to fix. This is the problem :

AutoModeler::factory("user", 1);

Will actually load the object. So the state is initially set to NEW, then to LOADING in the load method, then LOADED. That makes complete sense.

AutoModeler::factory("user")->load(NULL, 3)->current();

Will create the object from mysql_query_object. So the state is initially NEW, then direcly set to LOADED when the object is set. No LOADING.

This is especially annoying as mysql_query_object uses the magic __set(), whereas, load directly se the data in the _data array. So if in your magic set you want to do checks if the object is loading or if it is a manual set, you cannot rely on states...

The fix is simple : default AutoModeler state to STATE_LOADING, then in the construct method add an "else" statement to set to state to new if is not loading nor loaded (first two cases):

if ($id !== NULL)
{
    $this->load(db::select_array($this->fields())->where($this->_table_name.'.id', '=', $id));
}
elseif ($this->id) // We loaded this via mysql_result_object
{
    $this->_state = AutoModeler::STATE_LOADED;
}
else
{
    $this->_state = AutoModeler::STATE_NEW;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions