-
Notifications
You must be signed in to change notification settings - Fork 429
Refactor db code #1143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor db code #1143
Conversation
Signed-off-by: Kirtan Gajjar <[email protected]>
Signed-off-by: Kirtan Gajjar <[email protected]>
Fix function name in where Signed-off-by: Kirtan Gajjar <[email protected]>
Fix EE_Site_Command Signed-off-by: Kirtan Gajjar <[email protected]>
Signed-off-by: Kirtan Gajjar <[email protected]>
Signed-off-by: Kirtan Gajjar <[email protected]>
Signed-off-by: Kirtan Gajjar <[email protected]>
Signed-off-by: Kirtan Gajjar <[email protected]>
Signed-off-by: Kirtan Gajjar <[email protected]>
Signed-off-by: Kirtan Gajjar <[email protected]>
Signed-off-by: Kirtan Gajjar <[email protected]>
Signed-off-by: Kirtan Gajjar <[email protected]>
Signed-off-by: Kirtan Gajjar <[email protected]>
Signed-off-by: Kirtan Gajjar <[email protected]>
Correct __get method Signed-off-by: Kirtan Gajjar <[email protected]>
Signed-off-by: Kirtan Gajjar <[email protected]>
Signed-off-by: Kirtan Gajjar <[email protected]>
php/EE/Model/Base.php
Outdated
| * | ||
| * @throws \Exception | ||
| * | ||
| * @return bool Model deleted` successfully |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"deleted`" typo.
php/EE/Model/Base.php
Outdated
| EE::db() | ||
| ->table( static::$table ) | ||
| ->where( $column, $value ) | ||
| ->all() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check first if you're getting anything here or not then use the many_array_to_model function to parse.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added Exception instead.
php/EE/Model/Base.php
Outdated
| /** | ||
| * Base EE Model class. | ||
| */ | ||
| abstract class Base extends \ArrayObject |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this need to be extended from ArrayObject?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because
- It would allow us to access any object property from array notation
- It allows us to use array_* functions on array of models.
i.e.
$sites = Sites::all();
$site_paths = array_column( $sites, 'site_fs_path' );
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess you can use ArrayObject https://github.com/EasyEngine/easyengine/pull/1143/files#diff-9a84c75c5309379993ff2ed9362b8772R309 instead of that.
return new static( $arr );
Since you're not going to need other functions of Base.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually the idea is that you'll be able to use both the methods you've defined on Model as well as methods of ArrayObject.
i.e. Its possible to do
$sites = Site::all();
$sites[0]->add_default_cron(); //some method defined on site model
$sites[0]->delete();
Also if we return an ArrayObject we won't be able to update models easily as now like
$site = site::find('abc.com');
$site->is_ssl = true;
$site->save(); //If ArrayObject is returned we won't be able to do this.
Hence It's best to use return Model the model and have the model extend ArrayObject
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Verified. There's no need to extend ArrayObject hence removing it.
php/EE/Model/Base.php
Outdated
| * Base EE Model class. | ||
| */ | ||
| abstract class Base extends \ArrayObject | ||
| { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move this bracket to above line[L10], like as we are using it other places.
|
|
||
| // Getting the type of the respective site-name from database. | ||
| $type = EE::db()::get_site_command( $site_name ); | ||
| $type = Site::find( $site_name )->site_type; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this find method have false in return. So please check if you're getting proper response then try to use the site_type from it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There' a function called above - auto_site_name. It ensures site is present in database. If it isn't it will throw error. Hence Site::find() will never return false.
php/EE/Model/Site.php
Outdated
| * | ||
| * @return string type of site. | ||
| */ | ||
| public static function get_site_command( $site_name ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you want to get the site type from this function as @return comment. I think this function name should be get_site_type only. or maybe update @return comment to: type/command of site
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed the function as it isn't necessary now
| * | ||
| * @param array $data in key value pair. | ||
| * @return array Record | ||
| * @throws Exception |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move this @throws before @return in this file or if there are other places in this PR for consistency in some place you have added @throws before @return and in some place after that. Please add it before @return.
php/class-ee-db.php
Outdated
| */ | ||
| public static function get_site_command( $site_name ) { | ||
| private function get_where_fragment( $condition ) { | ||
| $column = $condition[0]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add one condition before using $condition[0] here and return if below condition is not satisfied.
if ( ! is_array( $condition ) || empty( $condition ) || count( $condition ) > 3 ) {
// return or exeption.....
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added below
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why below, what's the point checking empty after that ???
you know that if you try to access index 0 value if the variable is empty it will give you warning/error right? That's why I specifically asked you to add it before this line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ohh... Okay. I've moved the condition to the top and added array type hint in parameter(and removed is_array from if condition ).
php/class-ee-site.php
Outdated
| */ | ||
| abstract class EE_Site_Command { | ||
| abstract class EE_Site_Command | ||
| { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why this bracket is again in new line? Please check it all places and correct it.
php/class-ee-site.php
Outdated
|
|
||
| $this->site['type'] = $site->site_type; | ||
| $this->site['root'] = $site->site_fs_path; | ||
| $this->le = null !== $site->site_ssl; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add a bracket here:
$this->le = ( null !== $site->site_ssl );
At first look it seems we have assign this.... $this->le = null
|
|
||
| $set_clause = implode( $set_keys, ' = ?, ' ) . ' = ?'; | ||
|
|
||
| $query = "UPDATE $this->tables SET $set_clause{$this->where['query_string']}"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think here you should also add the modified_time for site table. or you're doing it in all places where you update any data in site table?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class-ee-db.php is a generic class and will be used by all tables. So instead of adding modified_date here, I will add it in Site model's save() method. (That's what we're using everywhere to update a site)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that should be the case.
Signed-off-by: Kirtan Gajjar <[email protected]>
php/EE/Model/Base.php
Outdated
| * | ||
| * @throws \Exception | ||
| * | ||
| * @param string|int $index Name of property to check |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Signed-off-by: Kirtan Gajjar <[email protected]>
Signed-off-by: Kirtan Gajjar <[email protected]>
Signed-off-by: Kirtan Gajjar <[email protected]>
Signed-off-by: Kirtan Gajjar <[email protected]>
Signed-off-by: Kirtan Gajjar <[email protected]>
Signed-off-by: Kirtan Gajjar <[email protected]>
Also corrected array usage Signed-off-by: Kirtan Gajjar <[email protected]>
For why we needed refactor, see - #1141.
This PR refactors methods of EE_DB class methods for better support of multiple tables and also migrates from Sqlite3 to PDO. Along with it, it also handles - #1153 (Refactor db schema) and #1161 (Add model class) since they are related and have to some extent overlapping work.
Related PRs -
EasyEngine/site-command#96
EasyEngine/site-type-wp#16
EasyEngine/cron-command#13
Closes #1153, Closes #1161, Closes #1141