Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ The following documentation uses `eden()` in its example reference. Enabling thi
Eden\Mysql\Index::i();
```

When using composer, there is not an easy way to access functions from packages. As a workaround, adding this constant in your code will allow `eden()` to be available after.
When using composer, there is not an easy way to access functions from packages. As a workaround, adding this constant in your code will allow `eden()` to be available after.

```php
Eden::DECORATOR;
Expand All @@ -54,7 +54,7 @@ eden()->inspect('Hello World');
**Figure 1. Database Connection Information**

```php
$database = eden('mysql', '[HOST]' ,'[DBNAME]', '[USER]', '[PASS]'); //instantiate
$database = eden('mysql', '[HOST]' ,'[DBNAME]', '[USER]', '[PASS]', '[SOCKET]'); //instantiate
```

Simply put, we first instantiate the class giving the host, database name, user name and password in the figure above. There are no other requirements and you can start using methods immediately.
Expand Down Expand Up @@ -102,12 +102,12 @@ Figure 4. Data Manilpulation
$settings = array(
'user_name' => 'Chris'
'user_email' => 'myemail@mail.com');

$filter[] = array('user_id=%s', 1);

// inserts row into 'user' table
$database->insertRow('user', $settings);
// updates rows in 'user' table where user_id is
// updates rows in 'user' table where user_id is
$database->updateRows('user', $settings, $filter);
// delete rows in 'user' table where user_id is 1
$database->deleteRows('user', $filter);
Expand All @@ -123,7 +123,7 @@ Inserting data is pretty trivial. We included 2 ways to insert data. Like getRow
```php
$settings = array('user_name' => 'Chris', 'user_email' => 'myemail@mail.com');
$database->insertRow('user', $settings); // inserts row into 'user' table

$settings = array();
$settings[] = array('user_name' => 'Chris', 'user_email' => 'myemail@mail.com');
$settings[] = array('user_name' => 'Dan', 'user_email' => 'myemail2@mail.com');
Expand Down Expand Up @@ -243,7 +243,7 @@ When your happy with your query you can retrieve the results in 3 ways as descri
->getCollection()
```

`Figure 10` shows three ways to get the results, the first way `getTotal()`, will retrieve the total number and does not consider pagination elements. `getRows()` will simply return a raw array. `getCollection()` will return you an object with the results for further manipulation.
`Figure 10` shows three ways to get the results, the first way `getTotal()`, will retrieve the total number and does not consider pagination elements. `getRows()` will simply return a raw array. `getCollection()` will return you an object with the results for further manipulation.

====

Expand All @@ -266,12 +266,12 @@ foreach($collection as $model) {
echo $model->getUserName().' ';
echo $model['user_email'];
}

//access as array
echo $collection[0]['user_name'];
//set as array
$collection[0]['user_email'] = 'my@email.com';
$collection[0]['user_email'] = 'my@email.com';

$collection->save('user', $database); //save to 'user' table in database
//only relavent columns will be saved
//for all rows
Expand All @@ -283,7 +283,7 @@ Some other utility methods not covered by th above examples are date formating a

```php
//formats a date column
$collection->formatTime('post_created', 'F d, y g:ia');
$collection->formatTime('post_created', 'F d, y g:ia');

//for each row, copy the value of post_user to the user_id column
$collection->copy('post_user', 'user_id');
Expand Down Expand Up @@ -313,15 +313,15 @@ In *Eden*, we managed to loosely define models which takes off the restrictivene
```php
$model->setUserName('Chris'); //set user name
$model->getUserEmail(); // returns user email

//$model->setAnyThing() // set or get any abstract key

echo $model['user_name']; //access as array
$model['user_email'] = 'my@email.com'; //set as array

echo $model->user_name; //access as object
$model->user_name = 'my@email.com'; //set as object

$model->save('user', $database); //save to 'user' table in database
//only relavent columns will be saved
```
Expand All @@ -340,7 +340,7 @@ $row = array(
'post_user' => 1,
'post_title' => 'My Post',
'post_detail' => 'This is my new article');

$db->model($row)->save('user')->save('post');
```

Expand Down Expand Up @@ -397,8 +397,8 @@ Contributions to *Eden* are following the Github work flow. Please read up befor
##Setting up your machine with the Eden repository and your fork

1. Fork the repository
2. Fire up your local terminal create a new branch from the `v4` branch of your
fork with a branch name describing what your changes are.
2. Fire up your local terminal create a new branch from the `v4` branch of your
fork with a branch name describing what your changes are.
Possible branch name types:
- bugfix
- feature
Expand Down
107 changes: 59 additions & 48 deletions src/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,23 @@ class Index extends \Eden\Sql\Index
* @var string|null $pass Database password
*/
protected $pass = null;


/**
* @var string|null $socket Socket connection
*/
protected $socket = null;

/**
* Construct: Store connection information
*
* @param *string $host Database host
* @param *string|null $name Database name
* @param *string|null $user Database user name
* @param string|null $pass Database password
* @param number|null $port Database port
* @param *string $host Database host
* @param *string|null $name Database name
* @param *string|null $user Database user name
* @param string|null $pass Database password
* @param number|null $port Database port
* @param string|null $socket Database socket
*/
public function __construct($host, $name, $user, $pass = null, $port = null)
public function __construct($host, $name, $user, $pass = null, $port = null, $socket = null)
{
//argument test
Argument::i()
Expand All @@ -67,15 +73,18 @@ public function __construct($host, $name, $user, $pass = null, $port = null)
//Argument 4 must be a string or null
->test(4, 'string', 'null')
//Argument 5 must be a number or null
->test(5, 'numeric', 'null');

->test(5, 'numeric', 'null')
//Argument 6 must be a string or null
->test(6, 'string', 'null');

$this->host = $host;
$this->name = $name;
$this->user = $user;
$this->pass = $pass;
$this->port = $port;
$this->socket = $socket;
}

