Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5.x #26

Merged
merged 90 commits into from
Sep 16, 2024
Merged

5.x #26

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
90 commits
Select commit Hold shift + click to select a range
231f72b
IdColumn and DbTable attributes can only apply to a class
thewunder Mar 1, 2024
d0d411d
Get machinery working to read relationship attributes and load the re…
thewunder Mar 3, 2024
3ba0dc1
rename RelationshipReader to RelationshipManager
thewunder Mar 3, 2024
e70c452
Add property name to RelationshipType
thewunder Mar 3, 2024
a8b5f57
Switch one-to-one save and load to new RelationshipHandler
thewunder Mar 3, 2024
ffbf21a
switch from public properties to private with accessors on Relationsh…
thewunder Mar 4, 2024
a614dc9
add OneToMany relationship + handler
thewunder Mar 4, 2024
27dc2fb
add ManyToMany relationship + handler
thewunder Mar 4, 2024
743c999
Make RelationshipLoader just a pass-through to the new RelationshipMa…
thewunder Mar 4, 2024
b78c803
Make RelationshipSaver just a pass-through to the new RelationshipMan…
thewunder Mar 5, 2024
aa68925
Add phpdoc to relationship attributes
thewunder Mar 5, 2024
7a4a11a
Add addHandler method to allow end user to add new relationship handl…
thewunder Mar 5, 2024
34cceba
Mark relationship loader and saver and associated orm methods as depr…
thewunder Mar 6, 2024
27ce79f
Add methods on RelationshipManager to save all relationships and load…
thewunder Mar 6, 2024
79e1b95
Update relationship load documentation in README
thewunder Mar 6, 2024
24fb1d6
Delete broken code quality and travis badges
thewunder Mar 6, 2024
d741019
Bump version in readme
thewunder Mar 6, 2024
b37e4b2
Add Polymorphic relationship and handler
thewunder Mar 6, 2024
1d13a6c
Run rector
thewunder Mar 7, 2024
15ee74c
bump number of relationships (added a polymorphic relationship)
thewunder Mar 7, 2024
0ed17d1
Handle objects of mixed class in orm load method, document method
thewunder Mar 7, 2024
8a4423f
Move deprecated orm methods to bottom of class, move load up next to …
thewunder Mar 7, 2024
6a53888
make ObjectMapper final
thewunder Mar 7, 2024
9a3d688
Make all exceptions extend from base exception
thewunder Mar 7, 2024
20ad5f1
Update changelog for 4.2
thewunder Mar 7, 2024
4a5638b
unfinal ObjectMapper so it can be mocked
thewunder Mar 21, 2024
412ab49
Replace travis with github action
thewunder Mar 21, 2024
f867fb7
add mysql and postgres host env vars
thewunder Mar 21, 2024
e644e46
fix mysql pass env var and add pgsl port
thewunder Mar 21, 2024
4a34baf
add default NULL to polymorphicClass column to fix tests with default…
thewunder Mar 21, 2024
8bbb1e6
change postgresql port to default
thewunder Mar 21, 2024
0ce32ae
try setting pgsql pass
thewunder Mar 21, 2024
3bb930d
set up test postgres user with password
thewunder Mar 21, 2024
113ac13
grant create to postgres user
thewunder Mar 21, 2024
ceaeb32
Fix phpunit warning on BaseIntegrationTest
thewunder Mar 21, 2024
28eca10
bump to v4 of checkout and cache to fix warning about node 16
thewunder Mar 22, 2024
885aa53
delete coverage badge
thewunder Mar 22, 2024
31db901
remover coveralls, add psr/container, tweak versions
thewunder Mar 22, 2024
e952c15
add use in rector.php for phpunit ruleset
thewunder Mar 22, 2024
6ea29b5
Fix backwards compatibility with a custom foreign object getter on on…
thewunder Mar 22, 2024
1e600df
Setter should be based on property name not id property in one-to-one…
thewunder Mar 22, 2024
de70cdc
Allow more than one relationship to be loaded via a variadic property…
thewunder Mar 25, 2024
0ae65b9
Also allow more than one relationship to be saved via a variadic prop…
thewunder Mar 25, 2024
757dc74
one-to-one relationship setter should be based on the property not class
thewunder Mar 26, 2024
28773b1
Define relationship class in load/save methods to clean up a few type…
thewunder Mar 26, 2024
8e0362f
Add interface so that RelationshipHandlers can join to foreign table(s)
thewunder Mar 27, 2024
5354f28
rename className to relationship->foreignClass for clarity, add class…
thewunder Mar 27, 2024
941cc2e
implement join method stubs on handlers
thewunder Mar 27, 2024
d7a204f
implement getClass() on all relationships
thewunder Mar 27, 2024
46a5de8
Add helper method to join method, add phpdoc
thewunder Mar 27, 2024
116be69
Implement one-to-one join method
thewunder Mar 27, 2024
a9229cc
Implement one-to-many join method
thewunder Mar 27, 2024
d2e1726
Implement many-to-many join method
thewunder Mar 27, 2024
a8c4add
fix saving of polymorphic relationship on objects that have no id
thewunder Mar 29, 2024
2f9dae4
implement join method for polymorphic relationships, alter join contr…
thewunder Apr 3, 2024
693673b
mention join helper in changelog
thewunder Apr 4, 2024
5be73e5
don't use deprecated array parameter types
thewunder Apr 9, 2024
6c685da
Add backward compatibility note for legacy save and load, name releas…
thewunder Apr 9, 2024
df2f626
Switch to thewunder/corma-dbal, a fork of Doctrine DBAL 4.0
thewunder Jul 7, 2024
73cdad3
Make necessary changes to adapt to corma-dbal
thewunder Jul 7, 2024
5aa3675
Drop tests for OR support in query helper that I never got working
thewunder Jul 7, 2024
0a69562
Fix getting of primary keys
thewunder Jul 7, 2024
cc6fb56
Throw an exception that can actually be thrown
thewunder Jul 7, 2024
19afa0c
Fix parsing of database driver name
thewunder Jul 7, 2024
bd2ab65
Fix errors in integration tests
thewunder Jul 8, 2024
156a775
Throw concrete exception instead of interface in testIsDuplicateExcep…
thewunder Jul 8, 2024
6fe4f12
Require corma-dbal 1.x
thewunder Jul 8, 2024
d6d3192
Merge pull request #23 from thewunder/corma-dbal
thewunder Jul 8, 2024
59c2aa0
Add DBAL changes to CHANGELOG
thewunder Jul 8, 2024
dbf364c
Update version in README
thewunder Jul 8, 2024
44e848f
Mention polymorphic relationships
thewunder Jul 8, 2024
5c5566c
Fix postgres error: database "cormatest" does not exist
thewunder Jul 8, 2024
8bb86f4
Fix postgres error: database "cormatest" does not exist (try 2)
thewunder Jul 8, 2024
0f16d56
Fix bug in loadMany where the setter is incorrectly inferred
thewunder Aug 1, 2024
e745135
Update save and saveAll methods to take list of relationships to save
thewunder Sep 7, 2024
6a2f1bd
Support * as a shortcut to load or save all relationships, if multipl…
thewunder Sep 7, 2024
565b6eb
Move all unit tests into their own namespace
thewunder Sep 7, 2024
d139c5c
Better document relationship joining
thewunder Sep 9, 2024
3d3c90d
Split database connection and setup into separate DatabaseTestPlatfor…
thewunder Sep 9, 2024
152c612
Split integration tests into multiple classes
thewunder Sep 9, 2024
f3cf832
Give example .env files
thewunder Sep 9, 2024
5a3cab0
Split unit tests and integration tests into different suites, add com…
thewunder Sep 9, 2024
1abc8c9
Fix testUpsertWithoutPrimaryKey to take into account differing behavi…
thewunder Sep 9, 2024
d1db96d
Always load the .env file if it exists
thewunder Sep 9, 2024
f401647
Fix bad greater-than assertions on relationship save test
thewunder Sep 9, 2024
10547de
clean up unused import / param
thewunder Sep 11, 2024
77f9d9d
Merge pull request #24 from thewunder/integration-test-refactor
thewunder Sep 11, 2024
986182e
Fix backward compatibility for repository save / saveAll
thewunder Sep 12, 2024
1e79a32
Override default pluralization rule to reflect that the children is a…
thewunder Sep 16, 2024
87ca54f
Merge pull request #25 from thewunder/5.x-fixes
thewunder Sep 16, 2024
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
5 changes: 5 additions & 0 deletions .env.mysql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DB_PLATFORM=Mysql

