ساخت پایگاه داده ها وروابط
ما احتیاج داریم که سه جدول بسازیم tbl_issue, tbl_user و جدولی برای نگه داشتن روابط بین جداول که اون رو هم tbl_project_user_assignment مینامیم.
برای راحتی کار شما ما یک زبان پایه برای تعریف داده های شما ارائه داده ایم.
CREATE TABLE IF NOT EXISTS 'tbl_issue'
(
'id' INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
'name' varchar(256) NOT NULL,
'description' varchar(2000),
'project_id' INTEGER,
'type_id' INTEGER,
'status_id' INTEGER,
'owner_id' INTEGER,
'requester_id' INTEGER,
'create_time' DATETIME,
'create_user_id' INTEGER,
'update_time' DATETIME,
'update_user_id' INTEGER
) ENGINE = InnoDB
;
CREATE TABLE IF NOT EXISTS 'tbl_user'
(
'id' INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
'email' Varchar(256) NOT NULL,
'username' Varchar(256),
'password' Varchar(256),
'last_login_time' Datetime,
'create_time' DATETIME,
'create_user_id' INTEGER,
'update_time' DATETIME,
'update_user_id' INTEGER
) ENGINE = InnoDB
;
CREATE TABLE IF NOT EXISTS 'tbl_project_user_assignment'
(
'project_id' Int(11) NOT NULL,
'user_id' Int(11) NOT NULL,
'create_time' DATETIME,
'create_user_id' INTEGER,
'update_time' DATETIME,
'update_user_id' INTEGER,
PRIMARY KEY ('project_id','user_id')
) ENGINE = InnoDB
;
-- The Relationships
ALTER TABLE 'tbl_issue' ADD CONSTRAINT 'FK_issue_project' FOREIGN KEY
('project_id') REFERENCES 'tbl_project' ('id') ON DELETE CASCADE ON
UPDATE RESTRICT;
ALTER TABLE 'tbl_issue' ADD CONSTRAINT 'FK_issue_owner' FOREIGN KEY
('owner_id') REFERENCES 'tbl_user' ('id') ON DELETE CASCADE ON UPDATE
RESTRICT;
ALTER TABLE 'tbl_issue' ADD CONSTRAINT 'FK_issue_requester' FOREIGN
KEY ('requester_id') REFERENCES 'tbl_user' ('id') ON DELETE CASCADE ON
UPDATE RESTRICT;
ALTER TABLE 'tbl_project_user_assignment' ADD CONSTRAINT 'FK_project_
user' FOREIGN KEY ('project_id') REFERENCES 'tbl_project' ('id') ON
DELETE CASCADE ON UPDATE RESTRICT;
ALTER TABLE 'tbl_project_user_assignment' ADD CONSTRAINT 'FK_user_
project' FOREIGN KEY ('user_id') REFERENCES 'tbl_user' ('id') ON
DELETE CASCADE ON UPDATE RESTRICT;
-- Insert some seed data so we can just begin using the database
INSERT INTO 'tbl_user'
('email', 'username', 'password')
VALUES
('test1@notanaddress.com','Test_User_One', MD5('test1')),
('test2@notanaddress.com','Test_User_Two', MD5('test2'))
;.
حالا که ما جداول رو ساخته شده داریم احتیاج داریم کخ مدل AR رو برای اینها درتس کنیم تا در برنامه بتونیم به راحتی ازشون استفاده کیم.ما این کار رو نیز انجام دادیم وقتی ساختیم project.php مدل کلاس رو در فصل 5 با استفاده از gii .در هر صورت ما این کارها رو دوباره برای شما انجام خواهیم داد ولی بدون تصویر.
ساختن مدل کلاس برای issue
در صفحه ظاهر شده قسمت prefix پر شده است اونجا رو بیخیال شید و فیلد table name رو با نام جدول tbl_issue پر کنید ، قسمت بعد که نام مدل کلاس است نیز به طور خودکار پر خواهد شد. بعد از پر کردن تمام فیلدها دکمه prevew رو بزنید مراحل دیگر مثل قبل است.همونطور که میبینید آدرسی نشون میده که میگه این فایل ساخته شده در کجا قرار دارد . کدهای ساخته شده در این فایل به شرح زیر می باشد:
<?php
/**
* This is the model class for table "tbl_issue".
*/
class Issue extends CActiveRecord
{
/**
* The followings are the available columns in table 'tbl_issue':
* @var integer $id
* @var string $name
* @var string $description
* @var integer $project_id
* @var integer $type_id
* @var integer $status_id
* @var integer $owner_id
* @var integer $requester_id
* @var string $create_time
* @var integer $create_user_id
* @var string $update_time
* @var integer $update_user_id
*/
/**
* Returns the static model of the specified AR class.
* @return Issue the static model class
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'tbl_issue';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('name', 'required'),
array('project_id, type_id, status_id, owner_id, requester_id,
create_user_id, update_user_id', 'numerical', 'integerOnly'=>true),
array('name', 'length', 'max'=>256),
array('description', 'length', 'max'=>2000),
array('create_time, update_time', 'safe'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, name, description, project_id, type_id, status_id,
owner_id, requester_id, create_time, create_user_id, update_time,
update_user_id', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'owner' => array(self::BELONGS_TO, 'User', 'owner_id'),
'project' => array(self::BELONGS_TO, 'Project', 'project_id'),
'requester' => array(self::BELONGS_TO, 'User', 'requester_id'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'name' => 'Name',
'description' => 'Description',
'project_id' => 'Project',
'type_id' => 'Type',
'status_id' => 'Status',
'owner_id' => 'Owner',
'requester_id' => 'Requester',
'create_time' => 'Create Time',
'create_user_id' => 'Create User',
'update_time' => 'Update Time',
'update_user_id' => 'Update User',
);
}
/**
* Retrieves a list of models based on the current search/filter
conditions.
* @return CActiveDataProvider the data provider that can return the
models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes
that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('name',$this->name,true);
$criteria->compare('description',$this->description,true);
$criteria->compare('project_id',$this->project_id);
$criteria->compare('type_id',$this->type_id);
$criteria->compare('status_id',$this->status_id);
$criteria->compare('owner_id',$this->owner_id);
$criteria->compare('requester_id',$this->requester_id);
$criteria->compare('create_time',$this->create_time,true);
$criteria->compare('create_user_id',$this->create_user_id);
$criteria->compare('update_time',$this->update_time,true);
$criteria->compare('update_user_id',$this->update_user_id);
return new CActiveDataProvider(get_class($this), array(
'criteria'=>$criteria,
));
}
}