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
c783e9fd
Commit
c783e9fd
authored
Dec 05, 2013
by
Paul Klimov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Mongo File Active Record refactored.
parent
04fb75b6
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
45 additions
and
43 deletions
+45
-43
ActiveRecord.php
extensions/mongo/file/ActiveRecord.php
+45
-43
No files found.
extensions/mongo/file/ActiveRecord.php
View file @
c783e9fd
...
...
@@ -88,26 +88,18 @@ class ActiveRecord extends \yii\mongo\ActiveRecord
}
}
$collection
=
static
::
getCollection
();
if
(
array_key_exists
(
'newFileContent'
,
$values
))
{
$fileContent
=
$values
[
'newFileContent'
];
unset
(
$values
[
'newFileContent'
]);
unset
(
$values
[
'file'
]);
$newId
=
$collection
->
insertFileContent
(
$fileContent
,
$values
);
}
elseif
(
array_key_exists
(
'file'
,
$values
))
{
$file
=
$values
[
'file'
];
if
(
$file
instanceof
UploadedFile
)
{
$fileName
=
$file
->
tempName
;
}
elseif
(
is_string
(
$file
))
{
if
(
file_exists
(
$file
))
{
$fileName
=
$file
;
}
else
{
throw
new
InvalidParamException
(
"File '
{
$file
}
' does not exist."
);
}
}
else
{
throw
new
InvalidParamException
(
'Unsupported type of "file" attribute.'
);
}
if
(
isset
(
$values
[
'newFileContent'
]))
{
$newFileContent
=
$values
[
'newFileContent'
];
unset
(
$values
[
'newFileContent'
]);
}
if
(
isset
(
$values
[
'file'
]))
{
$newFile
=
$values
[
'file'
];
unset
(
$values
[
'file'
]);
}
if
(
isset
(
$newFileContent
))
{
$newId
=
$collection
->
insertFileContent
(
$newFileContent
,
$values
);
}
elseif
(
isset
(
$newFile
))
{
$fileName
=
$this
->
extractFileName
(
$newFile
);
$newId
=
$collection
->
insertFile
(
$fileName
,
$values
);
}
else
{
$newId
=
$collection
->
insert
(
$values
);
...
...
@@ -136,35 +128,24 @@ class ActiveRecord extends \yii\mongo\ActiveRecord
}
$collection
=
static
::
getCollection
();
if
(
array_key_exists
(
'newFileContent'
,
$values
))
{
$
f
ileContent
=
$values
[
'newFileContent'
];
if
(
isset
(
$values
[
'newFileContent'
]
))
{
$
newF
ileContent
=
$values
[
'newFileContent'
];
unset
(
$values
[
'newFileContent'
]);
}
if
(
isset
(
$values
[
'file'
]))
{
$newFile
=
$values
[
'file'
];
unset
(
$values
[
'file'
]);
$values
[
'_id'
]
=
$this
->
getAttribute
(
'_id'
);
$this
->
deleteInternal
();
$collection
->
insertFileContent
(
$fileContent
,
$values
);
$rows
=
1
;
$this
->
setAttribute
(
'newFileContent'
,
null
);
$this
->
setAttribute
(
'file'
,
null
);
}
elseif
(
array_key_exists
(
'file'
,
$values
))
{
$file
=
$values
[
'file'
];
if
(
$file
instanceof
UploadedFile
)
{
$fileName
=
$file
->
tempName
;
}
elseif
(
is_string
(
$file
))
{
if
(
file_exists
(
$file
))
{
$fileName
=
$file
;
}
else
{
throw
new
InvalidParamException
(
"File '
{
$file
}
' does not exist."
);
}
}
if
(
isset
(
$newFileContent
)
||
isset
(
$newFile
))
{
$rows
=
$this
->
deleteInternal
();
$insertValues
=
$values
;
$insertValues
[
'_id'
]
=
$this
->
getAttribute
(
'_id'
);
if
(
isset
(
$newFileContent
))
{
$collection
->
insertFileContent
(
$newFileContent
,
$insertValues
);
}
else
{
throw
new
InvalidParamException
(
'Unsupported type of "file" attribute.'
);
$fileName
=
$this
->
extractFileName
(
$newFile
);
$collection
->
insertFile
(
$fileName
,
$insertValues
);
}
unset
(
$values
[
'newFileContent'
]);
unset
(
$values
[
'file'
]);
$values
[
'_id'
]
=
$this
->
getAttribute
(
'_id'
);
$this
->
deleteInternal
();
$collection
->
insertFile
(
$fileName
,
$values
);
$rows
=
1
;
$this
->
setAttribute
(
'newFileContent'
,
null
);
$this
->
setAttribute
(
'file'
,
null
);
}
else
{
...
...
@@ -192,6 +173,27 @@ class ActiveRecord extends \yii\mongo\ActiveRecord
}
/**
* Extracts filename from given raw file value.
* @param mixed $file raw file value.
* @return string file name.
* @throws \yii\base\InvalidParamException on invalid file value.
*/
protected
function
extractFileName
(
$file
)
{
if
(
$file
instanceof
UploadedFile
)
{
return
$file
->
tempName
;
}
elseif
(
is_string
(
$file
))
{
if
(
file_exists
(
$file
))
{
return
$file
;
}
else
{
throw
new
InvalidParamException
(
"File '
{
$file
}
' does not exist."
);
}
}
else
{
throw
new
InvalidParamException
(
'Unsupported type of "file" attribute.'
);
}
}
/**
* Refreshes the [[file]] attribute from file collection, using current primary key.
* @return \MongoGridFSFile|null refreshed file value.
*/
...
...
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