2017 © Pedro Peláez
 

library redbean-traversing

Add traversing through associations with scopes for redbeans

image

xire28/redbean-traversing

Add traversing through associations with scopes for redbeans

  • Thursday, February 18, 2016
  • by xire28
  • Repository
  • 1 Watchers
  • 1 Stars
  • 2 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 11 Versions
  • 0 % Grown

The README.md

Redbean traversing

Add traversing through associations with scopes for redbeans, (*1)

Requirements

  • PHP >= 5.4.0

Install

Using Composer

composer require xire28/redbean-traversing

Note

This package is composed of two parts: - MulticallProxy to use a collection like an object and apply the operation on all the items simultaneously - SQLConditionBuilder to progressively construct the SQL query filter, (*2)

Examples

Seeds

  • United States of America
    • Alabama
      • Lionel Richie (1949-06-20)
      • Felicia Day (1979-06-28)
    • Alaska
      • Anna Graceman (1999-08-01)
    • Arizona
      • Emma Stone (1988-11-06)
    • Arkansas
  • China
    • Gensu
      • Wong Lee (1978-02-01)

Retrieve all persons in the usa

multi()->ownState->ownPerson;
  }
}

$usa = R::load('country', 1);

echo '
    '; foreach($usa->people() as $person){ echo "
  • {$person->fullName}
  • "; } echo '
'; ?>

SQL statements generated

SELECT `country`.* FROM `country` WHERE (`id` IN ( 1 ))
SELECT `state`.* FROM `state` WHERE country_id = '1'
SELECT `person`.* FROM `person` WHERE state_id = '1'
SELECT `person`.* FROM `person` WHERE state_id = '2'
SELECT `person`.* FROM `person` WHERE state_id = '3'
SELECT `person`.* FROM `person` WHERE state_id = '4'

Output

- Lionel Richie
- Felicia Day
- Anna Graceman
- Emma Stone

Retrieve all adult persons in the usa

multi()->ownState->isAdult()->ownPerson;
    }
}

class Model_State extends BaseModel
{
    use PersonScope;
}

trait PersonScope {
    public function isAdult(){
        return $this->personOlderThan(17);
    }
    public function personOlderThan($age){
        return $this->where('TIMESTAMPDIFF(YEAR, born_at, CURDATE()) > ?', $age);
    }
}

$usa = R::load('country', 1);
echo '
    '; foreach($usa->adultPersons() as $person){ echo "
  • {$person->fullName}
  • "; } echo '
'; ?>

SQL statements generated

SELECT `country`.* FROM `country` WHERE (`id` IN ( 1 ))
SELECT `state`.* FROM `state` WHERE country_id = '1'
SELECT `person`.* FROM `person` WHERE state_id = '1' AND TIMESTAMPDIFF(YEAR, born_at, CURDATE()) > 17
SELECT `person`.* FROM `person` WHERE state_id = '2' AND TIMESTAMPDIFF(YEAR, born_at, CURDATE()) > 17
SELECT `person`.* FROM `person` WHERE state_id = '3' AND TIMESTAMPDIFF(YEAR, born_at, CURDATE()) > 17
SELECT `person`.* FROM `person` WHERE state_id = '4' AND TIMESTAMPDIFF(YEAR, born_at, CURDATE()) > 17

Output

- Lionel Richie
- Felicia Day
- Emma Stone

Build complex queries

';
foreach($usa->multi()->group(function($q){ return $q->where('name LIKE "Ar%"')->_or()->where('name = ?', 'Alabama'); })->ownState->ownPerson as $person){
    echo "
  • {$person->fullName}
  • "; } echo ''; ?>

    SQL statements generated

    SELECT `country`.* FROM `country` WHERE (`id` IN ( 1 ))
    SELECT `state`.* FROM `state` WHERE country_id = '1' AND ( name LIKE "Ar%" OR name = 'Alabama' )
    SELECT `person`.* FROM `person` WHERE state_id = '1'
    SELECT `person`.* FROM `person` WHERE state_id = '3'
    

    Output

    - Lionel Richie
    - Felicia Day
    - Emma Stone
    

    The Versions

    18/02 2016

    dev-master

    9999999-dev https://github.com/xire28/redbean-traversing

    Add traversing through associations with scopes for redbeans

      Sources   Download

    MIT

    The Requires

    • php >=5.4.0

     

    trait scope redbean traversing associations through

    18/02 2016

    v1.2.3

    1.2.3.0 https://github.com/xire28/redbean-traversing

    Add traversing through associations with scopes for redbeans

      Sources   Download

    MIT

    The Requires

    • php >=5.4.0

     

    trait scope redbean traversing associations through

    18/02 2016

    v1.3.0

    1.3.0.0 https://github.com/xire28/redbean-traversing

    Add traversing through associations with scopes for redbeans

      Sources   Download

    MIT

    The Requires

    • php >=5.4.0

     

    trait scope redbean traversing associations through

    18/02 2016

    v1.2.2

    1.2.2.0 https://github.com/xire28/redbean-traversing

    Add traversing through associations with scopes for redbeans

      Sources   Download

    MIT

    The Requires

    • php >=5.4.0

     

    trait scope redbean traversing associations through

    16/02 2016

    v1.2.1

    1.2.1.0 https://github.com/xire28/redbean-traversing

    Add traversing through associations with scopes for redbeans

      Sources   Download

    MIT

    The Requires

    • php >=5.4.0

     

    trait scope redbean traversing associations through

    16/02 2016

    v1.2.0

    1.2.0.0 https://github.com/xire28/redbean-traversing

    Add traversing through associations with scopes for redbeans

      Sources   Download

    MIT

    The Requires

    • php >=5.4.0

     

    trait scope redbean traversing associations through

    14/02 2016

    v1.1.2

    1.1.2.0 https://github.com/xire28/redbean-traversing

    Add traversing through associations for redbeans

      Sources   Download

    MIT

    The Requires

    • php >=5.4.0

     

    trait scope redbean traversing has one through has many through

    14/02 2016

    v1.1.1

    1.1.1.0 https://github.com/xire28/redbean-traversing

    Add traversing through associations for redbeans

      Sources   Download

    MIT

    The Requires

    • php >=5.4.0

     

    trait scope redbean traversing has one through has many through

    14/02 2016

    v1.1.0

    1.1.0.0 https://github.com/xire28/redbean-traversing

    Add traversing through associations for redbeans

      Sources   Download

    MIT

    The Requires

    • php >=5.3.0

     

    trait scope redbean traversing has one through has many through

    13/02 2016

    v1.0.1

    1.0.1.0 https://github.com/xire28/redbean-traversing

    Add traversing through associations for redbeans using decorators

      Sources   Download

    MIT

    The Requires

    • php >=5.3.0

     

    scope redbean decorators traversing has one through has many through

    13/02 2016

    v1.0.0

    1.0.0.0 https://github.com/xire28/redbean-traversing

    Add traversing through associations for redbeans using decorators

      Sources   Download

    MIT

    The Requires

    • php >=5.3.0

     

    scope redbean decorators traversing has one through has many through