Commit a38e26b3 by dianc

Category Migrations and Models

parent 1fd46aee
...@@ -20,10 +20,10 @@ TEST_DB_PASSWORD = root ...@@ -20,10 +20,10 @@ TEST_DB_PASSWORD = root
# Urls # Urls
# ---- # ----
FRONTEND_URL = http://libromi-web.en FRONTEND_URL = http://libromi-web.yii
USERPANEL_URL = http://userpanel.libromi-web.en USERPANEL_URL = http://userpanel.libromi-web.yii
BACKEND_URL = http://admin.libromi-web.en BACKEND_URL = http://admin.libromi-web.yii
STORAGE_URL = http://storage.libromi-web.en STORAGE_URL = http://storage.libromi-web.yii
# Other # Other
# ----- # -----
......
<?php
namespace backend\modules\theme;
/**
* theme module definition class
*/
class Module extends \yii\base\Module
{
/**
* @inheritdoc
*/
public $controllerNamespace = 'backend\modules\theme\controllers';
/**
* @inheritdoc
*/
public function init()
{
parent::init();
// custom initialization code goes here
}
}
<?php
namespace backend\modules\theme\controllers;
use yii\web\Controller;
/**
* Default controller for the `theme` module
*/
class DefaultController extends Controller
{
/**
* Renders the index view for the module
* @return string
*/
public function actionIndex()
{
return $this->render('index');
}
}
<div class="theme-default-index">
<h1><?= $this->context->action->uniqueId ?></h1>
<p>
This is the view content for action "<?= $this->context->action->id ?>".
The action belongs to the controller "<?= get_class($this->context) ?>"
in the "<?= $this->context->module->id ?>" module.
</p>
<p>
You may customize this page by editing the following file:<br>
<code><?= __FILE__ ?></code>
</p>
</div>
<?php
use yii\db\Migration;
class m160830_105840_category extends Migration
{
/* public function up()
{
}
public function down()
{
echo "m160830_105840_category cannot be reverted.\n";
return false;
}*/
public function safeUp()
{
$tableOptions = null;
if ($this->db->driverName === 'mysql') {
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
}
$this->createTable('{{%category}}', [
'id' => $this->primaryKey(),
'name' => $this->string(32)->notNull(),
'slug' => $this->string(32)->notNull(),
'description' => $this->string(512),
'image_url' => $this->string(1024),
'image_path' => $this->string(1024),
'status' => $this->smallInteger()->notNull(),
//->defaultValue(Category::STATUS_ACTIVE),
'created_at' => $this->integer(),
'updated_at' => $this->integer()
], $tableOptions);
$this->createTable('{{%category_files}}', [
'id' => $this->primaryKey(),
'category_id' => $this->integer()->notNull(),
'code' => $this->string(32)->notNull(),
'type' => $this->string(32),
'name' => $this->string(32),
'created_at' => $this->integer(),
'updated_at' => $this->integer()
], $tableOptions);
$this->addForeignKey('fk_category', '{{%category_files}}', 'category_id', '{{%category}}', 'id', 'cascade', 'cascade');
}
public function safeDown()
{
$this->dropForeignKey('fk_category', '{{%category_files}}');
$this->dropTable('{{%category_files}}');
$this->dropTable('{{%category}}');
}
}
<?php
namespace common\models;
use Yii;
/**
* This is the model class for table "{{%category}}".
*
* @property integer $id
* @property string $name
* @property string $slug
* @property string $description
* @property string $image_url
* @property string $image_path
* @property integer $status
* @property integer $created_at
* @property integer $updated_at
*
* @property CategoryFiles[] $categoryFiles
*/
class Category extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%category}}';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['name', 'slug', 'status'], 'required'],
[['status', 'created_at', 'updated_at'], 'integer'],
[['name', 'slug'], 'string', 'max' => 32],
[['description'], 'string', 'max' => 512],
[['image_url', 'image_path'], 'string', 'max' => 1024],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('app', 'ID'),
'name' => Yii::t('app', 'Name'),
'slug' => Yii::t('app', 'Slug'),
'description' => Yii::t('app', 'Description'),
'image_url' => Yii::t('app', 'Image Url'),
'image_path' => Yii::t('app', 'Image Path'),
'status' => Yii::t('app', 'Status'),
'created_at' => Yii::t('app', 'Created At'),
'updated_at' => Yii::t('app', 'Updated At'),
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getCategoryFiles()
{
return $this->hasMany(CategoryFiles::className(), ['category_id' => 'id']);
}
/**
* @inheritdoc
* @return CategoryQuery the active query used by this AR class.
*/
public static function find()
{
return new CategoryQuery(get_called_class());
}
}
<?php
namespace common\models;
/**
* This is the ActiveQuery class for [[Category]].
*
* @see Category
*/
class CategoryQuery extends \yii\db\ActiveQuery
{
/*public function active()
{
return $this->andWhere('[[status]]=1');
}*/
/**
* @inheritdoc
* @return Category[]|array
*/
public function all($db = null)
{
return parent::all($db);
}
/**
* @inheritdoc
* @return Category|array|null
*/
public function one($db = null)
{
return parent::one($db);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment