<p>I have since discovered that searching on a related model is fairly easy to implement. You just define the related model as an attribute that can be searched on in <code>defineSearchableAttributes</code> method of the element type.</p>

<p>For example:</p>

<pre><code>/**
 * @inheritDoc IElementType::defineSearchableAttributes()
 *
 * @return array
 */
public function defineSearchableAttributes()
{
    return ['dateCreated', 'totalPaid', 'customer'];
}
</code></pre>

<p>Then you just have to have a matching handler for that attribute (<em>customer</em>) in the <code>modifyElementsQuery</code> method, like so:</p>

<pre><code>    if ($criteria-&gt;customer) {
        if ($criteria-&gt;customer instanceof MyPlugin_CustomerModel) {
            if ($criteria-&gt;customer-&gt;id) {
                $criteria-&gt;customerId = $criteria-&gt;customer-&gt;id;
                $criteria-&gt;customer = null;
            } else {
                return false;
            }
        }
    }

    if ($criteria-&gt;customerId) {
        $query-&gt;andWhere(DbHelper::parseParam('orders.customerId', $criteria-&gt;customerId, $query-&gt;params));
    }
</code></pre>

<p>Then once search indexes are rebuilt, you'll be able to search on all attributes of a customer.</p>

CC BY-SA 3.0
2016-05-04T09:18:59.933Z
So this allows you to enter 'Australia' or 'customer:Australia' to find all related customers with Australia as the country. 


But the big question is, how would you search specifically on just the *country* attribute of a customer (i.e. the customers.country attribute)? What is the search input syntax for that?
Tim Everts
CC BY-SA 3.0
2016-05-04T09:14:57.150Z
2016-05-04T09:14:57.150Z
Tim Everts
<p>If I define searchable fields via <code>IElementType::defineSearchableAttributes()</code>, the provided fields have to be model attributes. Can they also be getter-methods on the model class, which may return values from other related elements? </p>

<p>e.g. on <strong>Plugin_XYElementType.php</strong>: </p>

<pre><code>public function defineSearchableAttributes()
{
    return ['relatedTitle','otherField'],
}
</code></pre>

<p>in <strong>Plugin_XYModel.php</strong>:</p>

<pre><code>public function getRelatedTitle()
{
    return craft()-&gt;plugin_service-&gt;getTitleFromRelatedThing($this-&gt;relatedThingId);
}
</code></pre>

<p>Will this fields be correctly indexed and searchable in a ElementIndexView, where I use this related fields in a table column? Or does this only work for Attributes, that were defined via <code>defineAttributes()</code> ?</p>

<p>Thanks!</p>

CC BY-SA 3.0
2016-05-04T08:22:06.293Z
I'd love to know the answer to this too. If my element type model has a related model (e.g. a customer model related to the order element type) is it possible to allow searching on attributes of the related (customer) model from the ElementIndexView search field? 

If so, how do we set this up in our custom element type class?
Tim Everts
<plugin-development><search><elementtype><baseelementmodel>
Does defineSearchableAttributes() work with getter methods on the model?
# Does defineSearchableAttributes() work with getter methods on the model?

(Asked by: username_0 on 2015-12-01)

If I define searchable fields via IElementType::defineSearchableAttributes(), the provided fields have to be model attributes. Can they also be getter-methods on the model class, which may return values from other related elements? 
e.g. on Plugin_XYElementType.php: 
public function defineSearchableAttributes()
{
    return ['relatedTitle','otherField'],
}

in Plugin_XYModel.php:
public function getRelatedTitle()
{
    return craft()->plugin_service->getTitleFromRelatedThing($this->relatedThingId);
}

Will this fields be correctly indexed and searchable in a ElementIndexView, where I use this related fields in a table column? Or does this only work for Attributes, that were defined via defineAttributes() ?
Thanks!

**Tags:** plugin-development, search, elementtype, baseelementmodel
> **Comments:**
> **username_1**: I'd love to know the answer to this too. If my element type model has a related model (e.g. a customer model related to the order element type) is it possible to allow searching on attributes of the related (customer) model from the ElementIndexView search field?   If so, how do we set this up in our custom element type class?  

---

## 1 answers

### Answer by username_1 on 2016-05-04. Score: 2

I have since discovered that searching on a related model is fairly easy to implement. You just define the related model as an attribute that can be searched on in defineSearchableAttributes method of the element type.
For example:
/**
 * @inheritDoc IElementType::defineSearchableAttributes()
 *
 * @return array
 */
public function defineSearchableAttributes()
{
    return ['dateCreated', 'totalPaid', 'customer'];
}

Then you just have to have a matching handler for that attribute (customer) in the modifyElementsQuery method, like so:
    if ($criteria->customer) {
        if ($criteria->customer instanceof MyPlugin_CustomerModel) {
            if ($criteria->customer->id) {
                $criteria->customerId = $criteria->customer->id;
                $criteria->customer = null;
            } else {
                return false;
            }
        }
    }

    if ($criteria->customerId) {
        $query->andWhere(DbHelper::parseParam('orders.customerId', $criteria->customerId, $query->params));
    }

Then once search indexes are rebuilt, you'll be able to search on all attributes of a customer.

> **Comments:**
> **username_1**: So this allows you to enter 'Australia' or 'customer:Australia' to find all related customers with Australia as the country.    But the big question is, how would you search specifically on just the *country* attribute of a customer (i.e. the customers.country attribute)? What is the search input syntax for that?  

---

