Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/quentingab/wodel

WordPress Post Model. Eloquent like syntax with wp database function.
https://github.com/quentingab/wodel

eloquent model models wordpress wordpress-development

Last synced: about 2 months ago
JSON representation

WordPress Post Model. Eloquent like syntax with wp database function.

Awesome Lists containing this project

README

        

# wodel
Easy way to interact with WordPress database, query, insert and update posts.
And it also works with ACF.

[![Latest Version on Packagist][ico-version]](https://packagist.org/packages/quentingab/wodel)
[![Software License][ico-license]](LICENSE.md)
[![Total Downloads][ico-downloads]](https://packagist.org/packages/quentingab/wodel)

## Install

Via Composer

``` bash
$ composer require quentingab/wodel
```

## Usage with WordPress posts

### Get all posts/page and custom post type
``` php
$posts = QuentinGgab\Models\Wodel::all();
foreach($posts as $post){
echo $post->post_title;
}
```
### Get current post with acf
``` php
$post = QuentinGab\Models\Wodel::current();
```

### Update a post
``` php
$post = QuentinGab\Models\Wodel::current();
$post->post_title = "Hello World";
$post->save();
```

### Insert a post
``` php
$post = new QuentinGab\Models\Wodel(
[
'post_title'=>'Hello World'
]
);
$post->save();
```

## Extend the Wodel
``` php
class Page extends QuentinGab\Wodel\Models\Wodel
{
protected $post_type = 'page';

//only necessary if you want to insert a new post programmatically
//otherwise the acf fields will not be populated
//If you only get Model or update existing Model you can omit $acf_keys
protected $acf_keys = [
'the_field_name' => 'the_field_key',
'color' => 'field_5f7848684c404',
];
}

$page = Page::find(1);
echo $page->color;
```

## Usage with custom table
if you have data stored in a custom table you can use QuentinGab\Models\Model to interact with the database.
Under the hood it only use default WordPress object $wpdb.

### Example of a custom table
``` php
global $wpdb;

$table_name = 'events';
$charset_collate = $wpdb->get_charset_collate();

$sql = "CREATE TABLE $table_name (
id bigint(20) NOT NULL AUTO_INCREMENT,
title varchar(255),
active boolean DEFAULT 0 NOT NULL,
created_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";

dbDelta($sql);
```
### Create a Model class
``` php
class Event extends QuentinGab\Wodel\Models\Model
{
protected $table = 'events';

protected $primary_key = "id";

protected $fillable = [
'title'
];

protected $casts = [
'active' => 'bool',
];
}
```
### Get Model
``` php
$all = Event::all();
$only_active = Event::where(['active'=>true]);
$with_primary_key_1 = Event::find(1);
```
### Save Model
``` php
$new_event = new Event(['title'=>'my new event','active'=>false]);
$new_event->save();
```

## Change log

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

## Testing

``` bash
$ composer test
```

## Contributing

Please see [CONTRIBUTING](CONTRIBUTING.md) and [CODE_OF_CONDUCT](CODE_OF_CONDUCT.md) for details.

## Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

## Credits

- [quentin gabriele](https://github.com/QuentinGab)

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

[ico-version]: https://img.shields.io/packagist/v/quentingab/wodel.svg?style=flat-square
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
[ico-travis]: https://img.shields.io/travis/quentingab/wodel/master.svg?style=flat-square
[ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/quentingab/wodel.svg?style=flat-square
[ico-code-quality]: https://img.shields.io/scrutinizer/g/quentingab/wodel.svg?style=flat-square
[ico-downloads]: https://img.shields.io/packagist/dt/quentingab/wodel.svg?style=flat-square

[link-packagist]: https://packagist.org/packages/quentingab/wodel
[link-travis]: https://travis-ci.org/quentingab/wodel
[link-scrutinizer]: https://scrutinizer-ci.com/g/quentingab/wodel/code-structure
[link-code-quality]: https://scrutinizer-ci.com/g/quentingab/wodel
[link-downloads]: https://packagist.org/packages/quentingab/wodel
[link-author]: https://github.com/quentingab
[link-contributors]: ../../contributors