yii2

Yii2 model saves null values

怎甘沉沦 提交于 2019-12-24 09:39:09
问题 I'm using REST POST request to create new row in my db, but it saves me null values instead values which I sent. So I tried to send PUT request to modify existing data, but it returned not modified row (like when I use GET). Actually, GET request works fine CountryController.php : <?php namespace app\controllers; use yii\rest\ActiveController; class CountryController extends ActiveController { public $modelClass = 'app\models\Country'; } Model Country.php : I tried to do this with another

How to write where Between query in yii2

冷暖自知 提交于 2019-12-24 09:26:17
问题 I am passing $start_date and $end_date as parameter to compare them with created field in MySQL database table. I am using yii2 framework. Here is what I tried: $modelStockDetails=StockDetails::find()->where(['BETWEEN', 'created', $start_date, $end_date])->andwhere(['receiving_order_id' =>$modelRecevingOrder->id,'deleted' => 'N'])->all(); which returns an empty array when values $start_date and $end_date are different from created date in the table. But it returns an array containing the data

Yii2 How to translate SUM SQL function to Query Builder?

这一生的挚爱 提交于 2019-12-24 09:26:03
问题 I have this simple SQL query: SELECT product_name, SUM (product_amount) FROM orders GROUP BY product_name; It will show a list with product names and their amounts. Like this example: I want to translate it to Yii2 Query Builder. I don't know how to use the SUM function. I tried this but it didn't work: Orders::find() ->select( [ Orders::tableName() . ".product_name", (new \yii\db\Query())->sum(Orders::tableName() . ".product_amount") ] ) ->groupBy( [ Orders::tableName() . '.product_name',

yii2 detailview conditional row class

China☆狼群 提交于 2019-12-24 09:18:49
问题 I would like to change class for one single attribute in detailview, based on a condition: If I wouldn't want to make it conditional, it would be working like so: [ 'attribute' => 'ungueltig', 'format' => 'boolean', 'contentOptions' => [ 'class' => 'danger', ] ], I want this one to change to conditional, and I have tried a lot of different ways, e.g.: [ 'attribute' => 'ungueltig', 'format' => 'boolean', 'contentOptions' => function ($model) { if ($model->ungueltig == 1) { return ['class' =>

Yii2 routing with get parameters only ( hidden controller and action )

夙愿已清 提交于 2019-12-24 08:57:48
问题 I saw routing where the controller and the action are hidden and url is constructed like www.domain.com/en/page-33/category-28/product-89?param=some_param . In this routing when I try to get the parameters with var_dump(Yii::$app->getRequest()->getQueryParams()) I got array like so : array(4) { ["first_step"]=> string(7) "page-33" ["second_step"]=> string(11) "category-28" ['product']=> string(10) "product-89" ['param']=> string(10) "some_param"} How can I do that ? I saw the rules and they

Yii2 custom client validation with ajax rendering in modal

拈花ヽ惹草 提交于 2019-12-24 08:25:00
问题 I have a model with a custom validation method. For testing it always returns an error message. public function rules() { return [ ... ['staff_ids', 'each', 'rule' => ['string']], [['staff_ids'], 'validateStaffIds'], ... ]; } public function validateStaffIds($attribute, $params, $validator) { $this->addError($attribute, 'There is an error in the staff ids'); } In the view.php is the modal element <p> <?= Html::button('Add Ensemble Staff', ['value' => Url::to(['ensemble/add', 'id' => $model-

Write info message in app.log file using yii2

泪湿孤枕 提交于 2019-12-24 08:10:55
问题 I want to write info message in app.log file for debugging. For that I am using that code in web.php file:- log' => [ 'traceLevel' => YII_DEBUG ? 3 : 0, 'targets' => [ [ 'class' => 'yii\log\FileTarget', 'levels' => ['error', 'warning'], ], [ 'class' => 'yii\log\FileTarget', 'levels' => ['info'], 'categories' => ['orders'], 'logFile' => '@app/runtime/logs/requests.log', 'maxFileSize' => 1024 * 2, 'maxLogFiles' => 20, ], [ 'class' => 'yii\log\FileTarget', 'levels' => ['info'], 'categories' => [

how to get email field of contact us form in the email which admin got (yii2)

≯℡__Kan透↙ 提交于 2019-12-24 07:55:00
问题 I am using yii2 advanced contact us form. but unable to get email of user which is filled in email field of contact form.. public function sendEmail($email) { return Yii::$app->mailer->compose() ->setTo($email) ->setFrom([$this->email => $this->name]) //echo "emailll<pre>";print_r($this->email);exit; //here I am getting the correct email which is filled in contact form //but when I received mail. nowhere I got That email id... // In from: username@exmple.com which i set as username // In To :

how to get senders email Id in received mail from contact us form in yii2

非 Y 不嫁゛ 提交于 2019-12-24 07:48:15
问题 I am using yii2 default contact us page to get mail from user.. It is working fine but I am getting mail from email which I mention in code, to adminEmail which is also my email id. here is code=> public function sendEmail($email) { return Yii::$app->mailer->compose() ->setTo($email) ->setFrom([$this->email => $this->name])//want the data from email field..how to achieve that?? ->setSubject($this->subject) ->setTextBody($this->body) ->send(); } if I try like this ->setFrom([$this->email =>

Yii2 rule which would allow all the letters (including special ones), but without special characters?

↘锁芯ラ 提交于 2019-12-24 07:41:09
问题 I have a problem here. My username rule allowing to enter only letters without special characters, but could someone help me to make that it would allow letters (including special letters), but without special characters? F.e: Test - good, Tęęęst - good, Test2 - bad. My rule is: ['username', 'match', 'pattern' => '/[a-z]+$/', ] Thanks for any help 回答1: Use this but remember it does not work on the client side since JavaScript does not support unicode regex flags (at least without special