$sql = Wallet::find()
->where(['id' => $id])
->createCommand()
->getRawSql();
/** @var Wallet $model */
$model = Wallet::findBySql($sql . ' FOR UPDATE')->one();
$model->amount += $amount;
$model->save(false);
return [
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'attributes' => [
PDO::ATTR_PERSISTENT => true
]
],
],
];
public function behaviors()
{
return [
[
'class' => CorrectDateBehavior::class,
'format' => 'datetime',
'attributes' => [
'dateFrom' => 'date_from',
'dateTo' => 'date_to',
]
]
];
}
$model = new Post();
$model->date_from = 1524775512;
echo $model->dateFrom; // 26.4.2018 20:45:12
$model->dateFrom = '01.05.2018';
echo $model->date_from; // 1515110400
/**
* @see https://tech.yandex.ru/speechkit/cloud/doc/guide/concepts/tts-http-request-docpage/
*/
$apiUrl = 'https://tts.voicetech.yandex.net/generate';
$options = [
'key' => '******',
'speaker' => 'oksana',
'format' => 'mp3',
'lang' => 'ru-RU',
'speed' => 1.2
];
$options['text'] = $text;
$url = $apiUrl . '?' . http_build_query($options);
$data = file_get_contents($url);
file_put_contents('audio.' . $options['format'], $data);
public function rules()
{
return array_merge(parent::rules(), [
// ...
]);
}
"require": {
"yiisoft/yii2-dev": "dev-myfix"
},
Но поля приходится задавать таким вот нестандартным способом, а хотелось бы для экономии времени сперва создать класс, задать в нем поля таблицы в виде полей класса, а затем вызвать команду, которая по этому классу создаст таблицу.
namespace app;
use yii\base\Object;
use yii\db\ActiveRecord;
/**
* @property integer $mifar_card_id
* @property integer $car_id
* @property integer $driver_id
* @property Car $car
* @property Driver $driver
* @property TariffInterface $tariff
*/
class DriverCar extends ActiveRecord
{
public function getCar()
{
return $this->hasOne(Car::class, ['id' => 'car_id']);
}
public function getDriver()
{
return $this->hasOne(Driver::class, ['id' => 'driver_id']);
}
public function getTariff()
{
return $this->car->tariff ?: $this->driver->tariff;
}
}
interface chargeableInterface
{
public function getTariff();
}
/**
* @property integer $group_tariff_id
* @property GroupTariff $tariff
* @property string $model
* @property string $color
* @property string $number
*/
class Car extends ActiveRecord implements chargeableInterface
{
public function getTariff()
{
return $this->hasOne(GroupTariff::class, ['id' => 'group_tariff_id']);
}
public function getCarBrand()
{
}
}
/**
* @property DriverTariff $tariff
* @property string $serial_number
*/
class Driver extends ActiveRecord implements chargeableInterface
{
public function getTariff()
{
return $this->hasOne(DriverTariff::class, ['driver_id' => 'id']);
}
public function getFullName()
{
}
}
interface TariffInterface
{
public function getTown();
public function getTownCenter();
public function getKmPrice();
}
/**
* @property integer $driver_id
*/
class DriverTariff extends ActiveRecord implements TariffInterface
{
public function getTown()
{
}
public function getTownCenter()
{
}
public function getKmPrice()
{
}
}
class GroupTariff extends ActiveRecord implements TariffInterface
{
public function getTown()
{
}
public function getTownCenter()
{
}
public function getKmPrice()
{
}
}
class DriverCarInfo extends Object
{
/**
* @var DriverCar|null
*/
private $_model;
public function setModel(DriverCar $model)
{
$this->_model = $model;
}
public function getTown()
{
return $this->_model->tariff->getTown();
}
public function getTownCenter()
{
return $this->_model->tariff->getTownCenter();
}
public function getKmPrice()
{
return $this->_model->tariff->getKmPrice();
}
public function getCompany()
{
return $this->_model->car->getCarBrand();
}
public function getModel()
{
return $this->_model->car->model;
}
public function getColor()
{
return $this->_model->car->color;
}
public function getNumber()
{
return $this->_model->car->number;
}
public function getFullName()
{
return $this->_model->driver->getFullName();
}
public function getSerialNumber()
{
return $this->_model->driver->serial_number;
}
}
function getFullInfo($id)
{
$model = DriverCar::findOne(['mifar_card_id' => $id]);
return $model === null ? null : new DriverCarInfo(['model' => $model]);
}
GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'title:html',
'description:html',
]
])