raoul2000 / yii2-workflow

A simple workflow engine for Yii2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

null not being handled properly in combination with setmap

mteichtahl opened this issue · comments

I am calling setMap() as below

        $this->registerWorkFlowEvents();

        $map = array();

        $converter = Yii::$app->get('statusConverter');

        foreach (self::$state as $state) {
            $map[$this->collectionName() . '/' . $state] = $state;
        }

        $map['null'] = 1;
        $converter->setMap($map);

which is providing me the following map

Array
(
    [article/DRAFT] => DRAFT
    [article/PUBLISHED] => PUBLISHED
    [article/EXPIRED] => EXPIRED
    [article/SCHEDULED] => SCHEDULED
    [article/MODERATED] => MODERATED
    [null] => 1
)

However, Im seeing the following

Exception 'yii\base\Exception' with message 'Conversion to SimpleWorkflow failed : no value found for id = null'

in /vagrant/app/vendor/raoul2000/yii2-workflow/src/base/StatusIdConverter.php:105

Stack trace:
#0 /vagrant/app/vendor/raoul2000/yii2-workflow/src/base/SimpleWorkflowBehavior.php(863): raoul2000\workflow\base\StatusIdConverter->toSimpleWorkflow(NULL)
#1 /vagrant/app/vendor/raoul2000/yii2-workflow/src/base/SimpleWorkflowBehavior.php(308): raoul2000\workflow\base\SimpleWorkflowBehavior->getOwnerStatus()
#2 /vagrant/app/vendor/raoul2000/yii2-workflow/src/base/SimpleWorkflowBehavior.php(248): raoul2000\workflow\base\SimpleWorkflowBehavior->initStatus()

I have tried all combination of null, ' null', NULL, 'NULL' etc, with the same results

The check on line 100

if ($id === null) {
            $id = self::VALUE_NULL;
        }

Leads me to think that if I pass in NULL, or null, then $id gets replaced. then line 103

        $statusId = array_search($id, $this->_map);

tried to find a matching entry in $map.

As I already have null in $map, I would assume this would return a value of 1, but rather it throws on line 121.

any ideas ?

the following also fails

$map[\raoul2000\workflow\base\StatusIdConverter::VALUE_NULL] = 1;

commented

Hi,

The problem is that when the Article model instance is created, it's status attribute is NULL and as a status Id Converter is declared, the SimpleWorkflow behavior is going to ask to the converter for a valid status ID to replace this NULL value. I dont know what default map you have used but I assume it doesn't contain any match for NULL.

Assuming you have an Artcile model like this one :

class Article extends \yii\db\ActiveRecord
{
    public function behaviors()
    {
        return [
            'workflow' =>[
                'class' => \raoul2000\workflow\base\SimpleWorkflowBehavior::className(),
                // declare a status converter with ID 'converter'
                'statusConverter' => 'converter'
            ]
        ];
    }
}

What you should do is :

  • initialize the Status Id Converter with a minimal map definig a mapping for NULL.
Yii::$app->set('converter',[
    'class' => 'raoul2000\workflow\base\StatusIdConverter',
    'map' => [StatusIdConverter::VALUE_NULL => StatusIdConverter::VALUE_NULL ]
]);
  • Once you have your final conversion map ready, update the Status Id Converter component, and don't forget the NULL match.
// the status ID conversion is invoked here, and status being NULL
// the conversion will look for a NULL matching line in the conversion map
$article = new Article(); 

$article->getStatusConverter()->setMap([
    'article/DRAFT' => 'DRAFT',
    'article/PUBLISHED' => 'PUBLISHED',
    'article/EXPIRED' => 'EXPIRED',
    'article/SCHEDULED' => 'SCHEDULED',
    'article/MODERATED' => 'MODERATED',
    StatusIdConverter::VALUE_NULL => StatusIdConverter::VALUE_NULL  
]);

This is ok if you want to consider that the NULL status is invariant regarding the Status Id Convertion. If that's not the case and if for instance you want to convert NULL status into 'article/new', then, you will have to force the conversion for all models that were created before the map is updated.
To force status ID conversion on a model, you must call the initStatus()method on this model:

$article->initStatus();

I hope this explanation is clear. If not, don't hesitate to ask ...

One last point : I think you should update yii2-workflow to dev-master until I release 0.0.12 because from the stack trace I see you are not using the lastest version. As for releasing 0.0.12 I will do that soon but I wanted to wait for your feedback first and make more tests on my side ....

Thanks raoul

I can confirm all is working as expected.

Thank you very much for your support.

commented

you're welcome :)