Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
b062a660
Commit
b062a660
authored
Aug 11, 2014
by
Klimov Paul
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Default event for `yii\behaviors\Sluggable` with `unique` enabled changed to 'insert'.
parent
327914e4
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
48 additions
and
15 deletions
+48
-15
SluggableBehavior.php
framework/behaviors/SluggableBehavior.php
+7
-3
SluggableBehaviorTest.php
tests/unit/framework/behaviors/SluggableBehaviorTest.php
+41
-12
No files found.
framework/behaviors/SluggableBehavior.php
View file @
b062a660
...
@@ -118,7 +118,11 @@ class SluggableBehavior extends AttributeBehavior
...
@@ -118,7 +118,11 @@ class SluggableBehavior extends AttributeBehavior
parent
::
init
();
parent
::
init
();
if
(
empty
(
$this
->
attributes
))
{
if
(
empty
(
$this
->
attributes
))
{
$this
->
attributes
=
[
BaseActiveRecord
::
EVENT_BEFORE_VALIDATE
=>
$this
->
slugAttribute
];
if
(
$this
->
unique
)
{
$this
->
attributes
=
[
BaseActiveRecord
::
EVENT_BEFORE_INSERT
=>
$this
->
slugAttribute
];
}
else
{
$this
->
attributes
=
[
BaseActiveRecord
::
EVENT_BEFORE_VALIDATE
=>
$this
->
slugAttribute
];
}
}
}
if
(
$this
->
attribute
===
null
&&
$this
->
value
===
null
)
{
if
(
$this
->
attribute
===
null
&&
$this
->
value
===
null
)
{
...
@@ -135,9 +139,9 @@ class SluggableBehavior extends AttributeBehavior
...
@@ -135,9 +139,9 @@ class SluggableBehavior extends AttributeBehavior
if
(
is_array
(
$this
->
attribute
))
{
if
(
is_array
(
$this
->
attribute
))
{
$slugParts
=
[];
$slugParts
=
[];
foreach
(
$this
->
attribute
as
$attribute
)
{
foreach
(
$this
->
attribute
as
$attribute
)
{
$slugParts
[]
=
Inflector
::
slug
(
$this
->
owner
->
{
$attribute
})
;
$slugParts
[]
=
$this
->
owner
->
{
$attribute
}
;
}
}
$this
->
value
=
implode
(
'-'
,
$slugParts
);
$this
->
value
=
Inflector
::
slug
(
implode
(
'-'
,
$slugParts
)
);
}
else
{
}
else
{
$this
->
value
=
Inflector
::
slug
(
$this
->
owner
->
{
$this
->
attribute
});
$this
->
value
=
Inflector
::
slug
(
$this
->
owner
->
{
$this
->
attribute
});
}
}
...
...
tests/unit/framework/behaviors/SluggableBehaviorTest.php
View file @
b062a660
...
@@ -87,12 +87,12 @@ class SluggableBehaviorTest extends TestCase
...
@@ -87,12 +87,12 @@ class SluggableBehaviorTest extends TestCase
{
{
$name
=
'test name'
;
$name
=
'test name'
;
$model
=
new
ActiveRecordSluggable
();
$model
=
new
ActiveRecordSluggable
Unique
();
$model
->
name
=
$name
;
$model
->
name
=
$name
;
$model
->
save
();
$model
->
save
();
$model
=
new
ActiveRecordSluggable
();
$model
=
new
ActiveRecordSluggable
Unique
();
$model
->
sluggable
->
unique
=
true
;
$model
->
sluggable
->
unique
SlugGenerator
=
'increment'
;
$model
->
name
=
$name
;
$model
->
name
=
$name
;
$model
->
save
();
$model
->
save
();
...
@@ -106,12 +106,11 @@ class SluggableBehaviorTest extends TestCase
...
@@ -106,12 +106,11 @@ class SluggableBehaviorTest extends TestCase
{
{
$name
=
'test name'
;
$name
=
'test name'
;
$model
=
new
ActiveRecordSluggable
();
$model
=
new
ActiveRecordSluggable
Unique
();
$model
->
name
=
$name
;
$model
->
name
=
$name
;
$model
->
save
();
$model
->
save
();
$model
=
new
ActiveRecordSluggable
();
$model
=
new
ActiveRecordSluggableUnique
();
$model
->
sluggable
->
unique
=
true
;
$model
->
sluggable
->
uniqueSlugGenerator
=
function
(
$baseSlug
,
$iteration
)
{
return
$baseSlug
.
'-callback'
;};
$model
->
sluggable
->
uniqueSlugGenerator
=
function
(
$baseSlug
,
$iteration
)
{
return
$baseSlug
.
'-callback'
;};
$model
->
name
=
$name
;
$model
->
name
=
$name
;
$model
->
save
();
$model
->
save
();
...
@@ -126,12 +125,11 @@ class SluggableBehaviorTest extends TestCase
...
@@ -126,12 +125,11 @@ class SluggableBehaviorTest extends TestCase
{
{
$name
=
'test name'
;
$name
=
'test name'
;
$model1
=
new
ActiveRecordSluggable
();
$model1
=
new
ActiveRecordSluggable
Unique
();
$model1
->
name
=
$name
;
$model1
->
name
=
$name
;
$model1
->
save
();
$model1
->
save
();
$model2
=
new
ActiveRecordSluggable
();
$model2
=
new
ActiveRecordSluggableUnique
();
$model2
->
sluggable
->
unique
=
true
;
$model2
->
sluggable
->
uniqueSlugGenerator
=
'uniqueid'
;
$model2
->
sluggable
->
uniqueSlugGenerator
=
'uniqueid'
;
$model2
->
name
=
$name
;
$model2
->
name
=
$name
;
$model2
->
save
();
$model2
->
save
();
...
@@ -146,18 +144,34 @@ class SluggableBehaviorTest extends TestCase
...
@@ -146,18 +144,34 @@ class SluggableBehaviorTest extends TestCase
{
{
$name
=
'test name'
;
$name
=
'test name'
;
$model1
=
new
ActiveRecordSluggable
();
$model1
=
new
ActiveRecordSluggable
Unique
();
$model1
->
name
=
$name
;
$model1
->
name
=
$name
;
$model1
->
save
();
$model1
->
save
();
$model2
=
new
ActiveRecordSluggable
();
$model2
=
new
ActiveRecordSluggableUnique
();
$model2
->
sluggable
->
unique
=
true
;
$model2
->
sluggable
->
uniqueSlugGenerator
=
'timestamp'
;
$model2
->
sluggable
->
uniqueSlugGenerator
=
'timestamp'
;
$model2
->
name
=
$name
;
$model2
->
name
=
$name
;
$model2
->
save
();
$model2
->
save
();
$this
->
assertNotEquals
(
$model2
->
slug
,
$model1
->
slug
);
$this
->
assertNotEquals
(
$model2
->
slug
,
$model1
->
slug
);
}
}
/**
* @depends testSlug
*/
public
function
testUpdateUnique
()
{
$name
=
'test name'
;
$model
=
new
ActiveRecordSluggableUnique
();
$model
->
name
=
$name
;
$model
->
save
();
$model
=
ActiveRecordSluggableUnique
::
find
()
->
one
();
$model
->
save
();
$this
->
assertEquals
(
'test-name'
,
$model
->
slug
);
}
}
}
/**
/**
...
@@ -194,4 +208,18 @@ class ActiveRecordSluggable extends ActiveRecord
...
@@ -194,4 +208,18 @@ class ActiveRecordSluggable extends ActiveRecord
{
{
return
$this
->
getBehavior
(
'sluggable'
);
return
$this
->
getBehavior
(
'sluggable'
);
}
}
}
class
ActiveRecordSluggableUnique
extends
ActiveRecordSluggable
{
public
function
behaviors
()
{
return
[
'sluggable'
=>
[
'class'
=>
SluggableBehavior
::
className
(),
'attribute'
=>
'name'
,
'unique'
=>
true
,
],
];
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment