2017 © Pedro Peláez
 

windwalker-package datamapper

Windwalker DataMapper package

image

windwalker/datamapper

Windwalker DataMapper package

  • Saturday, July 28, 2018
  • by asika32764
  • Repository
  • 3 Watchers
  • 2 Stars
  • 3,554 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 56 Versions
  • 2 % Grown

The README.md

Windwalker DataMapper

Installation via Composer

Add this to the require block in your composer.json., (*1)

``` json { "require": { "windwalker/datamapper": "~3.0" } }, (*2)


## Getting Started ### Prepare Windwalker Database object ``` php use Windwalker\Database\DatabaseFactory; // Make the database driver. $db = DatabaseFactory::getDbo( 'mysql', array( 'driver' => 'mysql', 'host' => 'localhost', 'user' => 'root', 'password' => 'xxxx', 'database' => 'mydb', 'prefix' => 'prefix_' ) );

The DatabaseDriver will be cached in Factory, now DataMapper will auto load database driver., (*3)

See Database, (*4)

Create DataMapper

``` php use Windwalker\DataMapper\DataMapper;, (*5)

$fooMapper = new DataMapper('#__foo');, (*6)

$fooSet = $fooMapper->find(array('id' => 1));, (*7)


Inject DB to DataMapper ``` php // $db is Windwalker DatabaseDriver $mapper = new DataMapper('table', null, $db);

Custom primary keys:, (*8)

``` php // If keep keys NULL, the default id will auto set. $mapper = new DataMapper('table'); // Keys: array('id'), (*9)

// Set custom key $mapper = new DataMapper('table', 'table_id'); // Keys: array('table_id'), (*10)

// Set multiple keys $mapper = new DataMapper('table', array('table_id', 'uuid')); // Keys: array('table_id', 'uuid'), (*11)


### Extend It You can also create a class to operate specific table: ``` php class FooMapper extends DataMapper { protected static $table = '#__foo'; protected static $keys = 'id'; } $data = (new FooMapper)->findAll();

Or using facade:, (*12)

``` php use Windwalker\DataMapper\AbstractDatabaseMapperProxy;, (*13)

abstract class FooMapper extends AbstractDatabaseMapperProxy { protected $table = '#__foo';, (*14)

protected $keys = 'id'; // Keep NULL will use default `id`

}, (*15)

$data = FooMapper::findOne(array('id' => 5, 'alias' => 'bar'));, (*16)


## Find Records Find method will fetch rows from table, and return `DataSet` class. ### find() Get id = 1 record ``` php $fooSet = $fooMapper->find(array('id' => 1));

Fetch published = 1, and sort by date, (*17)

``` php $fooSet = $fooMapper->find(array('published' => 1), 'date');, (*18)


Fetch published = 1, language = en-US, sort by `date` DESC and start with `30`, limit `10`. ``` php $fooSet = $fooMapper->find(array('published' => 1, 'language' => 'en-US'), 'date DESC', 30, 10);

Using array, will be IN condition:, (*19)

``` php $fooSet = $fooMapper->find(array('id' => array(1,2,3))); // WHERE id IN (1,2,3), (*20)


### findOne() Just return one row. ``` php $foo = $dooMapper->findOne(array('published' => 1), 'date');

findAll()

Equal to find(array(), $order, $start, $limit)., (*21)

Find With Custom Query

