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
* 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
...
...
@@ -78,7 +78,7 @@ abstract class ActiveRecord extends BaseActiveRecord
* 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.
...
...
@@ -88,10 +88,6 @@ abstract class ActiveRecord extends BaseActiveRecord
*/
public
static
function
deleteAll
(
$condition
=
[],
$options
=
[])
{
$options
[
'w'
]
=
1
;
if
(
!
array_key_exists
(
'multiple'
,
$options
))
{
$options
[
'multiple'
]
=
true
;
}
return
static
::
getCollection
()
->
remove
(
$condition
,
$options
);
}
...
...
@@ -237,8 +233,7 @@ abstract class ActiveRecord extends BaseActiveRecord
$values
[
$key
]
=
isset
(
$currentAttributes
[
$key
])
?
$currentAttributes
[
$key
]
:
null
;
}
}
$collection
=
static
::
getCollection
();
$newId
=
$collection
->
insert
(
$values
);
$newId
=
static
::
getCollection
()
->
insert
(
$values
);
$this
->
setAttribute
(
'_id'
,
$newId
);
foreach
(
$values
as
$name
=>
$value
)
{
$this
->
setOldAttribute
(
$name
,
$value
);
...
...
@@ -348,4 +343,4 @@ abstract class ActiveRecord extends BaseActiveRecord
}
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
}
/**
* 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.
* @param array|object $data data to be inserted.
* @param array $options list of options in format: optionName => optionValue.
...
...
@@ -372,11 +384,12 @@ class Collection extends Object
* @param array $options list of options in format: optionName => optionValue.
* @return integer|boolean number of updated documents or whether operation was successful.
* @throws Exception on failure.
* @see http://www.php.net/manual/en/mongocollection.remove.php
*/
public
function
remove
(
$condition
=
[],
$options
=
[])
{
$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
]);
Yii
::
info
(
$token
,
__METHOD__
);
try
{
...
...
@@ -522,7 +535,7 @@ class Collection extends Object
* Argument will be automatically cast to [[\MongoCode]].
* @param string|array $out output collection name. It could be a string for simple output
* ('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 $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.
...
...
extensions/mongodb/Connection.php
View file @
0e023f53
...
...
@@ -73,6 +73,11 @@ use Yii;
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
*
* Correct syntax is:
...
...
@@ -233,6 +238,7 @@ class Connection extends Component
$options
[
'db'
]
=
$this
->
defaultDatabaseName
;
}
$this
->
mongoClient
=
new
\MongoClient
(
$this
->
dsn
,
$options
);
$this
->
initConnection
();
Yii
::
endProfile
(
$token
,
__METHOD__
);
}
catch
(
\Exception
$e
)
{
Yii
::
endProfile
(
$token
,
__METHOD__
);
...
...
@@ -253,4 +259,14 @@ class Connection extends Component
$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
parent
::
regenerateID
(
false
);
$newID
=
session_id
();
$query
=
new
Query
;
$row
=
$query
->
from
(
$this
->
sessionCollection
)
->
where
([
'id'
=>
$oldID
])
->
one
(
$this
->
db
);
if
(
$row
!==
false
)
{
$collection
=
$this
->
db
->
getCollection
(
$this
->
sessionCollection
);
$row
=
$collection
->
findOne
([
'id'
=>
$oldID
]);
if
(
$row
!==
null
)
{
if
(
$deleteOldSession
)
{
$this
->
db
->
getCollection
(
$this
->
sessionCollection
)
->
update
([
'_id'
=>
$row
[
'_id'
]],
[
'id'
=>
$newID
]);
$collection
->
update
([
'id'
=>
$oldID
],
[
'id'
=>
$newID
]);
}
else
{
unset
(
$row
[
'_id'
]);
$row
[
'id'
]
=
$newID
;
$this
->
db
->
getCollection
(
$this
->
sessionCollection
)
->
insert
(
$row
);
$collection
->
insert
(
$row
);
}
}
else
{
// shouldn't reach here normally
$this
->
db
->
getCollection
(
$this
->
sessionCollection
)
->
insert
([
'id'
=>
$newID
,
'expire'
=>
time
()
+
$this
->
getTimeout
(),
'data'
=>
''
,
]);
$collection
->
insert
([
'id'
=>
$newID
,
'expire'
=>
time
()
+
$this
->
getTimeout
()
]);
}
}
...
...
@@ -124,15 +118,15 @@ class Session extends \yii\web\Session
*/
public
function
readSession
(
$id
)
{
$
query
=
new
Query
;
$
row
=
$query
->
select
([
'data'
])
->
from
(
$this
->
sessionCollection
)
->
where
([
$
collection
=
$this
->
db
->
getCollection
(
$this
->
sessionCollection
)
;
$
doc
=
$collection
->
findOne
(
[
'id'
=>
$id
,
'expire'
=>
[
'$gt'
=>
time
()],
'id'
=>
$id
])
->
one
(
$this
->
db
);
return
$row
===
false
?
''
:
$row
[
'data'
]
;
],
[
'data'
=>
1
,
'_id'
=>
0
]
);
return
isset
(
$doc
[
'data'
])
?
$doc
[
'data'
]
:
''
;
}
/**
...
...
@@ -147,23 +141,15 @@ class Session extends \yii\web\Session
// exception must be caught in session write handler
// http://us.php.net/manual/en/function.session-set-save-handler.php
try
{
$expire
=
time
()
+
$this
->
getTimeout
();
$query
=
new
Query
;
$exists
=
$query
->
select
([
'id'
])
->
from
(
$this
->
sessionCollection
)
->
where
([
'id'
=>
$id
])
->
one
(
$this
->
db
);
if
(
$exists
===
false
)
{
$this
->
db
->
getCollection
(
$this
->
sessionCollection
)
->
insert
([
'id'
=>
$id
,
'data'
=>
$data
,
'expire'
=>
$expire
,
]);
}
else
{
$this
->
db
->
getCollection
(
$this
->
sessionCollection
)
->
update
([
'id'
=>
$id
],
[
'data'
=>
$data
,
'expire'
=>
$expire
]);
}
$this
->
db
->
getCollection
(
$this
->
sessionCollection
)
->
update
(
[
'id'
=>
$id
],
[
'id'
=>
$id
,
'data'
=>
$data
,
'expire'
=>
time
()
+
$this
->
getTimeout
(),
],
[
'upsert'
=>
true
]
);
}
catch
(
\Exception
$e
)
{
if
(
YII_DEBUG
)
{
echo
$e
->
getMessage
();
...
...
@@ -182,8 +168,10 @@ class Session extends \yii\web\Session
*/
public
function
destroySession
(
$id
)
{
$this
->
db
->
getCollection
(
$this
->
sessionCollection
)
->
remove
([
'id'
=>
$id
]);
$this
->
db
->
getCollection
(
$this
->
sessionCollection
)
->
remove
(
[
'id'
=>
$id
],
[
'justOne'
=>
true
]
);
return
true
;
}
...
...
@@ -196,9 +184,7 @@ class Session extends \yii\web\Session
public
function
gcSession
(
$maxLifetime
)
{
$this
->
db
->
getCollection
(
$this
->
sessionCollection
)
->
remove
([
'expire'
=>
[
'$lt'
=>
time
()]
]);
->
remove
([
'expire'
=>
[
'$lt'
=>
time
()]]);
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