本文整理汇总了PHP中yii\db\ActiveRecord::getBehaviors方法的典型用法代码示例。如果您正苦于以下问题:PHP ActiveRecord::getBehaviors方法的具体用法?PHP ActiveRecord::getBehaviors怎么用?PHP ActiveRecord::getBehaviors使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yii\db\ActiveRecord
的用法示例。
在下文中一共展示了ActiveRecord::getBehaviors方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: init
public function init()
{
parent::init();
if (empty($this->model)) {
throw new InvalidConfigException("Property {model} cannot be blank");
}
$hasFileBehavior = false;
foreach ($this->model->getBehaviors() as $behavior) {
if (is_a($behavior, FileBehavior::className())) {
$hasFileBehavior = true;
}
}
if (!$hasFileBehavior) {
throw new InvalidConfigException("The behavior {FileBehavior} has not been attached to the model.");
}
}
示例2: run
public function run()
{
if (!$this->model) {
return Html::tag('div', Html::tag('b', Yii::t('yii', 'Error')) . ': ' . $this->getModule()->t('attachments', 'The model cannot be empty.'), ['class' => 'alert alert-danger']);
}
$hasFileBehavior = false;
foreach ($this->model->getBehaviors() as $behavior) {
if (is_a($behavior, FileBehavior::className())) {
$hasFileBehavior = true;
}
}
if (!$hasFileBehavior) {
return Html::tag('div', Html::tag('b', Yii::t('yii', 'Error')) . ': ' . $this->getModule()->t('attachments', 'The behavior FileBehavior has not been attached to the model.'), ['class' => 'alert alert-danger']);
}
Url::remember(Url::current());
return GridView::widget(['dataProvider' => new ArrayDataProvider(['allModels' => $this->model->getFiles()]), 'layout' => '{items}', 'tableOptions' => $this->tableOptions, 'columns' => [['class' => 'yii\\grid\\SerialColumn'], ['label' => $this->getModule()->t('attachments', 'File name'), 'format' => 'raw', 'value' => function ($model) {
return Html::a("{$model->name}.{$model->type}", $model->getUrl());
}], ['class' => 'yii\\grid\\ActionColumn', 'template' => '{delete}', 'buttons' => ['delete' => function ($url, $model, $key) {
return Html::a('<span class="glyphicon glyphicon-trash"></span>', ['/attachments/file/delete', 'id' => $model->id], ['title' => Yii::t('yii', 'Delete'), 'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'), 'data-method' => 'post']);
}]]]]);
}
示例3: run
public function run()
{
if (!$this->model) {
return Html::tag('div', Html::tag('b', Yii::t('yii', 'Error')) . ': ' . $this->getModule()->t('attachments', 'The model cannot be empty.'), ['class' => 'alert alert-danger']);
}
$hasFileBehavior = false;
foreach ($this->model->getBehaviors() as $behavior) {
if ($behavior instanceof FileBehavior) {
$hasFileBehavior = true;
break;
}
}
if (!$hasFileBehavior) {
return Html::tag('div', Html::tag('b', Yii::t('yii', 'Error')) . ': ' . $this->getModule()->t('attachments', 'The behavior FileBehavior has not been attached to the model.'), ['class' => 'alert alert-danger']);
}
$confirm = Yii::t('yii', 'Are you sure you want to delete this item?');
$js = <<<JS
\$(".delete-button").click(function(){
var tr = this.closest('tr');
var url = \$(this).data('url');
if (confirm("{$confirm}")) {
\$.ajax({
method: "POST",
url: url,
success: function(data) {
if (data) {
tr.remove();
}
}
});
}
});
JS;
Yii::$app->view->registerJs($js);
return GridView::widget(['dataProvider' => new ArrayDataProvider(['allModels' => $this->model->getFiles()]), 'layout' => '{items}', 'tableOptions' => $this->tableOptions, 'columns' => [['class' => 'yii\\grid\\SerialColumn'], ['label' => $this->getModule()->t('attachments', 'File name'), 'format' => 'raw', 'value' => function ($model) {
return Html::a("{$model->name}.{$model->type}", $model->getUrl(), ['class' => ' group' . $model->itemId, 'onclick' => 'return false;']);
}], ['class' => 'yii\\grid\\ActionColumn', 'template' => '{delete}', 'buttons' => ['delete' => function ($url, $model, $key) {
return Html::a('<span class="glyphicon glyphicon-trash"></span>', '#', ['class' => 'delete-button', 'title' => Yii::t('yii', 'Delete'), 'data-url' => Url::to(['/attachments/file/delete', 'id' => $model->id])]);
}]]]]) . Colorbox::widget(['targets' => ['.group' . $this->model->id => ['rel' => '.group' . $this->model->id, 'photo' => true, 'scalePhotos' => true, 'width' => '100%', 'height' => '100%', 'maxWidth' => 800, 'maxHeight' => 600]], 'coreStyle' => 4]);
}
示例4: isBehaviorIncluded
/**
* @return bool
*/
protected function isBehaviorIncluded()
{
if (isset($this->_behaviorIncluded)) {
return $this->_behaviorIncluded;
}
if (!isset($this->aggregateModel) || !isset($this->userValue)) {
foreach ($this->model->getBehaviors() as $behavior) {
if ($behavior instanceof VoteBehavior) {
return $this->_behaviorIncluded = true;
}
}
}
return $this->_behaviorIncluded = false;
}
示例5: getBehavior
/**
* @param ActiveRecord $model
* @return TrackableBehavior
*/
private function getBehavior(ActiveRecord $model)
{
foreach ($model->getBehaviors() as $behavior) {
if ($behavior instanceof TrackableBehavior) {
return $behavior;
}
}
return null;
}
示例6: excludeField
/**
* @param ActiveRecord $model
*/
protected function excludeField($model, $addition = '')
{
$this->fieldExclude = ArrayHelper::merge(is_array($this->excludedField) ? $this->excludedField : [$this->excludedField], $this->fieldExclude);
if (is_callable($this->excludeFieldCallback)) {
$func = $this->excludeFieldCallback;
$result = $func($this);
$result = $result ? is_array($result) ? $result : [$result] : [];
$this->fieldExclude = ArrayHelper::merge($this->fieldExclude, $result);
}
$this->fieldExclude = ArrayHelper::merge($this->fieldExclude, $model->getTableSchema()->primaryKey);
$behaviors = $model->getBehaviors();
foreach ($behaviors as $_next) {
$_nextCopy = $_next;
$_next = (array) $_next;
$_next['class'] = $_nextCopy->className();
if ($_next['class'] === BlameableBehavior::className()) {
if (isset($_next['updatedByAttribute'])) {
if (!empty($_next['updatedByAttribute'])) {
$this->fieldExclude[] = $addition . $_next['updatedByAttribute'];
}
} else {
$this->fieldExclude[] = $addition . 'updated_by';
}
if (isset($_next['createdByAttribute'])) {
if (!empty($_next['createdByAttribute'])) {
$this->fieldExclude[] = $addition . $_next['createdByAttribute'];
}
} else {
$this->fieldExclude[] = $addition . 'created_by';
}
} else {
if ($_next['class'] === TimestampBehavior::className()) {
if (isset($_next['updatedAtAttribute'])) {
if (!empty($_next['updatedAtAttribute'])) {
$this->fieldExclude[] = $addition . $_next['updatedAtAttribute'];
}
} else {
$this->fieldExclude[] = $addition . 'updated_at';
}
if (isset($_next['createdAtAttribute'])) {
if (!empty($_next['createdAtAttribute'])) {
$this->fieldExclude[] = $addition . $_next['createdAtAttribute'];
}
} else {
$this->fieldExclude[] = $addition . 'created_at';
}
} else {
if ($_next['class'] === PhoneInputBehavior::className()) {
if (!$this->searchColumn($_next['phoneAttribute'])) {
$this->columns[] = new WidgetsCrud(['fieldName' => $_next['phoneAttribute'], 'widgetType' => 'phone']);
}
} else {
if ($_next['class'] === UploadBehavior::className()) {
if (!is_array($this->hasUploadBehavior)) {
$this->hasUploadBehavior = ['attributes' => []];
}
$this->hasUploadBehavior['attributes'][] = isset($_next['attribute']) ? $_next['attribute'] : $_next['attribute'];
$this->hasUploadBehavior[] = $_next['pathAttribute'];
$this->hasUploadBehavior[] = $_next['baseUrlAttribute'];
$this->fieldExclude[] = $addition . $_next['pathAttribute'];
$this->fieldExclude[] = $addition . $_next['baseUrlAttribute'];
if (!$this->searchColumn($this->hasUploadBehavior)) {
$this->columns[] = new WidgetsCrud(['fieldName' => $addition . $_next['attribute'], 'widgetType' => 'upload']);
}
if (isset($this->columnUsed['upload']) && ($uses = $this->columnUsed['upload'])) {
if (isset($uses['expression'])) {
$this->expressions = ArrayHelper::merge($this->expressions, $uses['expression']);
unset($this->columnUsed['expression']);
}
}
$this->used = ArrayHelper::merge($this->used, $uses);
} else {
if ($_next['class'] === 'omgdef\\multilingual\\MultilingualBehavior') {
$this->isMultilingual = true;
$this->languageField = $_next['languageField'];
$this->translateAttribute = $_next['attributes'];
$this->relationField = $_next['langForeignKey'];
$class = $_next['langClassName'] . ($_next['dynamicLangClass'] ? isset($_next['langClassSuffix ']) ? $_next['langClassSuffix '] : '' : '');
$this->relationClass = $class;
if (class_exists($class)) {
/** @var ActiveRecord $class */
$class = new $class();
$this->translateTable = $class::tableName();
} else {
$this->translateTable = $_next['tableName'];
}
$this->excludeField($class, 'translations.');
}
}
}
}
}
}
$this->columnUsed = ArrayHelper::merge($this->columnUsed, $this->widgetsUseClass);
}