1. Ricardo Obregón
  2. AweCrud

Commits

robr...@localhost  committed 4fc7159

CTimestampBehavior is now optional.

  • Participants
  • Parent commits 770940c
  • Branches default

Comments (0)

Files changed (5)

File CHANGELOG

View file
  • Ignore whitespace
 AweCrud Change Log
 ===============
 
-Version 0.14, 2013-05-31
+Version 0.14, 2013-06-14
 * Fixed bug when assigning MANY_MANY relations when creating/updating models
+* Added image upload fields detection (by column names) with the corresponding validation rules generation. (Thanks to @inbedder)
+* Added validation rules indention (1 row - properties names, 2 row - validator name, n - validator options). (Thanks to @inbedder)
+* Added "common.models" searching (for autocomplete in Boilerplate structure). (Thanks to @inbedder)
+* Added file field generation (for image-like field names). (Thanks to @inbedder)
+* CTimestampBehavior is optional now.
 
 Version 0.13, 2013-05-03
 * Added suport to MANY_MANY relations (even if no 3rd party extension is provided)

File README.md

View file
  • Ignore whitespace
 #AweCrud Readme
-AweCrud version 0.13
+AweCrud version 0.14
 
 AweCrud official website:
 https://bitbucket.org/robregonm/awecrud

File generators/AweModel/AweModelCode.php

View file
  • Ignore whitespace
         'file_name'
     );
     public $time_fields;
+    public $useTimestampBehavior = 1;
+
     public $validRelatedRecordBehaviors = array(
         'ActiveRecordRelation' => 'EActiveRecordRelationBehavior',
     );
         return array_merge(
             parent::rules(),
             array(
-                array('relatedRecordBehavior', 'safe'),
+                array('relatedRecordBehavior, useTimestampBehavior', 'safe'),
             )
         );
     }
 
-    public function hasActiveBehavior(){
+    public function hasActiveBehavior()
+    {
 
     }
 
             if ($r and !in_array($column->name, $this->time_fields)) {
                 $required[] = $column->name;
             }
-            if(in_array($column->name, $this->imageFields)) {
-                $file[]=$column->name;
+            if (in_array($column->name, $this->imageFields)) {
+                $file[] = $column->name;
             } elseif ($column->type === 'integer') {
                 $integers[] = $column->name;
             } elseif ($column->type === 'double') {
             }
         }
         if ($required !== array()) {
-            $rules[] = "array(\t'" . implode(', ', $required) . "',\n\t\t\t\t\t'required',\n\t\t\t\t\t'message' => Yii::t('app', 'Field is required')\n\t\t\t)";
+            $rules[] = "array(\t'" . implode(
+                    ', ',
+                    $required
+                ) . "',\n\t\t\t\t\t'required',\n\t\t\t\t\t'message' => Yii::t('app', 'Field is required')\n\t\t\t)";
         }
         if ($integers !== array()) {
             $rules[] = "array(\t'" . implode(', ', $integers) . "',\n\t\t\t\t\t'numerical',\n\t\t\t\t\t'integerOnly'=>true\n\t\t\t)";
         }
         if ($length !== array()) {
             foreach ($length as $len => $cols) {
-                $rules[] = "array(\t'" . implode(', ', $cols) . "',\n\t\t\t\t\t'length',\n\t\t\t\t\t'max'=>$len,\n\t\t\t\t\t'tooLong' => Yii::t('app', 'Field is required')\n\t\t\t)";
+                $rules[] = "array(\t'" . implode(
+                        ', ',
+                        $cols
+                    ) . "',\n\t\t\t\t\t'length',\n\t\t\t\t\t'max'=>$len,\n\t\t\t\t\t'tooLong' => Yii::t('app', 'Field is required')\n\t\t\t)";
             }
         }
-        if($file !== array()) {
-            $rules[]="array(\t'".implode(', ',$file)."',\n\t\t\t\t\t'file',\n\t\t\t\t\t'maxSize' => Yii::app()->params['imageMaxSize'],\n\t\t\t\t\t'maxFiles' => 50,\n\t\t\t\t\t'tooLarge' => Yii::app()->params['messageTooLarge'],\n\t\t\t\t\t'types' => Yii::app()->params['imageGoodType'],\n\t\t\t\t\t'wrongType' => Yii::app()->params['imageGoodType'],\n\t\t\t\t\t'allowEmpty' => true,\n\t\t\t)";
+        if ($file !== array()) {
+            $rules[] = "array(\t'" . implode(
+                    ', ',
+                    $file
+                ) . "',\n\t\t\t\t\t'file',\n\t\t\t\t\t'maxSize' => Yii::app()->params['imageMaxSize'],\n\t\t\t\t\t'maxFiles' => 50,\n\t\t\t\t\t'tooLarge' => Yii::app()->params['messageTooLarge'],\n\t\t\t\t\t'types' => Yii::app()->params['imageGoodType'],\n\t\t\t\t\t'wrongType' => Yii::app()->params['imageGoodType'],\n\t\t\t\t\t'allowEmpty' => true,\n\t\t\t)";
         }
         if ($safe !== array()) {
             $rules[] = "array(\t'" . implode(', ', $safe) . "',\n\t\t\t\t\t'safe'\n\t\t\t)";
         }
         if ($null !== array()) {
             $rules[] = "array('" . implode(
-                ', ',
-                $null
-            ) . "',\n\t\t\t\t\t'default',\n\t\t\t\t\t'setOnEmpty' => true,\n\t\t\t\t\t'value' => null\n\t\t\t)";
+                    ', ',
+                    $null
+                ) . "',\n\t\t\t\t\t'default',\n\t\t\t\t\t'setOnEmpty' => true,\n\t\t\t\t\t'value' => null\n\t\t\t)";
         }
 
         return array_merge($this->parentGenerateRules($table), $rules);
             // First we look for a string, not null, not pk, not fk column, not original number on db.
             foreach ($columns as $name => $column) {
                 if ($column->type === 'string' && !$column->allowNull && !$column->isPrimaryKey && !$column->isForeignKey && stripos(
-                    $column->dbType,
-                    'int'
-                ) === false
+                        $column->dbType,
+                        'int'
+                    ) === false
                 ) {
                     return $name;
                 }
             // Then a string, not null, not fk column, not original number on db.
             foreach ($columns as $name => $column) {
                 if ($column->type === 'string' && !$column->allowNull && !$column->isForeignKey && stripos(
-                    $column->dbType,
-                    'int'
-                ) === false
+                        $column->dbType,
+                        'int'
+                    ) === false
                 ) {
                     return $name;
                 }
         }
         return 'null';
     }
