<p>Assuming your plugin is going to be the only thing referencing this Active Record relationship, your plugin could have its own <code>MyUserRecord</code> that extends Craft's <code>UserRecord</code> and does something like:</p>

<pre><code>public function defineRelations()
{
    $relations = parent::defineRelations();

    $relations['myUser'] = array(static::BELONGS_TO, 'MyUserRecord', 'id', 'required' =&gt; true, 'onDelete' =&gt; static::CASCADE);

    return $relations;
}
</code></pre>

<p>And you would just utilize <code>MyUserRecord</code> instead of Craft's <code>UserRecord</code> in your plugin.</p>

CC BY-SA 3.0
2015-11-16T03:01:42.130Z
2015-11-16T03:01:42.130Z
Brad Bell
<p>In a custom plugin is it possible to have a element type with a belongs to relationship to a user, as you can only define half of the relationship (without somehow modifying the userRecord). </p>

<p>What would be the best practices to get something like this working?</p>

<plugin-development><relations>
How to handle Belongs To User relationship
# How to handle Belongs To User relationship

(Asked by: username_0 on 2015-11-16)

In a custom plugin is it possible to have a element type with a belongs to relationship to a user, as you can only define half of the relationship (without somehow modifying the userRecord).

What would be the best practices to get something like this working?

**Tags:** plugin-development, relations


---

## 1 answers

### Answer by username_1 on 2015-11-16. Score: 0 | Accepted answer

Assuming your plugin is going to be the only thing referencing this Active Record relationship, your plugin could have its own MyUserRecord that extends Craft's UserRecord and does something like:
public function defineRelations()
{
    $relations = parent::defineRelations();

    $relations['myUser'] = array(static::BELONGS_TO, 'MyUserRecord', 'id', 'required' => true, 'onDelete' => static::CASCADE);

    return $relations;
}

And you would just utilize MyUserRecord instead of Craft's UserRecord in your plugin.


---

