[Solved]Problem saving specfic attributes with save or saveAttributes

I'm trying to save two specific columns for login attempts.

I've tried to use both save and saveAttributes but get the same error message: Column name must be either a string or an array.

this is how i've tried to call them:

$user->saveAttributes(array('login_attempts'=>$user->login_attempts + 1, 'last_login_attempt'=>time()));

and

$user->saveAttributes(array('login_attempts', 'last_login_attempt'));

even tried it as one string in the array got a different error that time.

tried save as well setting validation to false but get the same results.

looked through the cookbook and definitive guide but couldn't find an example on how to save specific attributes.

Both of your usages should be fine. Could you give complete error information?

The attributes are declared in safeAttributes() ?

No they are not declared in safeattributes. As I understand safe, is that it is for massive assignments not saving.

Don't have the exact message as i'm at aN outing but i belive it was

column should be a string or array

I'll get the full error when i get home if needed.

ok this is the extact error message:

CDbException

Description

Column name must be either a string or an array.

Source File

D:\wamp\www\public_html\players-life.com\private\yii\framework\db\schema\CDbCommandBuilder.php(617)

00605:            }

00606:            if(count($values)===1)

00607:            {

00608:                $entries=array();

00609:                foreach($values[0] as $name=>$value)

00610:                    $entries[]=$prefix.$table->columns[$name]->rawName.($value===null?' IS NULL':'='.$value);

00611:                return implode(' AND ',$entries);

00612:            }

00613:

00614:            return $this->createCompositeInCondition($table,$values,$prefix);

00615:        }

00616:        else

00617: throw new CDbException(Yii::t('yii','Column name must be either a string or an array.'));

00618:    }

00619:

00620:    /**

00621:      * Generates the exp​ression for selecting rows with specified composite key values.

00622:      * @param CDbTableSchema the table schema

00623:      * @param array list of primary key values to be selected within

00624:      * @param string column prefix (ended with dot)

00625:      * @return string the exp​ression for selection

00626:      * @since 1.0.4

00627:      */

00628:    protected function createCompositeInCondition($table,$values,$prefix)

00629:    {

this is the line that causes it

			$user->saveAttributes(array('login_attempts', 'last_login_attempt'));


I found the problem after tracing through the code. My table i'm using doesn't have a primary key. it's a multiuse login table supporting traditional user/pass logins and open id logins.

The problem was since the primary key for the table was null for the AR that was what it was complaining about.

I was able to achieve what i was trying to do which was to update the 2 fields dealing with a failed login by doing this:

			$user->updateAll(array('login_attempts'=>$user->login_attempts+1, 'last_login_attempt'=>time()), 'LOWER(user_name)=?', array($username));


.

qiang perhaps a warning could be generated when the table doesn't have a primary key when using methods that will need the key to complete successfully.