/**
* Returns the alter query builder
*
Expand All @@ -87,10 +96,10 @@ public function alter($name = null)
{
//Argument 1 must be a string or null
Argument::i()->test(1, 'string', 'null');

return Alter::i($name);
}

/**
* Returns the create query builder
*
Expand All @@ -102,10 +111,10 @@ public function create($name = null)
{
//Argument 1 must be a string or null
Argument::i()->test(1, 'string', 'null');

return Create::i($name);
}

/**
* Connects to the database
*
Expand All @@ -115,24 +124,26 @@ public function create($name = null)
*/
public function connect(array $options = array())
{
$host = $port = null;
$host = null;

if (!is_null($this->host)) {
$host = 'host='.$this->host.';';
if (!is_null($this->port)) {
$port = 'port='.$this->port.';';
$host .= 'port='.$this->port.';';
}
} elseif (!is_null($this->socket)) {
$host = $this->socket.';';
}
$connection = 'mysql:'.$host.$port.'dbname='.$this->name;

$connection = 'mysql:'.$host.'dbname='.$this->name;

$this->connection = new \PDO($connection, $this->user, $this->pass, $options);

$this->trigger('mysql-connect');

return $this;
}

/**
* Returns the Subselect query builder
*
Expand All @@ -145,10 +156,10 @@ public function subselect($parentQuery, $select = '*')
{
//Argument 2 must be a string
Argument::i()->test(2, 'string');

return Subselect::i($parentQuery, $select);
}

/**
* Returns the alter query builder
*
Expand All @@ -158,7 +169,7 @@ public function utility()
{
return Utility::i();
}

/**
* Returns the columns and attributes given the table name
*
Expand All @@ -171,9 +182,9 @@ public function getColumns($table, $filters = null)
{
//Argument 1 must be a string
Argument::i()->test(1, 'string');

$query = $this->utility();

if (is_array($filters)) {
foreach ($filters as $i => $filter) {
//array('post_id=%s AND post_title IN %s', 123, array('asd'));
Expand All @@ -182,11 +193,11 @@ public function getColumns($table, $filters = null)
$filters[$i] = vsprintf($format, $filter);
}
}

$query->showColumns($table, $filters);
return $this->query($query, $this->getBinds());
}

/**
* Peturns the primary key name given the table
*
Expand All @@ -198,12 +209,12 @@ public function getPrimaryKey($table)
{
//Argument 1 must be a string
Argument::i()->test(1, 'string');

$query = $this->utility();
$results = $this->getColumns($table, "`Key` = 'PRI'");
return isset($results[0]['Field']) ? $results[0]['Field'] : null;
}

/**
* Returns the whole enitre schema and rows
* of the current databse
Expand All @@ -217,10 +228,10 @@ public function getSchema()
foreach ($tables as $table) {
$backup[] = $this->getBackup();
}

return implode("\n\n", $backup);
}

/**
* Returns a listing of tables in the DB
*
Expand All @@ -232,7 +243,7 @@ public function getTables($like = null)
{
//Argument 1 must be a string or null
Argument::i()->test(1, 'string', 'null');

$query = $this->utility();
$like = $like ? $this->bind($like) : null;
$results = $this->query($query->showTables($like), $q->getBinds());
Expand All @@ -243,10 +254,10 @@ public function getTables($like = null)
break;
}
}

return $newResults;
}

/**
* Returns the whole enitre schema and rows
* of the current table
Expand All @@ -259,7 +270,7 @@ public function getTableSchema($table)
{
//Argument 1 must be a string
Argument::i()->test(1, 'string');

$backup = array();
//get the schema
$schema = $this->getColumns($table);
Expand All @@ -270,15 +281,15 @@ public function getTableSchema($table)
//first try to parse what we can from each field
$fieldTypeArray = explode(' ', $field['Type']);
$typeArray = explode('(', $fieldTypeArray[0]);

$type = $typeArray[0];
$length = str_replace(')', '', $typeArray[1]);
$attribute = isset($fieldTypeArray[1]) ? $fieldTypeArray[1] : null;

$null = strtolower($field['Null']) == 'no' ? false : true;

$increment = strtolower($field['Extra']) == 'auto_increment' ? true : false;

//lets now add a field to our schema class
$q->addField($field['Field'], array(
'type' => $type,
Expand All @@ -287,7 +298,7 @@ public function getTableSchema($table)
'null' => $null,
'default' => $field['Default'],
'auto_increment' => $increment));

//set keys where found
switch ($field['Key']) {
case 'PRI':
Expand All @@ -301,14 +312,14 @@ public function getTableSchema($table)
break;
}
}

//store the query but dont run it
$backup[] = $query;
}

//get the rows
$rows = $this->query($this->select->from($table)->getQuery());

if (count($rows)) {
//lets build an insert query
$query = $this->insert($table);
Expand All @@ -317,11 +328,11 @@ public function getTableSchema($table)
$query->set($key, $this->getBinds($value), $index);
}
}

//store the query but dont run it
$backup[] = $query->getQuery(true);
}

return implode("\n\n", $backup);
}
}