MYSQL_HOST=localhost
MYSQL_USER=mysqluser
MYSQL_PASS=mypass
6 changes: 6 additions & 0 deletions .env.postgres
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
DB_PLATFORM=Postgres

PGSQL_HOST=localhost
PGSQL_PORT=5432
PGSQL_USER=pguser
PGSQL_PASS=pgpass
70 changes: 70 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: PHP Build

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

permissions:
contents: read

jobs:
test:

runs-on: ubuntu-latest
strategy:
matrix:
php: [ 8.1, 8.2, 8.3 ]

env:
MYSQL_HOST: localhost
MYSQL_USER: root
MYSQL_PASS: root
PGSQL_HOST: localhost
PGSQL_USER: cormatest
PGSQL_PASS: cormatest
PGSQL_PORT: 5432

steps:
- uses: actions/checkout@v4

- name: Start MySQL
run: |
sudo systemctl start mysql

- name: Start PostgreSQL
run: |
sudo systemctl start postgresql
pg_isready

- name: Create test database
run: sudo -u postgres createdb $PGSQL_USER

- name: Create test postgres user
run: |
sudo -u postgres psql --command="CREATE USER $PGSQL_USER PASSWORD '$PGSQL_PASS'" --command="GRANT CREATE ON DATABASE $PGSQL_USER TO $PGSQL_USER"

- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v4
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-

- name: Install dependencies
run: composer install --prefer-dist --no-progress

- name: Run unit tests
run: composer run-script test:unit

- name: Run mysql tests
run: composer run-script test:mysql

- name: Run postgres tests
run: composer run-script test:postgres
16 changes: 0 additions & 16 deletions .travis.yml

This file was deleted.

24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
Version 5.0
=============

New Features
-----------
- Add new way of loading relationships where relationships are defined via property attributes.
- Add support for loading and saving polymorphic relationships where a class and id column can load an object of any class in a namespace.
- Allow all specified relationships on a class to be loaded and saved with a single method call.
- Add helper to repositories to join other tables that have a relationship defined on the current class.
- Provides a framework where custom relationship types can be added or behavior for built-in types modified.

Updates
-------
- Existing loadOne, loadMany, loadManyToMany as well as the RelationshipSaver and RelationshipLoader classes have been deprecated.
- All exceptions throws extend from a common CormaException class
- Updated to a fork of Doctrine DBAL 4.0 corma-dbal with the ability to introspect into QueryBuilders

Breaking Changes
----------------

* Legacy relationship load / save methods require that the property names and getter / setter names are consistent with the property name. They are not 100% backward compatible, and may require some renaming.
* Many DBAL methods removed or changed, see [Doctrine Upgrade Guide](https://github.com/doctrine/dbal/blob/4.0.x/UPGRADE.md)
* Namespace of the DBAL classes changes from Doctrine\DBAL to Corma\DBAL this should be fixable with a simple search + replace.

Version 4.1.0
=============

Expand Down
35 changes: 23 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@ Corma

[![Latest Version on Packagist][ico-version]][link-packagist]
[![Software License][ico-license]](LICENSE.txt)
[![Build Status](https://api.travis-ci.org/thewunder/corma.svg?branch=master)](https://travis-ci.org/thewunder/corma)
[![Coverage Status](https://coveralls.io/repos/github/thewunder/corma/badge.svg?branch=master)](https://coveralls.io/github/thewunder/corma?branch=master)
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/3ab739ee-d54a-457d-9eec-43261102dfe4/mini.png)](https://insight.sensiolabs.com/projects/3ab739ee-d54a-457d-9eec-43261102dfe4)

Corma is a high-performance, convention-based ORM based on Doctrine DBAL.

Corma is great because:

* No complex and difficult to verify annotations or configuration files
* Promotes consistent code organization
* Loads and saves one-to-one, one-to-many, and many-to-many relationships with a method call
* Loads and saves one-to-one, one-to-many, many-to-many, and polymorphic relationships
* Can save multiple objects in a single query (using an upsert)
* Makes it easy to cache and avoid database queries
* Supports soft deletes
Expand All @@ -32,26 +29,41 @@ Install via Composer

Via the command line:

composer.phar require thewunder/corma ~4.0
composer.phar require thewunder/corma ^5.0

Or add the following to the require section your composer.json:

"thewunder/corma": "~4.0"
"thewunder/corma": "^5.0"

For PHP versions < 8.0 use Corma version ~3.0
For PHP versions < 8.1 use Corma version ~3.0

Basic Usage
-----------
Create a DataObject

```php
namespace YourNamespace\Dataobjects;

use Corma\Relationship\ManyToMany;
use Corma\Relationship\OneToMany;
use Corma\Relationship\OneToOne;

class YourDataObject {
protected $id;

//If the property name == column name on the table your_data_objects it will be saved
protected $myColumn;

protected ?int $otherObjectId = null;

#[OneToOne]
protected ?OtherObject $otherObject = null;

#[OneToMany(AnotherObject::class)]
protected ?array $anotherObjects = null;

#[ManyToMany(DifferentObject::class, 'your_data_object_different_link_table')]
protected ?array $differentObjects = null;
//Getters and setters..
}
```
Expand Down Expand Up @@ -79,7 +91,7 @@ $orm->save($object);
//Call more setters on $object...
$objects = [$object];
$newObject = $orm->create(YourDataObject::class);
//call setters on $newObject..
//call setters on $newObject...
$objects[] = $newObject;

$orm->saveAll($objects);
Expand All @@ -91,9 +103,9 @@ $existingObject = $orm->find(YourDataObject::class, 5);
$existingObjects = $orm->findBy(YourDataObject::class, ['myColumn >='=>42, 'otherColumn'=>1], ['sortColumn'=>'ASC']);

//load relationships
$orm->loadOne($existingObjects, OtherObject::class, 'otherObjectId');
$orm->loadMany($existingObjects, AnotherObject::class, 'yourObjectId');
$orm->loadManyToMany($existingObjects, DifferentObject::class, 'link_table');
$orm->load($existingObjects, 'otherObject');
$orm->load($existingObjects, 'anotherObjects');
$orm->load($existingObjects, 'differentObjects');

//delete those
$orm->deleteAll($existingObjects);
Expand All @@ -112,5 +124,4 @@ Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

[ico-version]: https://img.shields.io/packagist/v/thewunder/corma.svg?style=flat-square
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square

[link-packagist]: https://packagist.org/packages/thewunder/corma
13 changes: 8 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,23 @@
"php": ">=8.1",
"ext-pdo" : "*",
"ext-json": "*",
"doctrine/dbal": "^3.0",
"doctrine/inflector": "^1.3||^2.0",
"psr/event-dispatcher": "^1.0",
"psr/simple-cache": "^1.0||^2.0||^3.0"
"psr/simple-cache": "^2.0||^3.0",
"psr/container": "^2.0",
"thewunder/corma-dbal": "^1.0"
},
"require-dev": {
"vlucas/phpdotenv": "~2.0",
"php-coveralls/php-coveralls": "~2.0",
"phpunit/phpunit": "^10.0",
"symfony/event-dispatcher": "^6.0",
"symfony/event-dispatcher": "^6.0 || ^7.0",
"rector/rector": "^1.0"
},
"scripts": {
"test": "vendor/bin/phpunit"
"test": "vendor/bin/phpunit",
"test:unit": "vendor/bin/phpunit --testsuite Unit",
"test:mysql": ["@putenv DB_PLATFORM=Mysql", "vendor/bin/phpunit --testsuite Integration"],
"test:postgres": ["@putenv DB_PLATFORM=Postgres", "vendor/bin/phpunit --testsuite Integration"]
},
"suggest": {
"robmorgan/phinx": "*"
Expand Down
7 changes: 5 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="UnitTests">
<directory>test/</directory>
<testsuite name="Unit">
<directory>test/Unit</directory>
</testsuite>
<testsuite name="Integration">
<directory>test/Integration</directory>
</testsuite>
</testsuites>
<source>
Expand Down
3 changes: 2 additions & 1 deletion rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\PHPUnit\Set\PHPUnitSetList;
use Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector;

return RectorConfig::configure()
Expand All @@ -11,7 +12,7 @@
__DIR__ . '/test',
])->withPhpSets()
->withSets([
Rector\PHPUnit\Set\PHPUnitSetList::PHPUNIT_100
PHPUnitSetList::PHPUNIT_100
])
->withRules([
AddVoidReturnTypeWhereNoReturnRector::class,
Expand Down
2 changes: 1 addition & 1 deletion src/DataObject/Factory/ObjectFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace Corma\DataObject\Factory;

use Doctrine\DBAL\Result;
use Corma\DBAL\Result;

/**
* Manages the construction of objects
Expand Down
4 changes: 2 additions & 2 deletions src/DataObject/Factory/PsrContainerObjectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@


use Corma\DataObject\Hydrator\ObjectHydratorInterface;
use Doctrine\DBAL\Result;
use Doctrine\DBAL\FetchMode;
use Corma\DBAL\Result;
use Corma\DBAL\FetchMode;
use Psr\Container\ContainerInterface;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/DataObject/Identifier/AutoIncrementIdentifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct(Inflector $inflector, private readonly QueryHelperIn
public function setNewId(object $object): object
{
$table = $this->convention->getTable($object);
return $this->setId($object, $this->queryHelper->getLastInsertId($table, $this->getIdColumn($object)));
return $this->setId($object, $this->queryHelper->getLastInsertId());
}

public function isNew($object): bool
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
namespace Corma\DataObject\Identifier;

use Corma\DataObject\TableConvention\TableConventionInterface;
use Corma\QueryHelper\QueryHelperInterface;
use Corma\Util\Inflector;

Expand All @@ -10,15 +9,14 @@
*/
class CustomizableAutoIncrementIdentifier extends CustomizableIdentifier
{
public function __construct(Inflector $inflector, private readonly QueryHelperInterface $queryHelper, private readonly TableConventionInterface $convention)
public function __construct(Inflector $inflector, private readonly QueryHelperInterface $queryHelper)
{
parent::__construct($inflector);
}

public function setNewId(object $object): object
{
$table = $this->convention->getTable($object);
return $this->setId($object, $this->queryHelper->getLastInsertId($table, $this->getIdColumn($object)));
return $this->setId($object, $this->queryHelper->getLastInsertId());
}

public function isNew(object $object): bool
Expand Down
2 changes: 1 addition & 1 deletion src/DataObject/Identifier/IdColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Corma\DataObject\Identifier;

#[\Attribute]
#[\Attribute(\Attribute::TARGET_CLASS)]
class IdColumn
{
public function __construct(private readonly string $column)
Expand Down
2 changes: 1 addition & 1 deletion src/DataObject/ObjectManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Corma\DataObject\Hydrator\ObjectHydratorInterface;
use Corma\DataObject\Identifier\ObjectIdentifierInterface;
use Corma\DataObject\TableConvention\TableConventionInterface;
use Doctrine\DBAL\Result;
use Corma\DBAL\Result;

/**
* Manages creation, hydration, and table name and id inspection for a particular class
Expand Down
2 changes: 1 addition & 1 deletion src/DataObject/TableConvention/DbTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Corma\DataObject\TableConvention;


#[\Attribute]
#[\Attribute(\Attribute::TARGET_CLASS)]
class DbTable
{
public function __construct(private readonly string $table)
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/BadMethodCallException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/**
* Thrown when a method that should not be called is called
*/
class BadMethodCallException extends \BadMethodCallException
final class BadMethodCallException extends CormaException
{

}
2 changes: 1 addition & 1 deletion src/Exception/ClassNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/**
* Thrown when a class cannot be found by corma
*/
class ClassNotFoundException extends \LogicException
final class ClassNotFoundException extends CormaException
{

}
7 changes: 7 additions & 0 deletions src/Exception/CormaException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Corma\Exception;

abstract class CormaException extends \Exception
{
}
2 changes: 1 addition & 1 deletion src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/**
* Thrown if one or more function arguments are invalid
*/
class InvalidArgumentException extends \InvalidArgumentException
final class InvalidArgumentException extends CormaException
{

}
Loading