Mysql view in yii - php

I created 2 mysql views, and has generated from them 2 model.
MostPopularCoupon
class MostPopularCoupon extends CActiveRecord
{
public function tableName()
{
return 'most_popular_coupon';
}
public function rules()
{
return array(
array('coupon_id', 'required'),
array('coupon_id', 'numerical', 'integerOnly'=>true),
array('left_coupons', 'length', 'max'=>22),
array('stopped_at', 'safe'),
array('left_coupons, coupon_id, stopped_at', 'safe', 'on'=>'search'),
);
}
public function relations()
{
return array(
);
}
public function attributeLabels()
{
return array(
'left_coupons' => 'Left Coupons',
'coupon_id' => 'Coupon',
'stopped_at' => 'Stopped At',
);
}
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('left_coupons',$this->left_coupons,true);
$criteria->compare('coupon_id',$this->coupon_id);
$criteria->compare('stopped_at',$this->stopped_at,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}
MostActiveCity
class MostActiveCity extends CActiveRecord
{
public function tableName()
{
return 'most_active_city';
}
public function rules()
{
return array(
array('mines', 'length', 'max'=>21),
array('city', 'length', 'max'=>255),
array('mines, city', 'safe', 'on'=>'search'),
);
}
public function relations()
{
return array();
}
public function attributeLabels()
{
return array(
'mines' => 'Mines',
'city' => 'City',
);
}
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('mines',$this->mines,true);
$criteria->compare('city',$this->city,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}
But due to no experience, don't know how to use them correctly. I need to substitute company_id in the query to get the data of the company belongs to the current user...

Please relock at your design. You would be better off having model classes of City{} and Coupon{}, then have functions for example MostPopularCoupons inside the Coupon class. You could then call them as functions of the class. For example.
In your model
class Coupon extends CActiveRecord
{
// NOTE: This is important for your alias column.
public $couponCount;
public function tableName()
{
return 'coupon';
}
...
public function MostPopularCoupons()
{
$Criteria = new CDbCriteria();
$Criteria->select = ' count(*) as couponCount ';
$lstCoupons = Coupon::model()->findAll($Criteria);
return $lstCoupons;
}
}
Then, in your controller
$coupon_id = 10;
$model1 = Coupon::model()->findByPK((int)$coupon_id);
$lstCouponCount = Coupon::MostPopularCoupons();

Related

Get the last inserted row id which is save through model in Yii1.1?

I am using Microsoft SQL Server as my database and yii1.1 framework
$model = new Test();
$model->name = "test";
$model->save();
echo $model->id;
I can't get the id this way shown above the code. It doesn't return anything but the data is being saved to the database.
Here is my test class which I have generated through gii
<?php
class Test extends CActiveRecord
{
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function tableName()
{
return 'test';
}
public function rules()
{
return array(
array('name', 'length', 'max'=>10),
array('id, name', 'safe', 'on'=>'search'),
);
}
public function relations()
{
return array(
);
}
public function attributeLabels()
{
return array(
'id' => 'ID',
'name' => 'Name',
);
}
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('name',$this->name,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
}

YII: Value of variables not saving in the database

I am new to YII. I know this question has been asked a lot of times before nut i can really find an answer. So, please take a look.
I am saving some data in the database from my PaypalController.php
Here is the code of PaypalController.
$transid = $paymentResult['TRANSACTIONID'];
$profileid = $recurringResult['PROFILEID'];
/*var_dump($transid);
var_dump($profileid);
exit;*/
$payment = new Payment;
$payment->user_id = 2;
$payment->feature = 'import sending servers';
$payment->transaction_id = $transid;
$payment->amount = 10;
$payment->profile_id = $profileid;
if($payment->save()){
//var_dump($recurringResult);exit;
//echo "<script>alert('Your payment is succesful');</script>";
$this->redirect('../email-hustler/index');
}else{
print_r($payment->getErrors());
exit;
}
And here is the code of my Payment model
public $user_id;
public $feature;
public $transaction_id;
public $amount;
public $profile_id;
public $is_active;
public function tableName()
{
return '{{payment}}';
}
public function rules()
{
$rules = array(
array('user_id, feature, transaction_id, amount, profile_id, is_active', 'safe'),
);
return CMap::mergeArray($rules, parent::rules());
}
public function attributeLabels(){
return array(
'user_id' => 'user_id',
'feature' => 'feature',
'transaction_id' => 'transaction_id',
'amount' => 'amount',
'profile_id' => 'profile_id',
'is_active' => 'is_active',
);
}
public static function model($className=__CLASS__)
{
return parent::model($className);
}
Now the problem is the value of the variables is not saving but the value i am passing static are saved.
var_dump of variables is giving me correct output.
Please help.

Get attributes from yii CActiveRecord model

I have model like below, where i have defined some static variables (which is not in DB table) then i am trying to fetch those variables but it returns those variables which is in DB table. I am trying to fetch both variables (static variables as well as variables which is in DB table).
Model
class Eforms extends CActiveRecord
{
public $emp_name;
public $current_status;
public $action_type;
public $action_type_extra;
public $common_value = array(
1 => 'Yes',
2 => 'No',
);
public $hr_only_value = array(
1 => 'IT',
2 => 'BOLD',
);
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function tableName()
{
return 'tbl_eforms';
}
public function rules()
{
return array(
array('form_id', 'required'),
array('form_id, user_id', 'numerical', 'integerOnly'=>true),
array('name_in_form', 'length', 'max'=>500),
array('pdf_name', 'length', 'max'=>1000),
array('emp_name, current_status, action_type, action_type_extra', 'required', 'on'=>'form1'),
array('emp_name, current_status, action_type, action_type_extra','safe'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, form_id, user_id, name_in_form, email_recipients, pdf_name, created_on', 'safe', 'on'=>'search'),
);
}
................
...............
Controller :
public function actionIndex()
{
$model=new Eforms;
var_dump($model->attributes);exit;
}
If i changes CActiveRecord with CFormModel the it returns the only static variables not the DB related one.
From yii1 doc http://www.yiiframework.com/doc/api/1.1/CActiveRecord#attributes-detail
$model->attributes
Returns all column attribute values. Note, related objects are not
returned.
So you can access to the (related/calculated) var using
$myVar = $model->emp_name;
or
$model->emp_name = 'my_emp_name_value';

Create Rule In Form Model

in my Register Form, i want check email already in database:
class RegisterForm extends CFormModel
{
public $firstName;
public $lastName;
public $email;
public function rules()
{
return array(
array('firstName, lastName, email', 'required'),
array('email', 'email'),
array('email', 'checkEmail'),
);
}
public function attributeLabels()
{
return array();
}
public function checkEmail()
{
$record = Account::model()->findByAttributes(array('username'=>$this->email));
if ($record==null) {
return true;
}
return false;
}
}
but not working, how to create my rules for check email? somebody can help me????
Yii validators actually do not return true or false depending on validation result. Instead they add errors to your model, like this
public function checkEmail()
{
$record = Account::model()->findByAttributes(array('username'=>$this->email));
if ($record != null) {
$this->addError('email', 'Email should be unqiue');
}
}
Better use CUniqueValidator for this check
public function rules()
{
return array(
array('firstName, lastName, email', 'required'),
array('email', 'email'),
array('email', 'unique', 'attributeName' => 'username', 'className' => 'Account'),
);
}
That should work.

yii: Property "CWebUser.clientScript" is not defined

Hi iam newbie to yii framework. And iam getting this error after submitting the login details.
error
C:\wamp\www\yii\framework\web\auth\CWebUser.php(154)
152| $this->setState($name,$value);
153| else
154| parent::__set($name,$value);
155| }
and in STACKTRACE
C:\wamp\www\yiiapp1\protected\models\LoginForm.php(71): CModule->__get("user")
70| $duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
71| Yii::app()->user->login($this->_identity,$duration);
72| return true;
THIS IS MY USER.PHP FILE
class user extends CActiveRecord
{
public function validatePassword($password)
{
return CPasswordHelper::verifyPassword($password,$this->password);
}
public function hashPassword($password)
{
return CPasswordHelper::hashPassword($password);
}
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function tableName()
{
return 'user';
}
public function rules()
{
return array(
array('', 'safe', 'on'=>'search'),
);
}
public function relations()
{
return array(
);
}
public function attributeLabels()
{
return array(
);
}
public function search()
{
$criteria=new CDbCriteria;
return new CActiveDataProvider('user', array(
'criteria'=>$criteria,
));
}
}
LOGINFORM
class LoginForm extends CFormModel
{
public $username;
public $password;
public $rememberMe;
private $_identity;
public function rules()
{
return array(
// username and password are required
array('username, password', 'required'),
// rememberMe needs to be a boolean
array('rememberMe', 'boolean'),
// password needs to be authenticated
array('password', 'authenticate'),
);
}
public function attributeLabels()
{
return array(
'rememberMe'=>'Remember me next time',
);
}
public function authenticate($attribute,$params)
{
if(!$this->hasErrors())
{
$this->_identity=new UserIdentity($this->username,$this->password);
if(!$this->_identity->authenticate())
$this->addError('password','Incorrect username or password.');
}
}
public function login($identity,$duration)
{
if($this->_identity===null)
{
$this->_identity=new UserIdentity($this->username,$this->password);
$this->_identity->authenticate();
}
if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
{
$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
Yii::app()->user->login($this->_identity,$duration);// --> HERE THE STACKTRACE SHOWING THE ERROR
return true;
}
else
return false;
}
}
It seem you use Yii::app()->user->clientScript;, just replace it by Yii::app()->clientScript

Categories