mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-26 14:26:54 +08:00
Fixes #13035: Use ArrayHelper::getValue() in SluggableBehavior::getValue()
This commit is contained in:

committed by
Alexander Makarov

parent
75162652b7
commit
b58fee7d37
@ -39,6 +39,7 @@ Yii Framework 2 Change Log
|
|||||||
- Enh #12816: Added `columnSchemaClass` option for `yii\db\Schema` which adds ability to specify custom `yii\db\ColumnSchema` class (nanodesu88)
|
- Enh #12816: Added `columnSchemaClass` option for `yii\db\Schema` which adds ability to specify custom `yii\db\ColumnSchema` class (nanodesu88)
|
||||||
- Enh #12881: Added `removeValue` method to `yii\helpers\BaseArrayHelper` (nilsburg)
|
- Enh #12881: Added `removeValue` method to `yii\helpers\BaseArrayHelper` (nilsburg)
|
||||||
- Enh #12901: Added `getDefaultHelpHeader` method to the `yii\console\controllers\HelpController` class to be able to override default help header in a class heir (diezztsk)
|
- Enh #12901: Added `getDefaultHelpHeader` method to the `yii\console\controllers\HelpController` class to be able to override default help header in a class heir (diezztsk)
|
||||||
|
- Enh #13035: Use ArrayHelper::getValue() in SluggableBehavior::getValue() (thyseus)
|
||||||
- Enh: Added constants for specifying `yii\validators\CompareValidator::$type` (cebe)
|
- Enh: Added constants for specifying `yii\validators\CompareValidator::$type` (cebe)
|
||||||
|
|
||||||
|
|
||||||
|
@ -9,6 +9,7 @@ namespace yii\behaviors;
|
|||||||
|
|
||||||
use yii\base\InvalidConfigException;
|
use yii\base\InvalidConfigException;
|
||||||
use yii\db\BaseActiveRecord;
|
use yii\db\BaseActiveRecord;
|
||||||
|
use yii\helpers\ArrayHelper;
|
||||||
use yii\helpers\Inflector;
|
use yii\helpers\Inflector;
|
||||||
use yii\validators\UniqueValidator;
|
use yii\validators\UniqueValidator;
|
||||||
use Yii;
|
use Yii;
|
||||||
@ -139,7 +140,7 @@ class SluggableBehavior extends AttributeBehavior
|
|||||||
if ($this->isNewSlugNeeded()) {
|
if ($this->isNewSlugNeeded()) {
|
||||||
$slugParts = [];
|
$slugParts = [];
|
||||||
foreach ((array) $this->attribute as $attribute) {
|
foreach ((array) $this->attribute as $attribute) {
|
||||||
$slugParts[] = $this->owner->{$attribute};
|
$slugParts[] = ArrayHelper::getValue($this->owner, $attribute);
|
||||||
}
|
}
|
||||||
|
|
||||||
$slug = $this->generateSlug($slugParts);
|
$slug = $this->generateSlug($slugParts);
|
||||||
|
@ -44,8 +44,15 @@ class SluggableBehaviorTest extends TestCase
|
|||||||
'name' => 'string',
|
'name' => 'string',
|
||||||
'slug' => 'string',
|
'slug' => 'string',
|
||||||
'category_id' => 'integer',
|
'category_id' => 'integer',
|
||||||
|
'belongs_to_id' => 'integer',
|
||||||
];
|
];
|
||||||
Yii::$app->getDb()->createCommand()->createTable('test_slug', $columns)->execute();
|
Yii::$app->getDb()->createCommand()->createTable('test_slug', $columns)->execute();
|
||||||
|
|
||||||
|
$columns = [
|
||||||
|
'id' => 'pk',
|
||||||
|
'name' => 'string',
|
||||||
|
];
|
||||||
|
Yii::$app->getDb()->createCommand()->createTable('test_slug_related', $columns)->execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function tearDown()
|
public function tearDown()
|
||||||
@ -80,6 +87,25 @@ class SluggableBehaviorTest extends TestCase
|
|||||||
$this->assertEquals('test-10', $model->slug);
|
$this->assertEquals('test-10', $model->slug);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @depends testSlug
|
||||||
|
*/
|
||||||
|
public function testSlugRelatedAttribute()
|
||||||
|
{
|
||||||
|
$model = new ActiveRecordSluggable();
|
||||||
|
$model->getBehavior('sluggable')->attribute = 'related.name';
|
||||||
|
|
||||||
|
$relatedmodel = new ActiveRecordRelated();
|
||||||
|
$relatedmodel->name = 'I am an value inside an related activerecord model';
|
||||||
|
$relatedmodel->save(false);
|
||||||
|
|
||||||
|
$model->belongs_to_id = $relatedmodel->id;
|
||||||
|
|
||||||
|
$model->validate();
|
||||||
|
|
||||||
|
$this->assertEquals('i-am-an-value-inside-an-related-activerecord-model', $model->slug);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @depends testSlug
|
* @depends testSlug
|
||||||
*/
|
*/
|
||||||
@ -176,6 +202,19 @@ class ActiveRecordSluggable extends ActiveRecord
|
|||||||
{
|
{
|
||||||
return $this->getBehavior('sluggable');
|
return $this->getBehavior('sluggable');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getRelated()
|
||||||
|
{
|
||||||
|
return $this->hasOne(ActiveRecordRelated::className(), ['id' => 'belongs_to_id']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ActiveRecordRelated extends ActiveRecord
|
||||||
|
{
|
||||||
|
public static function tableName()
|
||||||
|
{
|
||||||
|
return 'test_slug_related';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ActiveRecordSluggableUnique extends ActiveRecordSluggable
|
class ActiveRecordSluggableUnique extends ActiveRecordSluggable
|
||||||
|
Reference in New Issue
Block a user