Keeping your Yii Models Lean – Use Behaviors

Standard

Active Record models are fantastic for consolidating “black box” logic and keeping your models self-aware of their business logic, but what do you do when the business rules and object specific operations keep adding up?

Do yourself a favor, and get into the habit of putting those business logic methods into attachable behaviors.
Continue reading

Advertisements

How to save multiple related models in yii [Complete Solution]

How to save multiple related models in yii
Standard

Scenario:
You are in a situation where you have two related tables a Parent and a child table. You need to create a user experience in which user presses Save button only once, meaning by, user enters all the data in parent model fields, then enters data in all child model fields, optionally generates more child model fields and then presses the save button.
Continue reading

Yii password, repeat password fields

Standard
class User extends CActiveRecord
{

    public $repeat_password;

    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    public function tableName()
    {
        return 'users';
    }

    public function rules()
    {
        return array(
            array('username', 'required'),
            array('password, repeat_password', 'required', 'on' => 'create'),
            array('password, repeat_password', 'length', 'min' => 6, 'max' => 30, 'on' => array('create', 'update')),
            array('password', 'compare', 'compareAttribute' => 'repeat_password', 'on' => array('create', 'update')),
            array('password, repeat_password', 'length', 'min' => 8),
            array('username', 'length', 'max' => 60),
            array('username', 'unique'),
            array('id, username', 'safe', 'on' => 'search'),
        );
    }

    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'username' => 'Username',
            'password' => 'Password',
        );
    }

    public function search()
    {
        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id, true);
        $criteria->compare('username', $this->username, true);
        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }

    public function encrypt($parameter)
    {
        return md5($parameter);
    }

    public function beforeSave()
    {
        if (!empty($this->password) && strlen($this->password))
        {
            $this->password = $this->encrypt($this->password);
        } else
        {
            if (empty($this->password))
                $this->password = $this->findByPk($this->id)->password;
        }
        return parent::beforeSave();
    }

}

fix for yii rights

Standard

Update UserIdentity.php

1- define protected variable protected $_id;

2- add following in authenticate() function

$this->setState('__name', $user->email_address);
$this->setState('__id', $user->id);

3- add following function

public function getId()
{
return $this->_id;
}