<p>You shouldn't be returning a <a href="http://buildwithcraft.com/docs/plugins/records" rel="nofollow">record</a> instance to your templates directly. That can lead to all sorts of badness like templates being able to update the database themselves.</p>

<p>Have your plugin's <a href="http://buildwithcraft.com/docs/plugins/services" rel="nofollow">service</a> populate a <a href="http://buildwithcraft.com/docs/plugins/models" rel="nofollow">Mailer_LogModel</a> with just the attributes you want to expose to your template and return that.  Pretty sure after you do that, a simple:</p>

<pre><code>{% if log.dateFinished %}
</code></pre>

<p>will work.</p>

CC BY-SA 3.0
2014-07-15T23:31:52.617Z
My bad, just checked again, and it still gives me a "Scope error". Also tried `log.dateFinished is defined and log.dateFinished is not iterable`.
Victor
CC BY-SA 3.0
2014-07-15T23:36:18.077Z
What's the actual error you're getting?  log.dateFinished is undefined?
Brad Bell
CC BY-SA 3.0
2014-07-15T23:39:32.117Z
`Craft\Mailer_LogRecord and its behaviors do not have a method or closure named "dateFinished".`
Victor
CC BY-SA 3.0
2014-07-15T23:45:39.490Z
OH!  Updating my answer. :)
Brad Bell
CC BY-SA 3.0
2014-07-16T00:03:31.967Z
Makes sense, thanks. Noob question: Is there a Yii-shortcut which can populate the model? (I need all attributes)
Victor
CC BY-SA 3.0
2014-07-16T00:05:48.797Z
Mailer_LogModel::populateModel($record)
Brad Bell
CC BY-SA 3.0
2014-07-16T17:08:16.910Z
Note: For some reason when populating a Record, `model()` needs to be added: `Mailer_LogRecord::model()->populateRecord($log)`
Victor
CC BY-SA 3.0
2014-07-15T23:23:02.667Z
2014-07-16T00:01:06.960Z
2014-07-16T00:01:06.960Z
Brad Bell
<p>I have a optional <code>dateFinished</code> attribute in my <a href="http://buildwithcraft.com/docs/plugins/records" rel="nofollow noreferrer">Record</a>:</p>

<pre class="lang-php prettyprint-override"><code>protected function defineAttributes()
{
    return array(
        ...
        'dateFinished'  =&gt; AttributeType::DateTime,
        ...
    );
}
</code></pre>

<p>Some Records have it set to a <a href="http://php.net/manual/en/datetime.format.php" rel="nofollow noreferrer">DateTime</a> other have it set to <code>null</code> (default).</p>

<p>To prevent Twig errors I tried using the <a href="https://craftcms.stackexchange.com/q/356/177"><code>is defined</code> test</a>:</p>

<pre class="lang-twig prettyprint-override"><code>{{ log.dateFinished is defined ? log.dateFinished|date('m.d.y - H:m'|t) : '-'}}
</code></pre>

<p>But this doesn't work, I'm getting the typical "Scope error". After taking a look at the <a href="http://twig.sensiolabs.org/doc/functions/dump.html" rel="nofollow noreferrer"><code>dump()</code></a> it looks that even a <code>null</code> value is getting defined:</p>

<pre class="lang-php prettyprint-override"><code>  ["dateFinished"]=&gt;
  object(Craft\DateTime)#2670 (3) {
    ["date"]=&gt;
    string(19) "2014-07-15 17:48:25"
    ["timezone_type"]=&gt;
    int(1)
    ["timezone"]=&gt;
    string(6) "+00:00"
  }

  ....

  ["dateFinished"]=&gt;
  array(2) {
    ["column"]=&gt;
    string(8) "datetime"
    ["type"]=&gt;
    string(8) "datetime"
  }
</code></pre>

<p>I tried to solve this by "<a href="https://stackoverflow.com/a/14482336/3589841">Check(ing) if a variable is a date with Twig</a>":</p>

<pre class="lang-twig prettyprint-override"><code>{{ log.dateFinished is defined and log.dateFinished.timestamp is defined ? log.dateFinished|date('m.d.y - H:m'|t) : '-'}}
</code></pre>

<p>But this doesn't make a difference, I'm still getting the "Scope error". Any ideas?</p>

<plugin-development><templating><records>
How to check if a DateTime attribute is set?
# How to check if a DateTime attribute is set?

(Asked by: username_0 on 2014-07-15)

I have a optional dateFinished attribute in my Record:
protected function defineAttributes()
{
    return array(
        ...
        'dateFinished'  => AttributeType::DateTime,
        ...
    );
}

Some Records have it set to a DateTime other have it set to null (default).
To prevent Twig errors I tried using the is defined test:
{{ log.dateFinished is defined ? log.dateFinished|date('m.d.y - H:m'|t) : '-'}}

But this doesn't work, I'm getting the typical "Scope error". After taking a look at the dump() it looks that even a null value is getting defined:
  ["dateFinished"]=>
  object(Craft\DateTime)#2670 (3) {
    ["date"]=>
    string(19) "2014-07-15 17:48:25"
    ["timezone_type"]=>
    int(1)
    ["timezone"]=>
    string(6) "+00:00"
  }

  ....

  ["dateFinished"]=>
  array(2) {
    ["column"]=>
    string(8) "datetime"
    ["type"]=>
    string(8) "datetime"
  }

I tried to solve this by "Check(ing) if a variable is a date with Twig":
{{ log.dateFinished is defined and log.dateFinished.timestamp is defined ? log.dateFinished|date('m.d.y - H:m'|t) : '-'}}

But this doesn't make a difference, I'm still getting the "Scope error". Any ideas?

**Tags:** plugin-development, templating, records


---

## 1 answers

### Answer by username_1 on 2014-07-15. Score: 4 | Accepted answer

You shouldn't be returning a record instance to your templates directly. That can lead to all sorts of badness like templates being able to update the database themselves.
Have your plugin's service populate a Mailer_LogModel with just the attributes you want to expose to your template and return that.  Pretty sure after you do that, a simple:
{% if log.dateFinished %}

will work.

> **Comments:**
> **username_0**: My bad, just checked again, and it still gives me a "Scope error". Also tried `log.dateFinished is defined and log.dateFinished is not iterable`.  
> **username_1**: What's the actual error you're getting?  log.dateFinished is undefined?  
> **username_0**: `Craft\Mailer_LogRecord and its behaviors do not have a method or closure named "dateFinished".`  
> **username_1**: OH!  Updating my answer. :)  
> **username_0**: Makes sense, thanks. Noob question: Is there a Yii-shortcut which can populate the model? (I need all attributes)  
> **username_1**: Mailer_LogModel::populateModel($record)  
> **username_0**: Note: For some reason when populating a Record, `model()` needs to be added: `Mailer_LogRecord::model()->populateRecord($log)`  

---

