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
0e023f53
Commit
0e023f53
authored
Feb 19, 2014
by
Paul Klimov
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2480 from mongosoft/mongodb
Mongodb extension refactorign
parents
2a7df8f1
c65b171e
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
69 additions
and
56 deletions
+69
-56
ActiveRecord.php
extensions/mongodb/ActiveRecord.php
+5
-9
Collection.php
extensions/mongodb/Collection.php
+15
-2
Connection.php
extensions/mongodb/Connection.php
+17
-0
Session.php
extensions/mongodb/Session.php
+32
-45
No files found.
extensions/mongodb/ActiveRecord.php
View file @
0e023f53
...
@@ -37,7 +37,7 @@ abstract class ActiveRecord extends BaseActiveRecord
...
@@ -37,7 +37,7 @@ abstract class ActiveRecord extends BaseActiveRecord
* For example, to change the status to be 1 for all customers whose status is 2:
* For example, to change the status to be 1 for all customers whose status is 2:
*
*
* ~~~
* ~~~
* Customer::updateAll(['status' => 1], ['status' = 2]);
* Customer::updateAll(['status' => 1], ['status' =
>
2]);
* ~~~
* ~~~
*
*
* @param array $attributes attribute values (name-value pairs) to be saved into the collection
* @param array $attributes attribute values (name-value pairs) to be saved into the collection
...
@@ -78,7 +78,7 @@ abstract class ActiveRecord extends BaseActiveRecord
...
@@ -78,7 +78,7 @@ abstract class ActiveRecord extends BaseActiveRecord
* For example, to delete all customers whose status is 3:
* For example, to delete all customers whose status is 3:
*
*
* ~~~
* ~~~
* Customer::deleteAll(
'status = 3'
);
* Customer::deleteAll(
['status' => 3]
);
* ~~~
* ~~~
*
*
* @param array $condition description of the objects to delete.
* @param array $condition description of the objects to delete.
...
@@ -88,10 +88,6 @@ abstract class ActiveRecord extends BaseActiveRecord
...
@@ -88,10 +88,6 @@ abstract class ActiveRecord extends BaseActiveRecord
*/
*/
public
static
function
deleteAll
(
$condition
=
[],
$options
=
[])
public
static
function
deleteAll
(
$condition
=
[],
$options
=
[])
{
{
$options
[
'w'
]
=
1
;
if
(
!
array_key_exists
(
'multiple'
,
$options
))
{
$options
[
'multiple'
]
=
true
;
}
return
static
::
getCollection
()
->
remove
(
$condition
,
$options
);
return
static
::
getCollection
()
->
remove
(
$condition
,
$options
);
}
}
...
@@ -237,8 +233,7 @@ abstract class ActiveRecord extends BaseActiveRecord
...
@@ -237,8 +233,7 @@ abstract class ActiveRecord extends BaseActiveRecord
$values
[
$key
]
=
isset
(
$currentAttributes
[
$key
])
?
$currentAttributes
[
$key
]
:
null
;
$values
[
$key
]
=
isset
(
$currentAttributes
[
$key
])
?
$currentAttributes
[
$key
]
:
null
;
}
}
}
}
$collection
=
static
::
getCollection
();
$newId
=
static
::
getCollection
()
->
insert
(
$values
);
$newId
=
$collection
->
insert
(
$values
);
$this
->
setAttribute
(
'_id'
,
$newId
);
$this
->
setAttribute
(
'_id'
,
$newId
);
foreach
(
$values
as
$name
=>
$value
)
{
foreach
(
$values
as
$name
=>
$value
)
{
$this
->
setOldAttribute
(
$name
,
$value
);
$this
->
setOldAttribute
(
$name
,
$value
);
...
@@ -348,4 +343,4 @@ abstract class ActiveRecord extends BaseActiveRecord
...
@@ -348,4 +343,4 @@ abstract class ActiveRecord extends BaseActiveRecord
}
}
return
$this
->
collectionName
()
===
$record
->
collectionName
()
&&
(
string
)
$this
->
getPrimaryKey
()
===
(
string
)
$record
->
getPrimaryKey
();
return
$this
->
collectionName
()
===
$record
->
collectionName
()
&&
(
string
)
$this
->
getPrimaryKey
()
===
(
string
)
$record
->
getPrimaryKey
();
}
}
}
}
\ No newline at end of file
extensions/mongodb/Collection.php
View file @
0e023f53
...
@@ -260,6 +260,18 @@ class Collection extends Object
...
@@ -260,6 +260,18 @@ class Collection extends Object
}
}
/**
/**
* Returns a a single document.
* @param array $condition query condition
* @param array $fields fields to be selected
* @return array|null the single document. Null is returned if the query results in nothing.
* @see http://www.php.net/manual/en/mongocollection.findone.php
*/
public
function
findOne
(
$condition
=
[],
$fields
=
[])
{
return
$this
->
mongoCollection
->
findOne
(
$this
->
buildCondition
(
$condition
),
$fields
);
}
/**
* Inserts new data into collection.
* Inserts new data into collection.
* @param array|object $data data to be inserted.
* @param array|object $data data to be inserted.
* @param array $options list of options in format: optionName => optionValue.
* @param array $options list of options in format: optionName => optionValue.
...
@@ -372,11 +384,12 @@ class Collection extends Object
...
@@ -372,11 +384,12 @@ class Collection extends Object
* @param array $options list of options in format: optionName => optionValue.
* @param array $options list of options in format: optionName => optionValue.
* @return integer|boolean number of updated documents or whether operation was successful.
* @return integer|boolean number of updated documents or whether operation was successful.
* @throws Exception on failure.
* @throws Exception on failure.
* @see http://www.php.net/manual/en/mongocollection.remove.php
*/
*/
public
function
remove
(
$condition
=
[],
$options
=
[])
public
function
remove
(
$condition
=
[],
$options
=
[])
{
{
$condition
=
$this
->
buildCondition
(
$condition
);
$condition
=
$this
->
buildCondition
(
$condition
);
$options
=
array_merge
([
'w'
=>
1
,
'
multiple'
=>
tru
e
],
$options
);
$options
=
array_merge
([
'w'
=>
1
,
'
justOne'
=>
fals
e
],
$options
);
$token
=
$this
->
composeLogToken
(
'remove'
,
[
$condition
,
$options
]);
$token
=
$this
->
composeLogToken
(
'remove'
,
[
$condition
,
$options
]);
Yii
::
info
(
$token
,
__METHOD__
);
Yii
::
info
(
$token
,
__METHOD__
);
try
{
try
{
...
@@ -522,7 +535,7 @@ class Collection extends Object
...
@@ -522,7 +535,7 @@ class Collection extends Object
* Argument will be automatically cast to [[\MongoCode]].
* Argument will be automatically cast to [[\MongoCode]].
* @param string|array $out output collection name. It could be a string for simple output
* @param string|array $out output collection name. It could be a string for simple output
* ('outputCollection'), or an array for parametrized output (['merge' => 'outputCollection']).
* ('outputCollection'), or an array for parametrized output (['merge' => 'outputCollection']).
* You can pass ['inline' => true] to fetch the result at once without temporary collection usage.
* You can pass ['inline' => true] to fetch the result at once without temporary collection usage.
* @param array $condition criteria for including a document in the aggregation.
* @param array $condition criteria for including a document in the aggregation.
* @param array $options additional optional parameters to the mapReduce command. Valid options include:
* @param array $options additional optional parameters to the mapReduce command. Valid options include:
* - sort - array - key to sort the input documents. The sort key must be in an existing index for this collection.
* - sort - array - key to sort the input documents. The sort key must be in an existing index for this collection.
...
...
extensions/mongodb/Connection.php
View file @
0e023f53
...
@@ -73,6 +73,11 @@ use Yii;
...
@@ -73,6 +73,11 @@ use Yii;
class
Connection
extends
Component
class
Connection
extends
Component
{
{
/**
/**
* @event Event an event that is triggered after a DB connection is established
*/
const
EVENT_AFTER_OPEN
=
'afterOpen'
;
/**
* @var string host:port
* @var string host:port
*
*
* Correct syntax is:
* Correct syntax is:
...
@@ -233,6 +238,7 @@ class Connection extends Component
...
@@ -233,6 +238,7 @@ class Connection extends Component
$options
[
'db'
]
=
$this
->
defaultDatabaseName
;
$options
[
'db'
]
=
$this
->
defaultDatabaseName
;
}
}
$this
->
mongoClient
=
new
\MongoClient
(
$this
->
dsn
,
$options
);
$this
->
mongoClient
=
new
\MongoClient
(
$this
->
dsn
,
$options
);
$this
->
initConnection
();
Yii
::
endProfile
(
$token
,
__METHOD__
);
Yii
::
endProfile
(
$token
,
__METHOD__
);
}
catch
(
\Exception
$e
)
{
}
catch
(
\Exception
$e
)
{
Yii
::
endProfile
(
$token
,
__METHOD__
);
Yii
::
endProfile
(
$token
,
__METHOD__
);
...
@@ -253,4 +259,14 @@ class Connection extends Component
...
@@ -253,4 +259,14 @@ class Connection extends Component
$this
->
_databases
=
[];
$this
->
_databases
=
[];
}
}
}
}
/**
* Initializes the DB connection.
* This method is invoked right after the DB connection is established.
* The default implementation triggers an [[EVENT_AFTER_OPEN]] event.
*/
protected
function
initConnection
()
{
$this
->
trigger
(
self
::
EVENT_AFTER_OPEN
);
}
}
}
\ No newline at end of file
extensions/mongodb/Session.php
View file @
0e023f53
...
@@ -91,28 +91,22 @@ class Session extends \yii\web\Session
...
@@ -91,28 +91,22 @@ class Session extends \yii\web\Session
parent
::
regenerateID
(
false
);
parent
::
regenerateID
(
false
);
$newID
=
session_id
();
$newID
=
session_id
();
$query
=
new
Query
;
$collection
=
$this
->
db
->
getCollection
(
$this
->
sessionCollection
);
$row
=
$query
->
from
(
$this
->
sessionCollection
)
$row
=
$collection
->
findOne
([
'id'
=>
$oldID
]);
->
where
([
'id'
=>
$oldID
])
if
(
$row
!==
null
)
{
->
one
(
$this
->
db
);
if
(
$row
!==
false
)
{
if
(
$deleteOldSession
)
{
if
(
$deleteOldSession
)
{
$this
->
db
->
getCollection
(
$this
->
sessionCollection
)
$collection
->
update
([
'id'
=>
$oldID
],
[
'id'
=>
$newID
]);
->
update
([
'_id'
=>
$row
[
'_id'
]],
[
'id'
=>
$newID
]);
}
else
{
}
else
{
unset
(
$row
[
'_id'
]);
unset
(
$row
[
'_id'
]);
$row
[
'id'
]
=
$newID
;
$row
[
'id'
]
=
$newID
;
$this
->
db
->
getCollection
(
$this
->
sessionCollection
)
$collection
->
insert
(
$row
);
->
insert
(
$row
);
}
}
}
else
{
}
else
{
// shouldn't reach here normally
// shouldn't reach here normally
$this
->
db
->
getCollection
(
$this
->
sessionCollection
)
$collection
->
insert
([
->
insert
([
'id'
=>
$newID
,
'id'
=>
$newID
,
'expire'
=>
time
()
+
$this
->
getTimeout
()
'expire'
=>
time
()
+
$this
->
getTimeout
(),
]);
'data'
=>
''
,
]);
}
}
}
}
...
@@ -124,15 +118,15 @@ class Session extends \yii\web\Session
...
@@ -124,15 +118,15 @@ class Session extends \yii\web\Session
*/
*/
public
function
readSession
(
$id
)
public
function
readSession
(
$id
)
{
{
$
query
=
new
Query
;
$
collection
=
$this
->
db
->
getCollection
(
$this
->
sessionCollection
)
;
$
row
=
$query
->
select
([
'data'
])
$
doc
=
$collection
->
findOne
(
->
from
(
$this
->
sessionCollection
)
[
->
where
([
'id'
=>
$id
,
'expire'
=>
[
'$gt'
=>
time
()],
'expire'
=>
[
'$gt'
=>
time
()],
'id'
=>
$id
],
])
[
'data'
=>
1
,
'_id'
=>
0
]
->
one
(
$this
->
db
);
);
return
$row
===
false
?
''
:
$row
[
'data'
]
;
return
isset
(
$doc
[
'data'
])
?
$doc
[
'data'
]
:
''
;
}
}
/**
/**
...
@@ -147,23 +141,15 @@ class Session extends \yii\web\Session
...
@@ -147,23 +141,15 @@ class Session extends \yii\web\Session
// exception must be caught in session write handler
// exception must be caught in session write handler
// http://us.php.net/manual/en/function.session-set-save-handler.php
// http://us.php.net/manual/en/function.session-set-save-handler.php
try
{
try
{
$expire
=
time
()
+
$this
->
getTimeout
();
$this
->
db
->
getCollection
(
$this
->
sessionCollection
)
->
update
(
$query
=
new
Query
;
[
'id'
=>
$id
],
$exists
=
$query
->
select
([
'id'
])
[
->
from
(
$this
->
sessionCollection
)
'id'
=>
$id
,
->
where
([
'id'
=>
$id
])
'data'
=>
$data
,
->
one
(
$this
->
db
);
'expire'
=>
time
()
+
$this
->
getTimeout
(),
if
(
$exists
===
false
)
{
],
$this
->
db
->
getCollection
(
$this
->
sessionCollection
)
[
'upsert'
=>
true
]
->
insert
([
);
'id'
=>
$id
,
'data'
=>
$data
,
'expire'
=>
$expire
,
]);
}
else
{
$this
->
db
->
getCollection
(
$this
->
sessionCollection
)
->
update
([
'id'
=>
$id
],
[
'data'
=>
$data
,
'expire'
=>
$expire
]);
}
}
catch
(
\Exception
$e
)
{
}
catch
(
\Exception
$e
)
{
if
(
YII_DEBUG
)
{
if
(
YII_DEBUG
)
{
echo
$e
->
getMessage
();
echo
$e
->
getMessage
();
...
@@ -182,8 +168,10 @@ class Session extends \yii\web\Session
...
@@ -182,8 +168,10 @@ class Session extends \yii\web\Session
*/
*/
public
function
destroySession
(
$id
)
public
function
destroySession
(
$id
)
{
{
$this
->
db
->
getCollection
(
$this
->
sessionCollection
)
$this
->
db
->
getCollection
(
$this
->
sessionCollection
)
->
remove
(
->
remove
([
'id'
=>
$id
]);
[
'id'
=>
$id
],
[
'justOne'
=>
true
]
);
return
true
;
return
true
;
}
}
...
@@ -196,9 +184,7 @@ class Session extends \yii\web\Session
...
@@ -196,9 +184,7 @@ class Session extends \yii\web\Session
public
function
gcSession
(
$maxLifetime
)
public
function
gcSession
(
$maxLifetime
)
{
{
$this
->
db
->
getCollection
(
$this
->
sessionCollection
)
$this
->
db
->
getCollection
(
$this
->
sessionCollection
)
->
remove
([
->
remove
([
'expire'
=>
[
'$lt'
=>
time
()]]);
'expire'
=>
[
'$lt'
=>
time
()]
]);
return
true
;
return
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