title: How to assign role to admin user Programmatically in Magento?
body: <p>I need to assign a user role to admin user programmatically in Magento. How can I? I tried some methods like, </p>
  
  <pre><code>try {
  $user-&gt;setRoleIds(array(&lt;role_id&gt;))
  -&gt;setRoleUserId($user-&gt;getUserId())
  -&gt;saveRelations();
  
  } catch (Exception $e) {
  echo $e-&gt;getMessage();
  exit;
  }
  </code></pre>
  
  <p>But when we apply the above code, the database get corrupted and gets an Error - <code>"Parent Role id 'G5' does not exist"</code>. </p>
  
  <p>This is because, when I apply the above code, the admin user with parent role Id is get deleted and I cant access the backend there after, even If I recreate it again!</p>
  
  <p>Can anyone point out a solution?</p>