-    
+
     public function hasUpload($table)
     {
-        foreach ($table->columns as $name=>$col)
-        {
-            if (in_array($name, $this->imageFields))
-            {
+        foreach ($table->columns as $name => $col) {
+            if (in_array($name, $this->imageFields)) {
                 return $name;
                 break;
             }

File generators/AweModel/templates/default/_base/basemodel.php

View file
  • Ignore whitespace
             ),
 <?php endif; ?>
 <?php foreach ($columns as $name => $column): ?>
-<?php if (in_array($column->name, $this->time_fields)): ?>
+<?php if ($this->useTimestampBehavior and in_array($column->name, $this->time_fields)): ?>
             'CTimestampBehavior' => array(
                 'class' => 'zii.behaviors.CTimestampBehavior',
                 'createAttribute' => <?php echo $this->getCreatetimeAttribute($columns) ?>,

File generators/AweModel/views/index.php

View file
  • Ignore whitespace
 <?php
 /** @var AweModelCode $model */
-$class=get_class($model);
-Yii::app()->clientScript->registerScript('gii.model',"
+$class = get_class($model);
+Yii::app()->clientScript->registerScript(
+    'gii.model',
+    "
 $('#{$class}_connectionId').change(function(){
 	var tableName=$('#{$class}_tableName');
 	tableName.autocomplete('option', 'source', []);
 	$.ajax({
-		url: '".Yii::app()->getUrlManager()->createUrl('gii/model/getTableNames')."',
+		url: '" . Yii::app()->getUrlManager()->createUrl('gii/model/getTableNames') . "',
 		data: {db: this.value},
 		dataType: 'json'
 	}).done(function(data){
 	}
 });
 $('.form .row.model-class').toggle($('#{$class}_tableName').val().substring($('#{$class}_tableName').val().length-1)!='*');
-");
+"
+);
 ?>
-<h1>AweModel Generator</h1>
+    <h1>AweModel Generator</h1>
 
-<p>This generator generates a model class for the specified database table.</p>
+    <p>This generator generates a model class for the specified database table.</p>
 
 <?php
 /** @var CCodeForm $form */
-$form=$this->beginWidget('CCodeForm', array('model'=>$model));
+$form = $this->beginWidget('CCodeForm', array('model' => $model));
 ?>
 
     <div class="row sticky">
-        <?php echo $form->labelEx($model, 'connectionId')?>
-        <?php echo $form->textField($model, 'connectionId', array('size'=>65))?>
+        <?php echo $form->labelEx($model, 'connectionId') ?>
+        <?php echo $form->textField($model, 'connectionId', array('size' => 65)) ?>
         <div class="tooltip">
-        The database component that should be used.
+            The database component that should be used.
         </div>
-        <?php echo $form->error($model,'connectionId'); ?>
+        <?php echo $form->error($model, 'connectionId'); ?>
     </div>
-	<div class="row sticky">
-		<?php echo $form->labelEx($model,'tablePrefix'); ?>
-		<?php echo $form->textField($model,'tablePrefix', array('size'=>65)); ?>
-		<div class="tooltip">
-		This refers to the prefix name that is shared by all database tables.
-		Setting this property mainly affects how model classes are named based on
-		the table names. For example, a table prefix <code>tbl_</code> with a table name <code>tbl_post</code>
-		will generate a model class named <code>Post</code>.
-		<br/>
-		Leave this field empty if your database tables do not use common prefix.
-		</div>
-		<?php echo $form->error($model,'tablePrefix'); ?>
-	</div>
-	<div class="row">
-		<?php echo $form->labelEx($model,'tableName'); ?>
-		<?php $form->widget('zii.widgets.jui.CJuiAutoComplete', array(
-			'model'=>$model,
-			'attribute'=>'tableName',
-            'name'=>'tableName',
-			'source'=>Yii::app()->hasComponent($model->connectionId) ? array_keys(Yii::app()->{$model->connectionId}->schema->getTables()) : array(),
-			'options'=>array(
-                'minLength'=>'0',
-                'focus'=>new CJavaScriptExpression('function(event,ui) {
-					$("#'.CHtml::activeId($model,'tableName').'").val(ui.item.label);
+    <div class="row sticky">
+        <?php echo $form->labelEx($model, 'tablePrefix'); ?>
+        <?php echo $form->textField($model, 'tablePrefix', array('size' => 65)); ?>
+        <div class="tooltip">
+            This refers to the prefix name that is shared by all database tables.
+            Setting this property mainly affects how model classes are named based on
+            the table names. For example, a table prefix <code>tbl_</code> with a table name <code>tbl_post</code>
+            will generate a model class named <code>Post</code>.
+            <br/>
+            Leave this field empty if your database tables do not use common prefix.
+        </div>
+        <?php echo $form->error($model, 'tablePrefix'); ?>
+    </div>
+    <div class="row">
+        <?php echo $form->labelEx($model, 'tableName'); ?>
+        <?php $form->widget(
+            'zii.widgets.jui.CJuiAutoComplete',
+            array(
+                'model' => $model,
+                'attribute' => 'tableName',
+                'name' => 'tableName',
+                'source' => Yii::app()->hasComponent($model->connectionId) ? array_keys(
+                    Yii::app()->{$model->connectionId}->schema->getTables()
+                ) : array(),
+                'options' => array(
+                    'minLength' => '0',
+                    'focus' => new CJavaScriptExpression('function(event,ui) {
+					$("#' . CHtml::activeId($model, 'tableName') . '").val(ui.item.label);
 					return false;
 				}')
-			),
-            'htmlOptions'=>array(
-                'id'=>CHtml::activeId($model,'tableName'),
-                'size'=>'65'
+                ),
+                'htmlOptions' => array(
+                    'id' => CHtml::activeId($model, 'tableName'),
+                    'size' => '65'
+                ),
+            )
+        );
+        ?>
+        <div class="tooltip">
+            This refers to the table name that a new model class should be generated for
+            (e.g. <code>tbl_user</code>). It can contain schema name, if needed (e.g. <code>public.tbl_post</code>).
+            You may also enter <code>*</code> (or <code>schemaName.*</code> for a particular DB schema)
+            to generate a model class for EVERY table.
+        </div>
+        <?php echo $form->error($model, 'tableName'); ?>
+    </div>
+    <div class="row model-class">
+        <?php echo $form->label($model, 'modelClass', array('required' => true)); ?>
+        <?php echo $form->textField($model, 'modelClass', array('size' => 65)); ?>
+        <div class="tooltip">
+            This is the name of the model class to be generated (e.g. <code>Post</code>, <code>Comment</code>).
+            It is case-sensitive.
+        </div>
+        <?php echo $form->error($model, 'modelClass'); ?>
+    </div>
+    <div class="row sticky">
+        <?php echo $form->labelEx($model, 'baseClass'); ?>
+        <?php echo $form->textField($model, 'baseClass', array('size' => 65)); ?>
+        <div class="tooltip">
+            This is the class that the new model class will extend from.
+            Please make sure the class exists and can be autoloaded.
+        </div>
+        <?php echo $form->error($model, 'baseClass'); ?>
+    </div>
+    <div class="row sticky">
+        <?php echo $form->labelEx($model, 'modelPath'); ?>
+        <?php echo $form->textField($model, 'modelPath', array('size' => 65)); ?>
+        <div class="tooltip">
+            This refers to the directory that the new model class file should be generated under.
+            It should be specified in the form of a path alias, for example, <code>application.models</code>.
+        </div>
+        <?php echo $form->error($model, 'modelPath'); ?>
+    </div>
+    <div class="row sticky">
+        <?php echo $form->labelEx($model, 'useTimestampBehavior'); ?>
+        <?php echo $form->dropDownList($model, 'useTimestampBehavior', array(0 => 'No', 1 => 'Yes')); ?>
+        <div class="tooltip">
+            This refers to the directory that the new model class file should be generated under.
+            It should be specified in the form of a path alias, for example, <code>application.models</code>.
+        </div>
+        <?php echo $form->error($model, 'useTimestampBehavior'); ?>
+    </div>
+    <div class="row sticky">
+        <?php echo $form->labelEx($model, 'relatedRecordBehavior') ?>
+        <?php echo $form->dropDownList(
+            $model,
+            'relatedRecordBehavior',
+            array(
+                'ActiveRecordRelation' => 'ActiveRecordRelation'
             ),
-		));
-		?>
-		<div class="tooltip">
-		This refers to the table name that a new model class should be generated for
-		(e.g. <code>tbl_user</code>). It can contain schema name, if needed (e.g. <code>public.tbl_post</code>).
-		You may also enter <code>*</code> (or <code>schemaName.*</code> for a particular DB schema)
-		to generate a model class for EVERY table.
-		</div>
-		<?php echo $form->error($model,'tableName'); ?>
-	</div>
-    <div class="row model-class">
-		<?php echo $form->label($model,'modelClass',array('required'=>true)); ?>
-		<?php echo $form->textField($model,'modelClass', array('size'=>65)); ?>
-		<div class="tooltip">
-		This is the name of the model class to be generated (e.g. <code>Post</code>, <code>Comment</code>).
-		It is case-sensitive.
-		</div>
-		<?php echo $form->error($model,'modelClass'); ?>
-	</div>
-	<div class="row sticky">
-		<?php echo $form->labelEx($model,'baseClass'); ?>
-		<?php echo $form->textField($model,'baseClass',array('size'=>65)); ?>
-		<div class="tooltip">
-			This is the class that the new model class will extend from.
-			Please make sure the class exists and can be autoloaded.
-		</div>
-		<?php echo $form->error($model,'baseClass'); ?>
-	</div>
-	<div class="row sticky">
-		<?php echo $form->labelEx($model,'modelPath'); ?>
-		<?php echo $form->textField($model,'modelPath', array('size'=>65)); ?>
-		<div class="tooltip">
-			This refers to the directory that the new model class file should be generated under.
-			It should be specified in the form of a path alias, for example, <code>application.models</code>.
-		</div>
-		<?php echo $form->error($model,'modelPath'); ?>
-	</div>
-    <div class="row sticky">
-        <?php echo $form->labelEx($model, 'relatedRecordBehavior')?>
-        <?php echo $form->dropDownList($model, 'relatedRecordBehavior', array(
-            'ActiveRecordRelation' => 'ActiveRecordRelation'
-        ), array('empty'=>'[None]'))?>
+            array('empty' => '[None]')
+        )?>
         <div class="tooltip">
             Third party behavior that allows to save related records when creating or updating.
         </div>
-        <?php echo $form->error($model,'relatedRecordBehavior'); ?>
+        <?php echo $form->error($model, 'relatedRecordBehavior'); ?>
     </div>
 
 <?php $this->endWidget(); ?>