<p>I would move that logic out of your model class and into a service method.</p>

<p>Your model could still have a thin wrapper for it:</p>

<pre><code>public function getVideos()
{
    return craft()-&gt;myPluginVideoService-&gt;getVideosByChannelId($this-&gt;id);
}
</code></pre>

<p>Then your plugin's video service would have:</p>

<pre><code>public function getVideosByChannelId($channelId)
{
    // Can memoize by channelId if this will be called frequently

    $result = craft()-&gt;db-&gt;createCommand()
        -&gt;select('*')
        -&gt;from('myVideoTable')
        -&gt;where('channelId = :channelId', array(':channelId' =&gt; $channelId))
        -&gt;queryAll();

    if ($result)
    {
        return MyPlugin_VideoModel::populateModels($result);
    }
}
</code></pre>

<p>The benefits are:</p>

<ol>
<li><p>Less cruft in your models, which should (as much as possible) be used as data transport objects</p></li>
<li><p>You're not leaking records out of your service layer (like your original model was doing)</p></li>
<li><p>You're building a consistent API at your service layer that any other classes (other models, other template variables, other services, other plugins, etc.) can go through.</p></li>
</ol>

<p>But essentially you're correct. You'll need to write methods to expose the relationships you're looking for to the templates.</p>

<blockquote>
  <p>what is the reason you use the direct database API, in stead of the <code>Record::model()-&gt;findAllByPk($pksArray)</code> method?</p>
</blockquote>

<p>Habit. Active records, while convenient for simple use-cases, have performance overhead associated with them, so usually I just default to DbCommand.</p>

CC BY-SA 3.0
2016-08-29T10:04:22.307Z
Ah right, that's a nicer way to structure it. Side question: what is the reason you use the direct database API, in stead of the Record::model()->findAllByPk($pksArray) method?

My original question still stands though: am I right that the way to get the relations of your models is through your own service methods? Which you have to add manually for every relationship?
Nelis86
CC BY-SA 3.0
2016-08-29T17:08:45.527Z
Updated my answer.
Brad Bell
CC BY-SA 3.0
2016-08-26T21:13:54.880Z
2016-08-29T17:08:39.477Z
2016-08-29T17:08:39.477Z
Brad Bell
<p>I recently started learning plugin development for Craft CMS. I'm creating a plugin that deals with video content. For this example I'll use two models: <code>MyPlugin_ChannelModel</code> and <code>MyPlugin_VideoModel</code>. I've also setup two record classes that mirror the model's properties (<code>MyPlugin_ChannelRecord</code> and <code>MyPlugin_VideoRecord</code>). </p>

<p>The two record classes have a many to many relationship with each other. So, a channel can have multiple videos and a video can be in multiple channels.</p>

<p>I can create, delete and modify records. So far, so good. Now, this next part confuses me. I want to use these records in my templates. Ideally, I'd like to do something like:</p>

<pre><code>{% for video in channel.videos %}
    {{ video.someProperty }}
{% endfor %}
</code></pre>

<p>However, I think the "rule" is that only services can interact with records. Models should be used in all other parts of a plugin to pass data (like passing data from a service to a template).</p>

<p>Since models do not have the relationship properties, I've written some getter methods on those models to help me get to my channels/videos from the opposite end of the relationship. An example for <code>MyPlugin_ChannelModel</code>:</p>

<pre><code>public function getVideos()
{
    if ($this-&gt;_videos === null &amp;&amp; $this-&gt;id !== null)
    {
        // Helper method that gets the record for this model
        $channelRecord = craft()-&gt;myPlugin_helpers-&gt;findRecordForModel($this);

        // Convert each related record back to a model
        $videoModels = array();
        foreach ($channelRecord-&gt;videos as $videoRecord) {
            $videoModel = new MyPlugin_VideoModel($videoRecord);
            $videoModels[] = $videoModel;
        }

        $this-&gt;_videos = $videoModels;
    }

    return $this-&gt;_videos;
}
</code></pre>

<p>It's nice that these getter methods automatically map to their corresponding property names (<code>$model-&gt;getVideos()</code> becomes <code>model.videos</code> in Twig), but this means I have to write each of these helper methods by hand on all my model classes to get the related models. Am I missing something, is there an easier way?</p>

<plugin-development><relations><models><records>
Getting related models in templates?
# Getting related models in templates?

(Asked by: username_0 on 2016-08-25)

I recently started learning plugin development for Craft CMS. I'm creating a plugin that deals with video content. For this example I'll use two models: MyPlugin_ChannelModel and MyPlugin_VideoModel. I've also setup two record classes that mirror the model's properties (MyPlugin_ChannelRecord and MyPlugin_VideoRecord). 
The two record classes have a many to many relationship with each other. So, a channel can have multiple videos and a video can be in multiple channels.
I can create, delete and modify records. So far, so good. Now, this next part confuses me. I want to use these records in my templates. Ideally, I'd like to do something like:
{% for video in channel.videos %}
    {{ video.someProperty }}
{% endfor %}

However, I think the "rule" is that only services can interact with records. Models should be used in all other parts of a plugin to pass data (like passing data from a service to a template).
Since models do not have the relationship properties, I've written some getter methods on those models to help me get to my channels/videos from the opposite end of the relationship. An example for MyPlugin_ChannelModel:
public function getVideos()
{
    if ($this->_videos === null && $this->id !== null)
    {
        // Helper method that gets the record for this model
        $channelRecord = craft()->myPlugin_helpers->findRecordForModel($this);

        // Convert each related record back to a model
        $videoModels = array();
        foreach ($channelRecord->videos as $videoRecord) {
            $videoModel = new MyPlugin_VideoModel($videoRecord);
            $videoModels[] = $videoModel;
        }

        $this->_videos = $videoModels;
    }

    return $this->_videos;
}

It's nice that these getter methods automatically map to their corresponding property names ($model->getVideos() becomes model.videos in Twig), but this means I have to write each of these helper methods by hand on all my model classes to get the related models. Am I missing something, is there an easier way?

**Tags:** plugin-development, relations, models, records

---

## 1 answers

### Answer by username_1 on 2016-08-26. Score: 2 | Accepted answer

I would move that logic out of your model class and into a service method.
Your model could still have a thin wrapper for it:
public function getVideos()
{
    return craft()->myPluginVideoService->getVideosByChannelId($this->id);
}

Then your plugin's video service would have:
public function getVideosByChannelId($channelId)
{
    // Can memoize by channelId if this will be called frequently

    $result = craft()->db->createCommand()
        ->select('*')
        ->from('myVideoTable')
        ->where('channelId = :channelId', array(':channelId' => $channelId))
        ->queryAll();

    if ($result)
    {
        return MyPlugin_VideoModel::populateModels($result);
    }
}

The benefits are:

Less cruft in your models, which should (as much as possible) be used as data transport objects
You're not leaking records out of your service layer (like your original model was doing)
You're building a consistent API at your service layer that any other classes (other models, other template variables, other services, other plugins, etc.) can go through.

But essentially you're correct. You'll need to write methods to expose the relationships you're looking for to the templates.

what is the reason you use the direct database API, in stead of the Record::model()->findAllByPk($pksArray) method?

Habit. Active records, while convenient for simple use-cases, have performance overhead associated with them, so usually I just default to DbCommand.

> **Comments:**
> **username_0**: Ah right, that's a nicer way to structure it. Side question: what is the reason you use the direct database API, in stead of the Record::model()->findAllByPk($pksArray) method?  My original question still stands though: am I right that the way to get the relations of your models is through your own service methods? Which you have to add manually for every relationship?  
> **username_1**: Updated my answer.  

---

