Email Id : phpmk888@gmail.com
Yii2 Dynamic Table name with multiple tables same structure..
I have implement the model for a similar occasion. See if my code will help you,
One Yii2 Model that uses multiple tables with the same structure.
The table names will change according to the new Action
In my model:
class PicklistModel extends \yii\db\ActiveRecord {
private static $tableName;
public function __construct($tableName = null, $scenario = 'insert') {
if ($tableName === null || $tableName === '') {
return false;
}
self::$tableName = $tableName;
//parent::__construct($scenario);
}
........>>
}
And that's the call model in controller...
$model = new PicklistModel("tableName");
$get = $model->find()->all();
$one = $model::findOne('condition');
Add Items Action In Controller .....
public function actionAddItem() {
if (Yii::$app->request->post('picklist_id')) {
$pick_model = new PicklistModel('TableName')';
if ($pick_model->load(Yii::$app->request->post()) && $pick_model->validate()) {
if ($pick_model->save()) {
return 'ok';
}
}
}
}
Update Item Action In Controller....
public function actionUpdateAjax($record_id) {
if (Yii::$app->request->isAjax) {
if ($record_id) {
$model_update = $pick_model::findOne($record_id);
if ($model_update->load(Yii::$app->request->post()) && $model_update->validate()) {
if ($model_update->save()) {
return 'ok';
}
} else {
echo $this->renderAjax('_update', array('pick_model' => $model_update));
}
}
}
}
ReplyDeleteIt is nice post and I found some interesting information on this blog, keep it up. Thanks for sharing. . .
Yii Framework Development Company – Nintriva