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
77f10ed9
Commit
77f10ed9
authored
Dec 05, 2013
by
Paul Klimov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Mongo file Active Record saving fixed.
parent
ca608a81
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
185 additions
and
33 deletions
+185
-33
ActiveRecord.php
extensions/mongo/file/ActiveRecord.php
+28
-5
Collection.php
extensions/mongo/file/Collection.php
+6
-17
ActiveRecordTest.php
tests/unit/extensions/mongo/ActiveRecordTest.php
+1
-2
ActiveRecordTest.php
tests/unit/extensions/mongo/file/ActiveRecordTest.php
+142
-1
CollectionTest.php
tests/unit/extensions/mongo/file/CollectionTest.php
+7
-7
QueryTest.php
tests/unit/extensions/mongo/file/QueryTest.php
+1
-1
No files found.
extensions/mongo/file/ActiveRecord.php
View file @
77f10ed9
...
@@ -92,7 +92,7 @@ class ActiveRecord extends \yii\mongo\ActiveRecord
...
@@ -92,7 +92,7 @@ class ActiveRecord extends \yii\mongo\ActiveRecord
$fileContent
=
$values
[
'newFileContent'
];
$fileContent
=
$values
[
'newFileContent'
];
unset
(
$values
[
'newFileContent'
]);
unset
(
$values
[
'newFileContent'
]);
unset
(
$values
[
'file'
]);
unset
(
$values
[
'file'
]);
$newId
=
$collection
->
storeBytes
(
$fileContent
,
$values
);
$newId
=
$collection
->
insertFileContent
(
$fileContent
,
$values
);
}
elseif
(
array_key_exists
(
'file'
,
$values
))
{
}
elseif
(
array_key_exists
(
'file'
,
$values
))
{
$file
=
$values
[
'file'
];
$file
=
$values
[
'file'
];
if
(
$file
instanceof
UploadedFile
)
{
if
(
$file
instanceof
UploadedFile
)
{
...
@@ -108,7 +108,7 @@ class ActiveRecord extends \yii\mongo\ActiveRecord
...
@@ -108,7 +108,7 @@ class ActiveRecord extends \yii\mongo\ActiveRecord
}
}
unset
(
$values
[
'newFileContent'
]);
unset
(
$values
[
'newFileContent'
]);
unset
(
$values
[
'file'
]);
unset
(
$values
[
'file'
]);
$newId
=
$collection
->
store
File
(
$fileName
,
$values
);
$newId
=
$collection
->
insert
File
(
$fileName
,
$values
);
}
else
{
}
else
{
$newId
=
$collection
->
insert
(
$values
);
$newId
=
$collection
->
insert
(
$values
);
}
}
...
@@ -142,8 +142,10 @@ class ActiveRecord extends \yii\mongo\ActiveRecord
...
@@ -142,8 +142,10 @@ class ActiveRecord extends \yii\mongo\ActiveRecord
unset
(
$values
[
'file'
]);
unset
(
$values
[
'file'
]);
$values
[
'_id'
]
=
$this
->
getAttribute
(
'_id'
);
$values
[
'_id'
]
=
$this
->
getAttribute
(
'_id'
);
$this
->
deleteInternal
();
$this
->
deleteInternal
();
$collection
->
storeBytes
(
$fileContent
,
$values
);
$collection
->
insertFileContent
(
$fileContent
,
$values
);
$rows
=
1
;
$rows
=
1
;
$this
->
setAttribute
(
'newFileContent'
,
null
);
$this
->
setAttribute
(
'file'
,
null
);
}
elseif
(
array_key_exists
(
'file'
,
$values
))
{
}
elseif
(
array_key_exists
(
'file'
,
$values
))
{
$file
=
$values
[
'file'
];
$file
=
$values
[
'file'
];
if
(
$file
instanceof
UploadedFile
)
{
if
(
$file
instanceof
UploadedFile
)
{
...
@@ -161,8 +163,10 @@ class ActiveRecord extends \yii\mongo\ActiveRecord
...
@@ -161,8 +163,10 @@ class ActiveRecord extends \yii\mongo\ActiveRecord
unset
(
$values
[
'file'
]);
unset
(
$values
[
'file'
]);
$values
[
'_id'
]
=
$this
->
getAttribute
(
'_id'
);
$values
[
'_id'
]
=
$this
->
getAttribute
(
'_id'
);
$this
->
deleteInternal
();
$this
->
deleteInternal
();
$collection
->
store
File
(
$fileName
,
$values
);
$collection
->
insert
File
(
$fileName
,
$values
);
$rows
=
1
;
$rows
=
1
;
$this
->
setAttribute
(
'newFileContent'
,
null
);
$this
->
setAttribute
(
'file'
,
null
);
}
else
{
}
else
{
$condition
=
$this
->
getOldPrimaryKey
(
true
);
$condition
=
$this
->
getOldPrimaryKey
(
true
);
$lock
=
$this
->
optimisticLock
();
$lock
=
$this
->
optimisticLock
();
...
@@ -188,6 +192,17 @@ class ActiveRecord extends \yii\mongo\ActiveRecord
...
@@ -188,6 +192,17 @@ class ActiveRecord extends \yii\mongo\ActiveRecord
}
}
/**
/**
* Refreshes the [[file]] attribute from file collection, using current primary key.
* @return \MongoGridFSFile|null refreshed file value.
*/
public
function
refreshFile
()
{
$mongoFile
=
$this
->
getCollection
()
->
get
(
$this
->
getPrimaryKey
());
$this
->
setAttribute
(
'file'
,
$mongoFile
);
return
$mongoFile
;
}
/**
* Returns the associated file content.
* Returns the associated file content.
* @return null|string file content.
* @return null|string file content.
* @throws \yii\base\InvalidParamException on invalid file value.
* @throws \yii\base\InvalidParamException on invalid file value.
...
@@ -195,10 +210,18 @@ class ActiveRecord extends \yii\mongo\ActiveRecord
...
@@ -195,10 +210,18 @@ class ActiveRecord extends \yii\mongo\ActiveRecord
public
function
getFileContent
()
public
function
getFileContent
()
{
{
$file
=
$this
->
getAttribute
(
'file'
);
$file
=
$this
->
getAttribute
(
'file'
);
if
(
empty
(
$file
)
&&
!
$this
->
getIsNewRecord
())
{
$file
=
$this
->
refreshFile
();
}
if
(
empty
(
$file
))
{
if
(
empty
(
$file
))
{
return
null
;
return
null
;
}
elseif
(
$file
instanceof
\MongoGridFSFile
)
{
}
elseif
(
$file
instanceof
\MongoGridFSFile
)
{
return
$file
->
getBytes
();
$fileSize
=
$file
->
getSize
();
if
(
empty
(
$fileSize
))
{
return
null
;
}
else
{
return
$file
->
getBytes
();
}
}
elseif
(
$file
instanceof
UploadedFile
)
{
}
elseif
(
$file
instanceof
UploadedFile
)
{
return
file_get_contents
(
$file
->
tempName
);
return
file_get_contents
(
$file
->
tempName
);
}
elseif
(
is_string
(
$file
))
{
}
elseif
(
is_string
(
$file
))
{
...
...
extensions/mongo/file/Collection.php
View file @
77f10ed9
...
@@ -63,38 +63,27 @@ class Collection extends \yii\mongo\Collection
...
@@ -63,38 +63,27 @@ class Collection extends \yii\mongo\Collection
/**
/**
* @param string $filename name of the file to store.
* @param string $filename name of the file to store.
* @param array $metadata other metadata fields to include in the file document.
* @param array $metadata other metadata fields to include in the file document.
* @return mixed the "_id" of the saved file document. This will be a generated [[\MongoId]]
* unless an "_id" was explicitly specified in the metadata.
*/
public
function
put
(
$filename
,
$metadata
=
[])
{
return
$this
->
mongoCollection
->
put
(
$filename
,
$metadata
);
}
/**
* @param string $bytes string of bytes to store.
* @param array $metadata other metadata fields to include in the file document.
* @param array $options list of options in format: optionName => optionValue
* @param array $options list of options in format: optionName => optionValue
* @return mixed the "_id" of the saved file document. This will be a generated [[\MongoId]]
* @return mixed the "_id" of the saved file document. This will be a generated [[\MongoId]]
* unless an "_id" was explicitly specified in the metadata.
* unless an "_id" was explicitly specified in the metadata.
*/
*/
public
function
storeBytes
(
$bytes
,
$metadata
=
[],
$options
=
[])
public
function
insertFile
(
$filename
,
$metadata
=
[],
$options
=
[])
{
{
$options
=
array_merge
([
'w'
=>
1
],
$options
);
$options
=
array_merge
([
'w'
=>
1
],
$options
);
return
$this
->
mongoCollection
->
store
Bytes
(
$bytes
,
$metadata
,
$options
);
return
$this
->
mongoCollection
->
store
File
(
$filename
,
$metadata
,
$options
);
}
}
/**
/**
* @param string $
filename name of the file
to store.
* @param string $
bytes string of bytes
to store.
* @param array $metadata other metadata fields to include in the file document.
* @param array $metadata other metadata fields to include in the file document.
* @param array $options list of options in format: optionName => optionValue
* @param array $options list of options in format: optionName => optionValue
* @return mixed the "_id" of the saved file document. This will be a generated [[\MongoId]]
* @return mixed the "_id" of the saved file document. This will be a generated [[\MongoId]]
* unless an "_id" was explicitly specified in the metadata.
* unless an "_id" was explicitly specified in the metadata.
*/
*/
public
function
storeFile
(
$filename
,
$metadata
=
[],
$options
=
[])
public
function
insertFileContent
(
$bytes
,
$metadata
=
[],
$options
=
[])
{
{
$options
=
array_merge
([
'w'
=>
1
],
$options
);
$options
=
array_merge
([
'w'
=>
1
],
$options
);
return
$this
->
mongoCollection
->
store
File
(
$filename
,
$metadata
,
$options
);
return
$this
->
mongoCollection
->
store
Bytes
(
$bytes
,
$metadata
,
$options
);
}
}
/**
/**
...
@@ -104,7 +93,7 @@ class Collection extends \yii\mongo\Collection
...
@@ -104,7 +93,7 @@ class Collection extends \yii\mongo\Collection
* @return mixed the "_id" of the saved file document. This will be a generated [[\MongoId]]
* @return mixed the "_id" of the saved file document. This will be a generated [[\MongoId]]
* unless an "_id" was explicitly specified in the metadata.
* unless an "_id" was explicitly specified in the metadata.
*/
*/
public
function
store
Uploads
(
$name
,
$metadata
=
[])
public
function
insert
Uploads
(
$name
,
$metadata
=
[])
{
{
return
$this
->
mongoCollection
->
storeUpload
(
$name
,
$metadata
);
return
$this
->
mongoCollection
->
storeUpload
(
$name
,
$metadata
);
}
}
...
...
tests/unit/extensions/mongo/ActiveRecordTest.php
View file @
77f10ed9
...
@@ -156,8 +156,7 @@ class ActiveRecordTest extends MongoTestCase
...
@@ -156,8 +156,7 @@ class ActiveRecordTest extends MongoTestCase
// updateAll
// updateAll
$pk
=
[
'_id'
=>
$record
->
_id
];
$pk
=
[
'_id'
=>
$record
->
_id
];
//$ret = Customer::updateAll(['status' => 55], $pk);
$ret
=
Customer
::
updateAll
([
'status'
=>
55
],
$pk
);
$ret
=
Customer
::
updateAll
([
'$set'
=>
[
'status'
=>
55
]],
$pk
);
$this
->
assertEquals
(
1
,
$ret
);
$this
->
assertEquals
(
1
,
$ret
);
$record
=
Customer
::
find
(
$pk
);
$record
=
Customer
::
find
(
$pk
);
$this
->
assertEquals
(
55
,
$record
->
status
);
$this
->
assertEquals
(
55
,
$record
->
status
);
...
...
tests/unit/extensions/mongo/file/ActiveRecordTest.php
View file @
77f10ed9
...
@@ -43,7 +43,7 @@ class ActiveRecordTest extends MongoTestCase
...
@@ -43,7 +43,7 @@ class ActiveRecordTest extends MongoTestCase
'status'
=>
$i
,
'status'
=>
$i
,
];
];
$content
=
'content'
.
$i
;
$content
=
'content'
.
$i
;
$record
[
'_id'
]
=
$collection
->
storeBytes
(
$content
,
$record
);
$record
[
'_id'
]
=
$collection
->
insertFileContent
(
$content
,
$record
);
$record
[
'content'
]
=
$content
;
$record
[
'content'
]
=
$content
;
$rows
[]
=
$record
;
$rows
[]
=
$record
;
}
}
...
@@ -116,4 +116,144 @@ class ActiveRecordTest extends MongoTestCase
...
@@ -116,4 +116,144 @@ class ActiveRecordTest extends MongoTestCase
$this
->
assertTrue
(
$customers
[
'1-1'
]
instanceof
CustomerFile
);
$this
->
assertTrue
(
$customers
[
'1-1'
]
instanceof
CustomerFile
);
$this
->
assertTrue
(
$customers
[
'2-2'
]
instanceof
CustomerFile
);
$this
->
assertTrue
(
$customers
[
'2-2'
]
instanceof
CustomerFile
);
}
}
public
function
testInsert
()
{
$record
=
new
CustomerFile
;
$record
->
tag
=
'new new'
;
$record
->
status
=
7
;
$this
->
assertTrue
(
$record
->
isNewRecord
);
$record
->
save
();
$this
->
assertTrue
(
$record
->
_id
instanceof
\MongoId
);
$this
->
assertFalse
(
$record
->
isNewRecord
);
$fileContent
=
$record
->
getFileContent
();
$this
->
assertEmpty
(
$fileContent
);
}
/**
* @depends testInsert
*/
public
function
testInsertFile
()
{
$record
=
new
CustomerFile
;
$record
->
tag
=
'new new'
;
$record
->
status
=
7
;
$fileName
=
__FILE__
;
$record
->
setAttribute
(
'file'
,
$fileName
);
$record
->
save
();
$this
->
assertTrue
(
$record
->
_id
instanceof
\MongoId
);
$this
->
assertFalse
(
$record
->
isNewRecord
);
$fileContent
=
$record
->
getFileContent
();
$this
->
assertEquals
(
file_get_contents
(
$fileName
),
$fileContent
);
}
/**
* @depends testInsert
*/
public
function
testInsertFileContent
()
{
$record
=
new
CustomerFile
;
$record
->
tag
=
'new new'
;
$record
->
status
=
7
;
$newFileContent
=
'Test new file content'
;
$record
->
setAttribute
(
'newFileContent'
,
$newFileContent
);
$record
->
save
();
$this
->
assertTrue
(
$record
->
_id
instanceof
\MongoId
);
$this
->
assertFalse
(
$record
->
isNewRecord
);
$fileContent
=
$record
->
getFileContent
();
$this
->
assertEquals
(
$newFileContent
,
$fileContent
);
}
/**
* @depends testInsert
*/
public
function
testUpdate
()
{
$record
=
new
CustomerFile
;
$record
->
tag
=
'new new'
;
$record
->
status
=
7
;
$record
->
save
();
// save
$record
=
CustomerFile
::
find
(
$record
->
_id
);
$this
->
assertTrue
(
$record
instanceof
CustomerFile
);
$this
->
assertEquals
(
7
,
$record
->
status
);
$this
->
assertFalse
(
$record
->
isNewRecord
);
$record
->
status
=
9
;
$record
->
save
();
$this
->
assertEquals
(
9
,
$record
->
status
);
$this
->
assertFalse
(
$record
->
isNewRecord
);
$record2
=
CustomerFile
::
find
(
$record
->
_id
);
$this
->
assertEquals
(
9
,
$record2
->
status
);
// updateAll
$pk
=
[
'_id'
=>
$record
->
_id
];
$ret
=
CustomerFile
::
updateAll
([
'status'
=>
55
],
$pk
);
$this
->
assertEquals
(
1
,
$ret
);
$record
=
CustomerFile
::
find
(
$pk
);
$this
->
assertEquals
(
55
,
$record
->
status
);
}
/**
* @depends testUpdate
* @depends testInsertFileContent
*/
public
function
testUpdateFile
()
{
$record
=
new
CustomerFile
;
$record
->
tag
=
'new new'
;
$record
->
status
=
7
;
$newFileContent
=
'Test new file content'
;
$record
->
setAttribute
(
'newFileContent'
,
$newFileContent
);
$record
->
save
();
$updateFileName
=
__FILE__
;
$record
=
CustomerFile
::
find
(
$record
->
_id
);
$record
->
setAttribute
(
'file'
,
$updateFileName
);
$record
->
status
=
55
;
$record
->
save
();
$this
->
assertEquals
(
file_get_contents
(
$updateFileName
),
$record
->
getFileContent
());
$record2
=
CustomerFile
::
find
(
$record
->
_id
);
$this
->
assertEquals
(
$record
->
status
,
$record2
->
status
);
$this
->
assertEquals
(
file_get_contents
(
$updateFileName
),
$record2
->
getFileContent
());
}
/**
* @depends testUpdate
* @depends testInsertFileContent
*/
public
function
testUpdateFileContent
()
{
$record
=
new
CustomerFile
;
$record
->
tag
=
'new new'
;
$record
->
status
=
7
;
$newFileContent
=
'Test new file content'
;
$record
->
setAttribute
(
'newFileContent'
,
$newFileContent
);
$record
->
save
();
$updateFileContent
=
'New updated file content'
;
$record
=
CustomerFile
::
find
(
$record
->
_id
);
$record
->
setAttribute
(
'newFileContent'
,
$updateFileContent
);
$record
->
status
=
55
;
$record
->
save
();
$this
->
assertEquals
(
$updateFileContent
,
$record
->
getFileContent
());
$record2
=
CustomerFile
::
find
(
$record
->
_id
);
$this
->
assertEquals
(
$record
->
status
,
$record2
->
status
);
$this
->
assertEquals
(
$updateFileContent
,
$record2
->
getFileContent
());
}
}
}
\ No newline at end of file
tests/unit/extensions/mongo/file/CollectionTest.php
View file @
77f10ed9
...
@@ -32,12 +32,12 @@ class CollectionTest extends MongoTestCase
...
@@ -32,12 +32,12 @@ class CollectionTest extends MongoTestCase
$this
->
assertTrue
(
$cursor
instanceof
\MongoGridFSCursor
);
$this
->
assertTrue
(
$cursor
instanceof
\MongoGridFSCursor
);
}
}
public
function
test
Store
File
()
public
function
test
Insert
File
()
{
{
$collection
=
$this
->
getConnection
()
->
getFileCollection
();
$collection
=
$this
->
getConnection
()
->
getFileCollection
();
$filename
=
__FILE__
;
$filename
=
__FILE__
;
$id
=
$collection
->
store
File
(
$filename
);
$id
=
$collection
->
insert
File
(
$filename
);
$this
->
assertTrue
(
$id
instanceof
\MongoId
);
$this
->
assertTrue
(
$id
instanceof
\MongoId
);
$files
=
$this
->
findAll
(
$collection
);
$files
=
$this
->
findAll
(
$collection
);
...
@@ -49,12 +49,12 @@ class CollectionTest extends MongoTestCase
...
@@ -49,12 +49,12 @@ class CollectionTest extends MongoTestCase
$this
->
assertEquals
(
file_get_contents
(
$filename
),
$file
->
getBytes
());
$this
->
assertEquals
(
file_get_contents
(
$filename
),
$file
->
getBytes
());
}
}
public
function
test
StoreBytes
()
public
function
test
InsertFileContent
()
{
{
$collection
=
$this
->
getConnection
()
->
getFileCollection
();
$collection
=
$this
->
getConnection
()
->
getFileCollection
();
$bytes
=
'Test file content'
;
$bytes
=
'Test file content'
;
$id
=
$collection
->
storeBytes
(
$bytes
);
$id
=
$collection
->
insertFileContent
(
$bytes
);
$this
->
assertTrue
(
$id
instanceof
\MongoId
);
$this
->
assertTrue
(
$id
instanceof
\MongoId
);
$files
=
$this
->
findAll
(
$collection
);
$files
=
$this
->
findAll
(
$collection
);
...
@@ -66,14 +66,14 @@ class CollectionTest extends MongoTestCase
...
@@ -66,14 +66,14 @@ class CollectionTest extends MongoTestCase
}
}
/**
/**
* @depends test
StoreBytes
* @depends test
InsertFileContent
*/
*/
public
function
testGet
()
public
function
testGet
()
{
{
$collection
=
$this
->
getConnection
()
->
getFileCollection
();
$collection
=
$this
->
getConnection
()
->
getFileCollection
();
$bytes
=
'Test file content'
;
$bytes
=
'Test file content'
;
$id
=
$collection
->
storeBytes
(
$bytes
);
$id
=
$collection
->
insertFileContent
(
$bytes
);
$file
=
$collection
->
get
(
$id
);
$file
=
$collection
->
get
(
$id
);
$this
->
assertTrue
(
$file
instanceof
\MongoGridFSFile
);
$this
->
assertTrue
(
$file
instanceof
\MongoGridFSFile
);
...
@@ -88,7 +88,7 @@ class CollectionTest extends MongoTestCase
...
@@ -88,7 +88,7 @@ class CollectionTest extends MongoTestCase
$collection
=
$this
->
getConnection
()
->
getFileCollection
();
$collection
=
$this
->
getConnection
()
->
getFileCollection
();
$bytes
=
'Test file content'
;
$bytes
=
'Test file content'
;
$id
=
$collection
->
storeBytes
(
$bytes
);
$id
=
$collection
->
insertFileContent
(
$bytes
);
$this
->
assertTrue
(
$collection
->
delete
(
$id
));
$this
->
assertTrue
(
$collection
->
delete
(
$id
));
...
...
tests/unit/extensions/mongo/file/QueryTest.php
View file @
77f10ed9
...
@@ -29,7 +29,7 @@ class QueryTest extends MongoTestCase
...
@@ -29,7 +29,7 @@ class QueryTest extends MongoTestCase
{
{
$collection
=
$this
->
getConnection
()
->
getFileCollection
();
$collection
=
$this
->
getConnection
()
->
getFileCollection
();
for
(
$i
=
1
;
$i
<=
10
;
$i
++
)
{
for
(
$i
=
1
;
$i
<=
10
;
$i
++
)
{
$collection
->
storeBytes
(
'content'
.
$i
,
[
$collection
->
insertFileContent
(
'content'
.
$i
,
[
'filename'
=>
'name'
.
$i
,
'filename'
=>
'name'
.
$i
,
'file_index'
=>
$i
,
'file_index'
=>
$i
,
]);
]);
...
...
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