First, the model instance is created. After that we set some data and save to database. Then we need to set a role and save relations. The code can be also put in a module install/upgrade script or executed from standalone script. If you put the code in index.php, remember to delete the code after you refresh the site. If you don’t do that, you will get an exception because some data are supposed to be unique.