0

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();
    }

}
2

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

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.

1
  • 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 Sep 18 '16 at 22:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.