Total Pageviews

Tuesday, July 29, 2014

Relationships in Yii

  • BELONGS_TO: e.g. Student belongs to School
  • HAS_MANY: e.g. School has many Student
  • HAS_ONE: e.g. Student has at most one School
  • MANY_MANY: e.g. Student belongs to many Teacher and Teacher has many Student.
​​class Student extends CActiveRecord
{
  public function relations()
     {
return array(
             'school'=>array(self::BELONGS_TO, 'School', 'school_id'),
             'teachers'=>array(self::MANY_MANY, 'Teacher','tbl_student_teacher(student_id, teacher_id)'),
         );
}
}

class School extends CActiveRecord
{
    ......
 
    public function relations()
    {
        return array(
            'students'=>array(self::HAS_MANY, 'Student', 'school_id'),
            'teacher'=>array(self::HAS_ONE, 'Teacher', 'school_id'),
        );
    }
}

FindAll() in Yii

$models = Vehicle::model()->findAll( array('order' => 'vehicle_no'));

FindAll with Where clause = condition
​MainMenu::model() -> findAll(array("condition"=>"res_id =  $res_id","order"=>"name"));​

FindAll with Where clause Like condition
$Staff= CHtml::listData(AuthAssignment::model() -> findAll('itemname LIKE :Staff',array(':Staff' => "%Staff%")), 'userid', 'userid');

Joining Two tables
$criteria=new CDbCriteria;
$criteria->select = 't.userid,users.username';
$criteria -> join = 'JOIN  users on userid= users.id';
$criteria->condition = 'itemname LIKE :Staff OR itemname LIKE :Admin OR itemname LIKE :Cashier'; 
$criteria->params    = array(':Staff'=>'%Staff%',':Admin'=>'%Admin%',':Cashier'=>'%Cashier%'); 
$criteria->order = 'username DESC,userid DESC';
$Staff = AuthAssignment::model()->findAll($criteria);
$StaffList= CHtml::listData($Staff, 'userid', 'username');
​echo $form->dropDownList($model,'emp_id',$StaffList);​




Thursday, July 17, 2014

Insert Update with createCommand in yii

public function assignVehicle($driver_id=null,$vehicle_id=null)
{
$sql = "UPDATE `drivers` SET `vehical_id`=$vehicle_id WHERE id=:driver_id";

$parameters = array(":driver_id"=>$driver_id);

Yii::app()->db->createCommand($sql)->execute($parameters);
}

public function insertCharges($area_id=null,$charge=null){


$sql = "insert into delivery_charge (`area_id`,`charge`) values (:area_id,:charge)";

$parameters = array(":area_id"=>$area_id,":charge"=>$charge);

Yii::app()->db->createCommand($sql)->execute($parameters);


}

Install CakePHP

We need,

  • A web server (Eg:Apache)
  • PHP  5.2.8 or greater version
  • A database server (Eg: MySQL)
  • Enabled pdo_mysql in PHP configurations
  • Basic PHP Knowledge (Object Oriented Programming, MVC Programming Pattern)
Download CakePHP (http://cakephp.org/)


Once you download CakePHP ZIP folder,extract files and rename the folder as cakephp or whatever….
Put this folder in your document root. I’m using WAMP Server. So my document root is www.
Then access this cakephp folder through your browser.

Figure 1 : Browser Display

In the first red line it says to change the value of 'Security.salt'.
"Notice (1024): Please change the value of 'Security.salt' in APP/Config/core.php to a salt value specific to your application. [CORE\Cake\Utility\Debugger.php, line 849]"

Open "/app/Config/core.php" and change the following code
Configure::write('Security.salt', 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi');
to 
Configure::write('Security.salt', 'pl345e-P45s_7h3*S@l7!');
and refresh the browser.

Now you will see that first red line will disappear.

Second red line it says to change the value of "Security.cipherSeed".
"Notice (1024): Please change the value of 'Security.cipherSeed' in APP/Config/core.php to a numeric (digits only) seed value specific to your application. [CORE\Cake\Utility\Debugger.php, line 853]"

Open "/app/Config/core.php" and change the following code
Configure::write('Security.cipherSeed', '76859309657453542496749683645');
to 
Configure::write('Security.cipherSeed', '7485712659625147843639846751');
and refresh the browser.

Now you will see that second red line will disappear.

In yellow line it says to configure the database settings.
"Your database configuration file is NOT present.
Rename APP/Config/database.php.default to APP/Config/database.php"

Open "/app/Config/database.php.default"
and change the following code 

public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'user',
'password' => 'password',
'database' => 'database_name',
'prefix' => '',
//'encoding' => 'utf8',
);
Insert your hostname for host, database username for login. database password for password and database name for database


Uploading image/file by creating a directory on the server in yii

$model->attributes=$_POST['Vehicle'];
$model->tax_document=CUploadedFile::getInstance($model,'tax_document');
$model->mot_document=CUploadedFile::getInstance($model,'mot_document');
$model->image=CUploadedFile::getInstance($model,'image');
$model->save();
if($model->save())

   $path="document/vechicle/";
if(!file_exists('document/vechicle/'.$model->id)){
mkdir('document/vechicle/'.$model->id, 0777,true);
chmod('document/vechicle/'.$model->id, 0777);
}
if ($model->tax_document){
$model->tax_document->saveAs('document/vechicle/'.$model->id.'/taxt'.$model->tax_document);
}
if ($model->mot_document){
$model->mot_document->saveAs('document/vechicle/'.$model->id.'/mot'.$model->mot_document);
}
if ($model->image){
$model->image->saveAs('document/vechicle/'.$model->id.'/img'.$model->image);
}
$this->redirect(array('view','id'=>$model->id));