<p>User error. It's a naming collision with the <code>getAttributes</code> method within my model and the <code>CModel</code> class.</p>

CC BY-SA 3.0
2016-09-18T22:00:58.823Z
2016-09-18T23:11:50.230Z
2016-09-18T23:11:50.230Z
Fyrebase
<p>Difficult to know for sure without seeing what's going on in <code>craft()-&gt;core_code-&gt;joinDataAttributes</code> and what type of data is in <code>$this-&gt;dataAttributes</code>, but my guess is its because you're effectively casting whatever the results of <code>craft()-&gt;core_code-&gt;joinDataAttributes</code> to a string by prepending an space to it here:</p>

<pre><code>' '.craft()-&gt;core_code-&gt;joinDataAttributes($this-&gt;dataAttributes)
</code></pre>

<p>And whatever code is working with that data isn't expecting a string, but an array.</p>

CC BY-SA 3.0
2016-09-18T22:02:48.390Z
Thanks brad, but it was actually something a little more silly on my part. I had unintentionally created a conflict with my method names
Fyrebase
CC BY-SA 3.0
2016-09-18T21:58:32.937Z
2016-09-18T21:58:32.937Z
Brad Bell
<p>I have a strange problem with the following public method on a model.</p>

<pre><code>public function getAttributes()
{
    return $this-&gt;getId() . $this-&gt;getStyle() . $this-&gt;getDataAttributes();
}
</code></pre>

<p><code>CMapIterator</code> warns with the following <code>array_keys() expects parameter 1 to be array, string given</code></p>

<p>But if I alter it to</p>

<pre><code>public function getAttributes()
{
    return array($this-&gt;getId() . $this-&gt;getStyle() . $this-&gt;getDataAttributes())[0];
}
</code></pre>

<p>Everybody is happy. It's late and I'm probably shouldn't be coding but any thoughts on the issue would be much appreciated.</p>

<p>Here is the complete model for reference...</p>

<pre><code>namespace Craft;

class Core_CodeModel extends BaseModel
{
    /**
     * @return array
     */
    protected function defineAttributes()
    {
        return array_merge(parent::defineAttributes(), array(
            'cssId' =&gt; array(AttributeType::String),
            'cssClass' =&gt; array(AttributeType::String),
            'style' =&gt; array(AttributeType::String),
            'dataAttributes' =&gt; array(AttributeType::Mixed),
        ));
    }

    public function getDataAttributes()
    {
        return is_array($this-&gt;dataAttributes) ? ' ' . craft()-&gt;core_code-&gt;joinDataAttributes($this-&gt;dataAttributes) : '';
    }

    public function getId()
    {
        return !empty($this-&gt;cssId) ? ' id="' . $this-&gt;cssId . '"' : '';
    }

    public function getStyle()
    {
        return !empty($this-&gt;style) ? ' style="' . str_replace(array("\r", "\n"), '', $this-&gt;style) . '"' : '';
    }

    public function getAttributes()
    {
        return $this-&gt;getId() . $this-&gt;getStyle() . $this-&gt;getDataAttributes();
    }

}
</code></pre>

<models>
Model public method throws CMapIterator warning
# Model public method throws CMapIterator warning

(Asked by: username_0 on 2016-09-18)

I have a strange problem with the following public method on a model.
public function getAttributes()
{
    return $this->getId() . $this->getStyle() . $this->getDataAttributes();
}

CMapIterator warns with the following array_keys() expects parameter 1 to be array, string given
But if I alter it to
public function getAttributes()
{
    return array($this->getId() . $this->getStyle() . $this->getDataAttributes())[0];
}

Everybody is happy. It's late and I'm probably shouldn't be coding but any thoughts on the issue would be much appreciated.
Here is the complete model for reference...
namespace Craft;

class Core_CodeModel extends BaseModel
{
    /**
     * @return array
     */
    protected function defineAttributes()
    {
        return array_merge(parent::defineAttributes(), array(
            'cssId' => array(AttributeType::String),
            'cssClass' => array(AttributeType::String),
            'style' => array(AttributeType::String),
            'dataAttributes' => array(AttributeType::Mixed),
        ));
    }

    public function getDataAttributes()
    {
        return is_array($this->dataAttributes) ? ' ' . craft()->core_code->joinDataAttributes($this->dataAttributes) : '';
    }

    public function getId()
    {
        return !empty($this->cssId) ? ' id="' . $this->cssId . '"' : '';
    }

    public function getStyle()
    {
        return !empty($this->style) ? ' style="' . str_replace(array("\r", "\n"), '', $this->style) . '"' : '';
    }

    public function getAttributes()
    {
        return $this->getId() . $this->getStyle() . $this->getDataAttributes();
    }

}

**Tags:** models


---

## 2 answers

### Answer by username_0 on 2016-09-18. Score: 2

User error. It's a naming collision with the `getAttributes` method within my model and the `CModel` class.


---

### Answer by username_1 on 2016-09-18. Score: 1

Difficult to know for sure without seeing what's going on in craft()->core_code->joinDataAttributes and what type of data is in $this->dataAttributes, but my guess is its because you're effectively casting whatever the results of craft()->core_code->joinDataAttributes to a string by prepending an space to it here:
' '.craft()->core_code->joinDataAttributes($this->dataAttributes)

And whatever code is working with that data isn't expecting a string, but an array.

> **Comments:**
> **username_0**: Thanks brad, but it was actually something a little more silly on my part. I had unintentionally created a conflict with my method names  

---

