Commits

christiansalazar committed 90bd33f

new: EYuiRelation demo

Comments (0)

Files changed (16)

protected/config/main.php

 		'application.models.*',
 		'application.components.*',
 		
-		'ext.EYui.*',
+		'ext.eyui.*',
 	),
 
 	'modules'=>array(
 			// enable cookie-based authentication
 			'allowAutoLogin'=>true,
 		),
+		
 		// uncomment the following to enable URLs in path-format
 		/*
 		'urlManager'=>array(

protected/controllers/ExampleController.php

 		$this->render('eyuiajaxaction'); 
 	}	
 	
+	public function actionEYuiRelation() 
+	{ 
+		$model = TblCompany::model()->findByPk(1);
+	
+		$this->render('eyuirelation',array('model'=>$model)); 
+	}	
+	
 	
 	public function actionDataExport($userid=null) {
 		

protected/data/eyuirelation-demo.sql

 INSERT INTO tbl_department (company_id,name) VALUES (2, 'reception');
 INSERT INTO tbl_department (company_id,name) VALUES (2, 'graphics');
 
-CREATE TABLE tbl_userjob (
+CREATE TABLE tbl_companyjob (
     id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
-	user_id INTEGER NOT NULL,
+	job_id INTEGER NOT NULL,
 	company_id INTEGER NOT NULL
 );
 
-CREATE TABLE tbl_userdepartment (
+CREATE TABLE tbl_companydepartment (
     id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
-	user_id INTEGER NOT NULL,
+	department_id INTEGER NOT NULL,
 	company_id INTEGER NOT NULL
 );

protected/data/testdrive.db

Binary file modified.

protected/models/TblCompany.php

  * @property string $name
  */
 class TblCompany extends CActiveRecord