``` php $fooMapper = new DataMapper('#__foo');, (*22)

$fooMapper->where('a = "b"') // Simple where ->where('%n = $q', 'foo', 'bar') // Where format ->where('flower = :sakura')->bind('sakura', 'Sakura') // Bind params ->orWhere(array('c = d', 'e = f')) // AND (c=d OR e=f) ->having('...') ->limit(10, 20) // Limit, offset ->order('created DESC') // Can be array or string ->select(array('id', 'title', 'alias')) // Can be array or string ->find();, (*23)


The available query methods. - `join($type = 'LEFT', $alias, $table, $condition = null, $prefix = null)` - `leftJoin($alias, $table, $condition = null, $prefix = null)` - `rightJoin($alias, $table, $condition = null, $prefix = null)` - `nnerJoin($alias, $table, $condition = null, $prefix = null)` - `outerJoin($alias, $table, $condition = null, $prefix = null)` - `call($columns)` - `group($columns)` - `order($columns)` - `limit($limit = null, $offset = null)` - `select($columns)` - `where($conditions, ...$args)` - `orWhere($conditions)` - `having($conditions, ...$args)` - `orHaving($conditions)` - `clear($clause = null)` - `bind($key = null, $value = null, $dataType = \PDO::PARAM_STR, $length = 0, $driverOptions = array())` See [Query Format](https://github.com/ventoviro/windwalker-query#format) ## Create Records Using DataSet to wrap every data, then send this object to create() method, these data will insert to table. ### create() ``` php use Windwalker\Data\Data; use Windwalker\Data\DataSet; $data1 = new Data; $data1->title = 'Foo'; $data1->auhor = 'Magneto'; $data2 = new Data( array( 'title' => 'Bar', 'author' => 'Wolverine' ) ); $dataset = new DataSet(array($data1, $data2)); $return = $fooMapper->create($dataset);

The return value will be whole dataset and add inserted ids., (*24)

Windwalker\Data\DataSet Object
(
    [storage:ArrayObject:private] => Array
        (
            [0] => Windwalker\Data\Data Object
                (
                    [title] => Foo
                    [auhor] => Magneto
                    [id] => 39
                )

            [1] => Windwalker\Data\Data Object
                (
                    [title] => Bar
                    [auhor] => Wolverine
                    [id] => 40
                )
        )
)

createOne()

Only insert one row, do not need DataSet., (*25)

``` php $data = new Data; $data->title = 'Foo'; $data->auhor = 'Magneto';, (*26)

$fooMapper->createOne($data);, (*27)



## Update Records Update methods help us update rows in table. ### update() ``` php use Windwalker\Data\Data; use Windwalker\Data\DataSet; $data1 = new Data; $data1->id = 1; $data1->title = 'Foo'; $data2 = new Data( array( 'id' => 2, 'title' => 'Bar' ) ); $dataset = new DataSet(array($data1, $data2)); $fooMapper->update($dataset);

updateOne()

Just update one row., (*28)

``` php $data = new Data; $data->id = 1; $data->title = 'Foo';, (*29)

$fooMapper->updateOne($data);, (*30)


### updateAll() UpdateAll is different from update method, we just send one data object, but using conditions as where to update every row match these conditions. We don't need primary key for updateAll(). ``` php $data = new Data; $data->published = 0; $fooMapper->updateAll($data, array('author' => 'Mystique'));

Delete

Delete rows by conditions., (*31)

delete()

``` php $boolean = $fooMapper->delete(array('author' => 'Jean Grey'));, (*32)


## Join Tables Use `newRelation()` to create a DataMapper and join other tables. ``` php use Windwalker\DataMapper\DataMapper; $items = DataMapper::newRelation('flower', '#__flower') ->leftJoin('author', '#__users', 'flower.user_id = author.id') ->innerJoin('category', '#__categories', array('category.lft >= flower.lft', 'category.rgt <= flower.rgt')) ->where('flower.id = 1') ->order('created DESC') ->group('category.id') ->find();

The Join query will be:, (*33)

