Skip to main content

Posts

Showing posts with the label model

YII2 DB Count Results query

YII2 DB count results query Simple in the controller or view file add do this: $count =MODEL:: find ()   ->select([ 'COUNT(id) AS cnt' ])   ->where([   'field1' => $value1 , ])-> andWhere([ '>' , 'field2' , $value2 ]) - >one()->cnt; And in the model of the table add before this line: public static function tableName() Add public $cnt ; like below: public $cnt ; public static function tableName() {

YII, my sample model for search with relataionship

A model file for yii which has search fucntion which searches using relation ship: <?php /** * This is the model class for table "sf_category_slider". * * The followings are the available columns in table 'sf_category_slider': * @property integer $rec_id * @property integer $cat_id * @property string $brand_id * @property string $slider_image * @property string $slider_link */ class CategorySlider extends CActiveRecord { /** * Returns the static model of the specified AR class. * @return CategorySlider the static model class */ public $category_name ; public static function model ( $className = __CLASS__) { return parent :: model ( $className ); } /** * @return string the associated database table name */ public function tableName () { return 'sf_category_slider' ; } /** * @return array validation rules for model attribute...

yii, some form validation examples

Yii some basic form validations using rules in models like email, compare password (match password), date, unique, captcha etc Below is full model code: <?php /** * This is the model class for table "users". * * The followings are the available columns in table 'users': * @property integer $user_id * @property string $user_name * @property string $user_password * @property string $user_email * @property string $user_gender * @property integer $user_age * @property string $user_location * @property string $user_avatar * @property string $user_facebook_id * @property integer $user_receive_newsletters_flag * @property integer $is_admin */ class Users extends CActiveRecord { public $confirm_password , $user_captcha ; /** * Returns the static model of the specified AR class. * @return Users the static model class */ public static function model ( $className = __CLASS__) { return parent :: model (...

yii, mode rule for unique composite key

Here is the code to to make a rule in the model file to make unique composite keys : Save code below as proctected/components/CompositeUniqueKeyValidator.php <?php /** * CompositeUniqueKeyValidator class file. */ class CompositeUniqueKeyValidator extends CValidator { /** * @var string comma separated columns that are unique key */ public $keyColumns ; public $errorMessage = '"{columns_labels}" are not unique' ; /** * @var boolean whether the error message should be added to all of the columns */ public $addErrorToAllColumns = false ; /** * @param CModel $object the object being validated * @param string $attribute if there is an validation error then error message * will be added to this property */ protected function validateAttribute ( $object , $attribute ) { $class = get_class ( $object ); Yii :: import ( $class ); $ke...