public $when = false;
['chat_id', 'required', 'when' => function($model) {
return $model->type == AuthItem::TYPE_PACKMAN && $model->when == true;
},'enableClientValidation' => false],botga request contact qilinganda messageni ichida text kelmay qoladi keyin bot qotib qoladi. shuning uchun textni ham issetga olib qo'yish kerak
if(isset($message->text)){
$text = $message->text;
}Migratsiyada meta-charsetni ham birgalikda korsatib ketish.
$this->createTable('category', [
'id' => $this->primaryKey(),
'title_uz' => $this->string()->notNull(),
'title_ru' => $this->string()->notNull(),
'status' => $this->integer()->notNull(),
],'CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci ENGINE=InnoDB');Agar yii2da malumotlarni bazaga emoji bilan tushurish kerak bolsa config/db.php charsetni utf8mb4 qilib qo'yish kerak
$model->text = strip_tags($model->text, '<b><strong><i><em><u><ins><s><strike><del><span><tg-spoiler><a><code><pre>');
Kelayotgan malumotlarda html teglar mavjud bolsa faqat shu teglarni qoldirib qolganini olib tashlaydihtml_entity_decode($string);
Matndan   va shunga o'xshagan elementlarni olib tashlaydi va decode holatga otkizadi\Yii::$app->view->registerLinkTag(['rel' => 'icon', 'type' => 'image/png', 'href' => Url::to(['/favicon.png'])]);Controllerdan turib browser tabga icon berish
'attribute' => 'status',yii2 gridviewda search collumni dropdownli qilish
'format' => 'raw',
'filter' => Html::activeDropDownList($searchModel, 'status', ['1' => 'O\'qilmagan', '0' => 'O\'qilgan'], ['class' => 'form-control', 'prompt' => 'Barchasi']),
hozirgi vaqtga yana 20 daqiqa qo'shish
$time = 20;
echo $time = date("H:i", strtotime('+ ' . $time . ' minute')) . ":00";before
public function insertMessage($response, $chat_id, $order_code_id)
{
$model = new MessageId();
$message_id = $response->result->message_id;
$model->message_id = $message_id;
$model->chat_id = $chat_id;
$model->order_code_id = $order_code_id;
return $model->save();
}
afterpublic function insertMessage($response, $chat_id, $order_code_id)
{
$model = new MessageId();
if ($response->ok === true) {
$message_id = $response->result->message_id;
$model->message_id = $message_id;
$model->chat_id = $chat_id;
$model->order_code_id = $order_code_id;
return $model->save();
}
}
foydalanuvchi botni bloklaganda responsedan message_idni topolmay qoladi shuning uchun response->ok dan true qaytsagina bu amallarni bajarish kerakForwarded from thelaziz
ahost da cron yozsa file ochib tashavotudi kamandelani shu kamandaga o'zgartirsela file ochib tashamasakan
wget -O /dev/null -q http://www.example.com/program/timecheck
yii2da updateAll funksiyasini ifni ichiga olish kerak emas sababi update qilishga malumot topolmasa return false qaytaradi
$clients = Clients::find()->select('id')->where(['not', ['chat_id' => null]]);
SELECT `id` FROM `clients` WHERE NOT (`chat_id` IS NULL)
SELECT `id` FROM `clients` WHERE `chat_id` IS NOT NULLhammasi bir hil natija beradi yani chat_id ni null emasligiga tekshiradi
if (count($array) > 0) {
// simple code...
}
if (!empty($array)) {
// simple code...
}
if (!!$array) {
// recommended code...
}@ operatori o'zgaruvchi yo'q elon qilinmagan bolsa yoki qiymatlanmagan bo'lsa null qaytaradi
Controllerdagi kod
Modeldagi kod
Jquerydagi kod
Ajax orqali file yuklanayotganda file malumotlari kelmay qoladi xattoki $_FILES o'zgaruvchisida ham shuning uchun manashu usulda malumotlarni saqlab olsa bo'ladi
public function actionCreate()
{
if (Yii::$app->request->isAjax) {
$model = new Product();
$product_price = new ProductPrice();
$currency = Currency::find()->where(['status' => Currency::STATUS_ACTIVE])->asArray()->all();
$currency_list = ArrayHelper::map($currency, 'id', 'title');
$category = Category::find()->where(['status' => Category::STATUS_ACTIVE])->asArray()->all();
$category_list = ArrayHelper::map($category, 'id', 'title');
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
if ($model->load(Yii::$app->request->post())) {
$model->imageFile = $_FILES;
if ($model->Upload() && $model->save()) return ['status' => 'success'];
}
return [
'status' => 'error',
'content' => $this->renderAjax('_form.php',[
'model' => $model,
'product_price' => $product_price,
'currency_list' => $currency_list,
'category_list' => $category_list,
]),
];
}
}Modeldagi kod
public function getExtension()
{
$extension = explode("/", $this->imageFile['imageFile']['type']);
return $extension[1];
}
public function Upload()
{
if ($this->validate()) {
$file_name = date("Y-m-d")."_".rand(1000, 9999) . '.' . $this->getExtension();
move_uploaded_file($this->imageFile['imageFile']['tmp_name'], 'uploads/images/' . $file_name);
$this->image = $file_name;
return true;
}
return false;
}Jquerydagi kod
$(document).on("click", "#insert-product-form",function(){
var data = new FormData();
var form = $('#product-create-form').serializeArray();
$.each(form, function (key, input) {
data.append(input.name, input.value);
});
var file_data = $("#product-imagefile").prop("files")[0];
data.append("imageFile", file_data);
console.log(data)
$.ajax({
url: '/product/create',
dataType: 'JSON',
type: 'post',
data: data,
contentType: false,
processData: false,
success: function(response){
if (response.status == 'error') {
$('#exampleModal .modal-title').html('Tovar qo\'shish').css({fontSize: 23})
$('#exampleModal .modal-body').html(response.content)
} else if (response.status == 'success') {
$.pjax.reload('#id-pjax')
$(".close").trigger("click");
}
}
});
});Ajax orqali file yuklanayotganda file malumotlari kelmay qoladi xattoki $_FILES o'zgaruvchisida ham shuning uchun manashu usulda malumotlarni saqlab olsa bo'ladi