+	implements EYuiRelationIMaster
 {
+	
+	public function eyuirelation_getPrimaryId() {
+		return $this->primarykey;
+	}
+
 	/**
 	 * Returns the static model of the specified AR class.
 	 * @param string $className active record class name.

protected/models/TblCompanyDepartment.php

+<?php
+
+/**
+ * This is the model class for table "tbl_companydepartment".
+ *
+ * The followings are the available columns in table 'tbl_companydepartment':
+ * @property integer $id
+ * @property integer $department_id
+ * @property integer $company_id
+ */
+class TblCompanyDepartment extends CActiveRecord
+	implements EYuiRelationIRelation
+{
+	public function eyuirelation_insert($widgetid,$masterPrimaryId, $optionPrimaryId){
+		// $masterPrimaryId is: company_id
+		// $optionPrimaryId is: department_id
+		$inst = new TblCompanyDepartment;
+		$inst->company_id = $masterPrimaryId;
+		$inst->department_id = $optionPrimaryId;
+		if($inst->insert()){
+			return $inst->id;
+		}
+		else
+		return null;
+	}
+	
+	public function eyuirelation_remove($widgetid,$primaryId){
+		$inst = self::findByPk($primaryId);
+		if($inst != null)
+			if($inst->delete())
+				return true;
+		return false;
+	}
+	
+	/**
+		must return a CHtml::listData
+	*/
+	public function eyuirelation_listData($widgetid,$masterPrimaryId){
+		$models = self::model()->findAllByAttributes(array('company_id'=>$masterPrimaryId));
+		$items = array();
+		foreach($models as $model)
+			$items[$model->id] = $model->department->name;
+		return $items;
+	}
+	
+
+	/**
+	 * Returns the static model of the specified AR class.
+	 * @param string $className active record class name.
+	 * @return TblCompanyDepartment 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_companydepartment';
+	}
+
+	/**
+	 * @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('department_id, company_id', 'required'),
+			array('department_id, company_id', 'numerical', 'integerOnly'=>true),
+			// The following rule is used by search().
+			// Please remove those attributes that should not be searched.
+			array('id, department_id, company_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(
+			'department' => array(self::BELONGS_TO, 'TblDepartment', 'department_id'),
+		);
+	}
+
+	/**
+	 * @return array customized attribute labels (name=>label)
+	 */
+	public function attributeLabels()
+	{
+		return array(
+			'id' => 'ID',
+			'department_id' => 'Department',
+			'company_id' => 'Company',
+		);
+	}
+
+	/**
+	 * 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('department_id',$this->department_id);
+		$criteria->compare('company_id',$this->company_id);
+
+		return new CActiveDataProvider($this, array(
+			'criteria'=>$criteria,
+		));
+	}
+}

protected/models/TblCompanyJob.php

+<?php
+
+/**
+ * This is the model class for table "tbl_companyjob".
+ *
+ * The followings are the available columns in table 'tbl_companyjob':
+ * @property integer $id
+ * @property integer $job_id
+ * @property integer $company_id
+ */
+class TblCompanyJob extends CActiveRecord
+	implements EYuiRelationIRelation
+{
+	public function eyuirelation_insert($widgetid,$masterPrimaryId, $optionPrimaryId){
+		// $masterPrimaryId is: company_id
+		// $optionPrimaryId is: job_id
+		$inst = new TblCompanyJob;
+		$inst->company_id = $masterPrimaryId;
+		$inst->job_id = $optionPrimaryId;
+		if($inst->insert()){
+			return $inst->id;
+		}
+		else
+		return null;
+	}
+	
+	public function eyuirelation_remove($widgetid,$primaryId){
+		$inst = self::findByPk($primaryId);
+		if($inst != null)
+			if($inst->delete())
+				return true;
+		return false;
+	}
+	
+	/**
+		must return a CHtml::listData
+	*/
+	public function eyuirelation_listData($widgetid,$masterPrimaryId){
+		$models = self::model()->findAllByAttributes(array('company_id'=>$masterPrimaryId));
+		$items = array();
+		foreach($models as $model)
+			$items[$model->id] = $model->job->name;
+		return $items;
+	}
+
+	/**
+	 * Returns the static model of the specified AR class.
+	 * @param string $className active record class name.
+	 * @return TblCompanyJob 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_companyjob';
+	}
+
+	/**
+	 * @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('job_id, company_id', 'required'),
+			array('job_id, company_id', 'numerical', 'integerOnly'=>true),
+			// The following rule is used by search().
+			// Please remove those attributes that should not be searched.
+			array('id, job_id, company_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(
+			'job' => array(self::BELONGS_TO, 'TblJob', 'job_id'),
+		);
+	}
+
+	/**
+	 * @return array customized attribute labels (name=>label)
+	 */
+	public function attributeLabels()
+	{
+		return array(
+			'id' => 'ID',
+			'job_id' => 'Job',
+			'company_id' => 'Company',
+		);
+	}
+
+	/**
+	 * 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('job_id',$this->job_id);
+		$criteria->compare('company_id',$this->company_id);
+
+		return new CActiveDataProvider($this, array(
+			'criteria'=>$criteria,
+		));
+	}
+}

protected/models/TblDepartment.php

  * @property string $name
  */
 class TblDepartment extends CActiveRecord
+	implements EYuiRelationIOptions
 {
+	public function eyuirelation_listData($widgetid,$primaryid) {
+		return CHtml::listData(
+		self::model()->findAllByAttributes(array('company_id'=>$primaryid)),'id','name');
+	}
+
+
 	/**
 	 * Returns the static model of the specified AR class.
 	 * @param string $className active record class name.

protected/models/TblJob.php

  * @property string $name
  */
 class TblJob extends CActiveRecord
+	implements EYuiRelationIOptions
 {
+	public function eyuirelation_listData($widgetid,$primaryid) {
+		return CHtml::listData(
+		self::model()->findAllByAttributes(array('company_id'=>$primaryid)),'id','name');
+	}
+
 	/**
 	 * Returns the static model of the specified AR class.
 	 * @param string $className active record class name.

protected/models/TblUserDepartment.php

-<?php
-
-/**
- * This is the model class for table "tbl_userdepartment".
- *
- * The followings are the available columns in table 'tbl_userdepartment':
- * @property integer $id
- * @property integer $user_id
- * @property integer $company_id
- */
-class TblUserDepartment extends CActiveRecord
-{
-	/**
-	 * Returns the static model of the specified AR class.
-	 * @param string $className active record class name.
-	 * @return TblUserDepartment 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_userdepartment';
-	}
-
-	/**
-	 * @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('user_id, company_id', 'required'),
-			array('user_id, company_id', 'numerical', 'integerOnly'=>true),
-			// The following rule is used by search().
-			// Please remove those attributes that should not be searched.
-			array('id, user_id, company_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(
-		);
-	}
-
-	/**
-	 * @return array customized attribute labels (name=>label)
-	 */
-	public function attributeLabels()
-	{
-		return array(
-			'id' => 'ID',
-			'user_id' => 'User',
-			'company_id' => 'Company',
-		);
-	}
-
-	/**
-	 * 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('user_id',$this->user_id);
-		$criteria->compare('company_id',$this->company_id);
-
-		return new CActiveDataProvider($this, array(
-			'criteria'=>$criteria,
-		));
-	}
-}

protected/models/TblUserJob.php

-<?php
-
-/**
- * This is the model class for table "tbl_userjob".
- *
- * The followings are the available columns in table 'tbl_userjob':
- * @property integer $id
- * @property integer $user_id
- * @property integer $company_id
- */
-class TblUserJob extends CActiveRecord
-{
-	/**
-	 * Returns the static model of the specified AR class.
-	 * @param string $className active record class name.
-	 * @return TblUserJob 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_userjob';
-	}
-
-	/**
-	 * @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('user_id, company_id', 'required'),
-			array('user_id, company_id', 'numerical', 'integerOnly'=>true),
-			// The following rule is used by search().
-			// Please remove those attributes that should not be searched.
-			array('id, user_id, company_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(
-		);
-	}
-
-	/**
-	 * @return array customized attribute labels (name=>label)
-	 */
-	public function attributeLabels()
-	{
-		return array(
-			'id' => 'ID',
-			'user_id' => 'User',
-			'company_id' => 'Company',
-		);
-	}
-
-	/**
-	 * 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('user_id',$this->user_id);
-		$criteria->compare('company_id',$this->company_id);
-
-		return new CActiveDataProvider($this, array(
-			'criteria'=>$criteria,
-		));
-	}
-}

protected/views/example/eyuiajaxaction.php

 <h1>EYuiAjaxAction</h1>
 <p>This widget invokes an Action via Ajax, please click the link below:</p>
 	<?php 
-		$this->widget('ext.EYui.EYuiAjaxAction'
+		$this->widget('ext.eyui.EYuiAjaxAction'
 			,array(
 				'id'=>'myId',
 				'action'=>array('site/Sample'),

protected/views/example/eyuiform.php

 <p>Current selected user is: <?php echo $user->username.",".$user->email; ?></p>
 
 <?php 
-	$this->widget('ext.EYui.EYuiForm',array(
+	$this->widget('ext.eyui.EYuiForm',array(
 		'id'=>'form1',
 		'label'=>'A Big Form',
 		'model'=>EYuiFormDb::newModel($user),

protected/views/example/eyuirelation.php

+<h1>EYuiRelation</h1>
+	<?php 
+		$this->widget('ext.eyui.EYuiRelation'
+			,array(
+				'id'=>'departmentrelationship',
+				'onError'=>'function(e){ $("#mylogger").html(e.responseText); }',
+				'model'=>$model,
+				'title'=>'Departments:<hr/>',
+				'optionsClassName'=>'TblDepartment',
+				'relationClassName'=>'TblCompanyDepartment',
+				'htmlOptions'=>array('style'=>'width: 300px; margin-bottom: 10px; margin-right: 10px; float: left;'),
+			)
+		);
+	?>
+	
+	<?php 
+		$this->widget('ext.eyui.EYuiRelation'
+			,array(
+				'id'=>'jobrelationship',
+				'onError'=>'function(e){ $("#mylogger").html(e.responseText); }',
+				'model'=>$model,
+				'title'=>'Jobs:<hr/>',
+				'optionsClassName'=>'TblJob',
+				'relationClassName'=>'TblCompanyJob',
+				'htmlOptions'=>array('style'=>'width: 300px; float: left;'),
+			)
+		);
+	?>	
+	
+	<div id='mylogger'></div>

protected/views/example/eyuisearch.php

 		</p>
 		<div style='width: 400px;'>
 		<?php 
-			$this->widget('ext.EYui.EYuiSearch'
+			$this->widget('ext.eyui.EYuiSearch'
 				,array(
 					'model'=>$model,
 					'attribute'=>'userid',

protected/views/site/index.php

 <?php echo CHtml::link("Test EYuiFormEditor",array('example/eyuiformeditor'));?>
 <br/>
 <?php echo CHtml::link("Test EYuiAjaxAction",array('example/eyuiajaxaction'));?>
+<br/>
+<?php echo CHtml::link("Test EYuiRelation",array('example/eyuirelation'));?>