Plugin only saving one column in database I'm having trouble saving some data into the database with my plugin. I am able to save content only if an index is defined on that column, but only if unique=>true is set, which causes some problems because I need repeated values in column b. My function that runs after plugin install works, as I am able to save all the correct content in column A. Data is saved in column b if I set a unique index on it, otherwise the column fills with Null. Any help is greatly appreciated! 

Model:

namespace Craft;

class Plugin_MyModel extends BaseModel
{
    public $a = "";
    public $b = "";

    protected function defineAttributes()
    {
        return array(
            'a' =&gt; AttributeType::String,
            'b' =&gt; AttributeType::String,
        );
    }
}


Record:

&lt;?php

namespace Craft;

class Plugin_MyRecord extends BaseRecord
{
    public function getTableName()
    {
        return 'my_table';
    }

    protected function defineAttributes()
    {
        return array(
            'a'     =&gt; AttributeType::String,
            'b' =&gt; AttributeType::String,
        );
    }

    public function defineIndexes()
    {
        return array(
            array('columns' =&gt; array('a'), 'unique' =&gt; true),
        );
    }
}


Service that gets data from API call and attempts to save in DB:

&lt;?php

namespace Craft;

class Plugin_MyService extends BaseApplicationComponent
{

    protected $myRecord;

    public function __construct($myRecord = null)
    {
        $this-&gt;myRecord = $myRecord;
        if(is_null($this-&gt;myRecord))
        {
            $this-&gt;myRecord = Plugin_MyRecord::model();
        }   
    }


    public function saveItem(Plugin_MyModel &amp;$model)
    {
        if ($id = $model-&gt;getAttribute('id')) {
            if (null === ($record = $this-&gt;myRecord-&gt;findByPk($id))) {
                throw new Exception(Craft::t('Can\'t find item with ID "{id}"', array('id' =&gt; $id)));
            }
        } else {
            $record = $this-&gt;myRecord-&gt;create();
        }

        $record-&gt;setAttributes($model-&gt;getAttributes());
        if ($record-&gt;save()) {
            // update id on model (for new records)
            $model-&gt;setAttribute('id', $record-&gt;getAttribute('id'));

            return true;
        } else {
            $model-&gt;addErrors($record-&gt;getErrors());

            return false;
        }
    }


    public function loadAllItems()
    {
        $items = $this-&gt;api();

        foreach($items['Items'] AS $item)
        {        
            $new_item = new Plugin_MyModel();
            $new_item-&gt;a = $item['a'];
            $new_item-&gt;b = $item['b'];
            $this-&gt;saveItem($new_item);
        }
    }

    private function api(){
        //Returns API Data  
    }
}
I had a similar problem, ended up using multiple setAttribute() calls instead of setAttributes().

I created an array of attributes separately and then looped through them and called setAttribute for each one, seemed to do the trick.

if ($id = $model-&gt;getAttribute('id')) {
    if (null === ($record = $this-&gt;myRecord-&gt;findByPk($id))) {
            throw new Exception(Craft::t('Can\'t find item with ID "{id}"', array('id' =&gt; $id)));
        }
    } else {
        $record = $this-&gt;myRecord-&gt;create();
    }

    /* Not this: */       
    $record-&gt;setAttributes($model-&gt;getAttributes());

    /* This instead: */
    $attributes = array(
        'column1' =&gt; $col1,
        'column2' =&gt; $col2
    );    
    foreach($attributes as $k =&gt; $v) $record-&gt;setAttribute($k,$v);
}