``` sql SELECT flower.id, flower.catid, flower.title, flower.user_id, flower.meaning, flower.ordering, flower.state, flower.params, author.id AS author_id, author.name AS author_name, author.pass AS author_pass, category.id AS category_id, category.title AS category_title, category.ordering AS category_ordering, category.params AS category_params FROM #__foo AS foo LEFT JOIN #__users AS author ON foo.user_id = author.id INNER JOIN #__categories AS category ON category.lft >= foo.lft AND category.rgt <= foo.rgt WHERE flower.id = 1 ORDER BY flower.created DESC GROUP BY category.id, (*34)


Where condition will auto add alias if not provided. ``` php $fooMapper->find(array( 'foo.id' => 3 // This is correct condition 'state' => 1 // This field may cause column conflict, DataMapper will auto covert it to `foo.state` => 1 ));

Reset all tables and query:, (*35)

``` php $fooMapper->reset();, (*36)


### Using OR Condition ``` php $fooMapper->addTable( 'category', '#__categories', 'category.lft >= foo.lft OR category.rgt <= foo.rgt', 'LEFT' );

Group

``` php $fooMapper->group('category.id');, (*37)


## Compare objects Using Compare objects help us set some where conditions which hard to use array to defind. ``` php $fooSet = $fooMapper->find( array( new GteCompare('id', 5), new NeqCompare('name', 'bar') new LtCompare('published', 1), new NinCompare('catid', array(1,2,3,4,5)) ) );

This will generate where conditions like below:, (*38)

``` sql WHERE id >= '5' AND name !== 'bar' AND published < '1' AND catid NOT IN (1,2,3,4,5), (*39)


### Available compares: | Name | Description | Operator | | ---------- | -----------------| -------- | | EqCompare | Equal | `=` | | NeqCompare | Not Equal | `!=` | | GtCompare | Greater than | `>` | | GteCompare | Greater than or Equal | `>=` | | LtCompare | Less than | `<` | | LteCompare | Less than or Equal | `<=` | | InCompare | In | `IN` | | NinCompare | Not In | `IN` | ### Custom Compare ``` php echo (string) new Compare('title', '%flower%', 'LIKE');

Will be, (*40)

``` sql title LIKE %flower%, (*41)


See: https://github.com/ventoviro/windwalker-compare ## Using Data and DataSet See: https://github.com/ventoviro/windwalker-data ## Hooks Add `"windwalker/event": "~3.0"` to `composer.json`. Then we are able to use hooks after every operations. ``` php class FooListener { public function onAfterCreate(Event $event) { $result = $event['result']; // Do something } } $mapper = new DataMapper('table'); // Add object as listener $mapper->getDispatcher()->addListener(new FooListener); // Use listen() to add a callback as listener $mapper->getDispatcher()->listen('onAfterUpdate', function () { ... }); $mapper->create($dataset);

Extends DataMapper:, (*42)

``` php class SakuraMapper extends DataMapper { protected $table = 'saluras';, (*43)

public function onAfterFind(Event $event)
{
    $result = $event['result'];

    // Find some relations
}

}, (*44)

$mapper = new DataMapper('table'); $mapper->find(array('id' => 5)); ```, (*45)

Available events:, (*46)

  • onBeforeFind
  • onAfterFind
  • onBeforeFindAll
  • onAfterFindAll
  • onBeforeFindOne
  • onAfterFindOne
  • onBeforeFindColumn
  • onAfterFindColumn
  • onBeforeCreate
  • onAfterCreate
  • onBeforeCreateOne
  • onAfterCreateOne
  • onBeforeUpdate
  • onAfterUpdate
  • onBeforeUpdateOne
  • onAfterUpdateOne
  • onBeforeUpdateBatch
  • onAfterUpdateBatch
  • onBeforeSave
  • onAfterSave
  • onBeforeSaveOne
  • onAfterSaveOne
  • onBeforeFlush
  • onAfterFlush
  • onBeforeDelete
  • onAfterDelete

More about Event: Windwalker Event, (*47)

The Versions

28/07 2018

dev-master

9999999-dev https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+ LGPL-2.0-or-later

The Requires

 

The Development Requires

framework datamapper windwalker

28/07 2018

3.4.4

3.4.4.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0-or-later

The Requires

 

The Development Requires

framework datamapper windwalker

08/07 2018

3.4.3

3.4.3.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0-or-later

The Requires

 

The Development Requires

framework datamapper windwalker

07/07 2018

3.4.2

3.4.2.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0-or-later

The Requires

 

The Development Requires

framework datamapper windwalker

15/06 2018

3.4.1

3.4.1.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0-or-later

The Requires

 

The Development Requires

framework datamapper windwalker

10/06 2018

3.4

3.4.0.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0-or-later

The Requires

 

The Development Requires

framework datamapper windwalker

02/04 2018

3.3.2

3.3.2.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0-or-later

The Requires

 

The Development Requires

framework datamapper windwalker

20/02 2018

dev-test

dev-test https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+ LGPL-2.0-or-later

The Requires

 

The Development Requires

framework datamapper windwalker

20/02 2018

3.3

3.3.0.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0-or-later

The Requires

 

The Development Requires

framework datamapper windwalker

20/02 2018

3.3.1

3.3.1.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0-or-later

The Requires

 

The Development Requires

framework datamapper windwalker

29/11 2017

3.2.8

3.2.8.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

16/10 2017

3.2.7

3.2.7.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

04/09 2017

3.2.6

3.2.6.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

03/08 2017

3.2.5

3.2.5.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

25/06 2017

3.2.4

3.2.4.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

18/06 2017

3.2.3

3.2.3.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

10/06 2017

3.2.2

3.2.2.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

05/06 2017

3.2.1

3.2.1.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

23/03 2017

3.2

3.2.0.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

21/03 2017

3.1.4

3.1.4.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

21/03 2017

3.1.5

3.1.5.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

21/03 2017

3.1.6

3.1.6.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

19/11 2016

3.1.3

3.1.3.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

11/10 2016

3.1

3.1.0.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

11/10 2016

3.1.1

3.1.1.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

11/10 2016

3.1.2

3.1.2.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

07/10 2016

3.0.1

3.0.1.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

18/07 2016

3.0

3.0.0.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

06/07 2016

3.0-beta2

3.0.0.0-beta2 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

04/07 2016

3.0-beta

3.0.0.0-beta https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

21/03 2016

2.1.8

2.1.8.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

21/03 2016

2.1.9

2.1.9.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

12/02 2016

2.1.7

2.1.7.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

01/02 2016

2.1.6

2.1.6.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

07/01 2016

2.1.5

2.1.5.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

07/09 2015

2.1.2

2.1.2.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

07/09 2015

2.1.4

2.1.4.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

11/08 2015

2.1.1

2.1.1.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

11/08 2015

2.1

2.1.0.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

27/07 2015

2.0.9

2.0.9.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

17/02 2015

2.0.5

2.0.5.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

17/02 2015

2.0.6

2.0.6.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

17/02 2015

2.0.7

2.0.7.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

17/02 2015

2.0.8

2.0.8.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

03/01 2015

2.0.4

2.0.4.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

26/12 2014

2.0.3

2.0.3.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

22/12 2014

2.0.2

2.0.2.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

17/12 2014

2.0.1

2.0.1.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

16/12 2014

2.0.0

2.0.0.0 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

24/11 2014

2.0.0-beta2

2.0.0.0-beta2 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

05/10 2014

2.0.0-beta1

2.0.0.0-beta1 https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

GPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

05/10 2014

2.0.0-alpha

2.0.0.0-alpha https://github.com/ventoviro/windwalker-datamapper

Windwalker DataMapper package

  Sources   Download

GPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

10/06 2014

1.0.2

1.0.2.0 https://github.com/windwalker-framework/datamapper

Windwalker DataMapper package

  Sources   Download

GPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

10/06 2014

1.0.3

1.0.3.0 https://github.com/windwalker-framework/datamapper

Windwalker DataMapper package

  Sources   Download

GPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

03/05 2014

1.0.1

1.0.1.0 https://github.com/windwalker-framework/datamapper

Windwalker DataMapper package

  Sources   Download

GPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker

12/04 2014

1.0.0

1.0.0.0 https://github.com/windwalker-framework/datamapper

Windwalker DataMapper package

  Sources   Download

GPL-2.0+

The Requires

 

The Development Requires

framework datamapper windwalker