Skip to content

Conversation

@kirtangajjar
Copy link
Contributor

@kirtangajjar kirtangajjar commented Aug 2, 2018

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

@kirtangajjar kirtangajjar changed the title WIP: Refactoring db code WIP: Refactor db code Aug 2, 2018
@kirtangajjar kirtangajjar self-assigned this Aug 6, 2018
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]>
Correct __get method

Signed-off-by: Kirtan Gajjar <[email protected]>
Signed-off-by: Kirtan Gajjar <[email protected]>
@kirtangajjar kirtangajjar changed the title WIP: Refactor db code Refactor db code Aug 13, 2018
@rahulsprajapati rahulsprajapati self-requested a review August 13, 2018 11:48
*
* @throws \Exception
*
* @return bool Model deleted` successfully
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"deleted`" typo.

EE::db()
->table( static::$table )
->where( $column, $value )
->all()
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added Exception instead.

/**
* Base EE Model class.
*/
abstract class Base extends \ArrayObject
Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because

  1. It would allow us to access any object property from array notation
  2. It allows us to use array_* functions on array of models.
    i.e.
$sites = Sites::all();
$site_paths = array_column( $sites, 'site_fs_path' );

Copy link
Contributor

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.

Copy link
Contributor Author

@kirtangajjar kirtangajjar Aug 16, 2018

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

Copy link
Contributor Author

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.

* Base EE Model class.
*/
abstract class Base extends \ArrayObject
{
Copy link
Contributor

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;
Copy link
Contributor

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.

Copy link
Contributor Author

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.

*
* @return string type of site.
*/
public static function get_site_command( $site_name ) {
Copy link
Contributor

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

Copy link
Contributor Author

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
Copy link
Contributor

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.

ref: https://github.com/EasyEngine/easyengine/pull/1143/files#diff-cfaa88ab8e5a4e9f3e67c13e5d049636R66

*/
public static function get_site_command( $site_name ) {
private function get_where_fragment( $condition ) {
$column = $condition[0];
Copy link
Contributor

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.....
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added below

Copy link
Contributor

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.

Copy link
Contributor Author

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 ).

*/
abstract class EE_Site_Command {
abstract class EE_Site_Command
{
Copy link
Contributor

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.


$this->site['type'] = $site->site_type;
$this->site['root'] = $site->site_fs_path;
$this->le = null !== $site->site_ssl;
Copy link
Contributor

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']}";
Copy link
Contributor

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?

Copy link
Contributor Author

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)

Copy link
Contributor

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]>
*
* @throws \Exception
*
* @param string|int $index Name of property to check
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why @param after @throws ?

Signed-off-by: Kirtan Gajjar <[email protected]>
Also corrected array usage

Signed-off-by: Kirtan Gajjar <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants