diff --git a/tests/cases/integration/Entity/entity.compositePK.phpt b/tests/cases/integration/Entity/entity.compositePK.phpt index dd3f41cb..2bcaddfe 100644 --- a/tests/cases/integration/Entity/entity.compositePK.phpt +++ b/tests/cases/integration/Entity/entity.compositePK.phpt @@ -15,6 +15,7 @@ use NextrasTests\Orm\DataTestCase; use NextrasTests\Orm\Helper; use NextrasTests\Orm\User; use NextrasTests\Orm\UserStat; +use NextrasTests\Orm\UserStatX; use Tester\Assert; @@ -54,6 +55,41 @@ class EntityCompositePKTest extends DataTestCase $this->orm->persistAndFlush($userStat); } + public function testCompositePKDateTime2() + { + if ($this->section === Helper::SECTION_MSSQL) { + // An explicit value for the identity column in table 'users' can only be specified when a column list is used and IDENTITY_INSERT is ON. + // http://stackoverflow.com/questions/2148091/syntax-for-inserting-into-a-table-with-no-values + Environment::skip('Inserting dummy rows when no arguments are passed is not supported.'); + } + + $user = new User(); + $this->orm->persistAndFlush($user); + + $stat = new UserStatX(); + $stat->user = $user; + $stat->date = '2019-01-01'; + $stat->value = 100; + $this->orm->persistAndFlush($stat); + + $this->orm->clear(); + + $res = $this->orm->userStatsX->getBy(['date' => '2019-01-01']); + Assert::same(100, $res->value); + + $res->value = 200; + $this->orm->persistAndFlush($res); + Assert::same(200, $res->value); + + $this->orm->clear(); + + $res = $this->orm->userStatsX->getBy(['date' => '2019-01-01']); + Assert::same(200, $res->value); + + Environment::$checkAssertions = false; + } + + public function testGetBy(): void { diff --git a/tests/db/mysql-init.sql b/tests/db/mysql-init.sql index 2ae9e55c..3b944863 100644 --- a/tests/db/mysql-init.sql +++ b/tests/db/mysql-init.sql @@ -120,6 +120,45 @@ CREATE TABLE photos CONSTRAINT photos_album_id FOREIGN KEY (album_id) REFERENCES photo_albums (id) ON DELETE CASCADE ON UPDATE CASCADE ) AUTO_INCREMENT = 1; +CREATE TABLE user_stats_x ( + user_id int NOT NULL, + date DATETIME NOT NULL, + value int NOT NULL, + PRIMARY KEY(user_id, date), + CONSTRAINT user_stats_x_user_id FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE ON UPDATE CASCADE +); + + +CREATE TABLE users_x_users ( + my_friends_id int NOT NULL, + friends_with_me_id int NOT NULL, + PRIMARY KEY (my_friends_id, friends_with_me_id), + CONSTRAINT my_friends_key FOREIGN KEY (my_friends_id) REFERENCES users (id) ON DELETE CASCADE ON UPDATE CASCADE, + CONSTRAINT friends_with_me_key FOREIGN KEY (friends_with_me_id) REFERENCES users (id) ON DELETE CASCADE ON UPDATE CASCADE +); + + +CREATE TRIGGER `book_collections_bu_trigger` BEFORE UPDATE ON `book_collections` +FOR EACH ROW SET NEW.updated_at = NOW(); + +CREATE TRIGGER `book_collections_bi_trigger` BEFORE INSERT ON `book_collections` +FOR EACH ROW SET NEW.updated_at = NOW(); + + +CREATE TABLE photo_albums ( + id int NOT NULL AUTO_INCREMENT, + title varchar(255) NOT NULL, + preview_id int NULL, + PRIMARY KEY(id) +) AUTO_INCREMENT=1; + +CREATE TABLE photos ( + id int NOT NULL AUTO_INCREMENT, + title varchar(255) NOT NULL, + album_id int NOT NULL, + PRIMARY KEY(id), + CONSTRAINT photos_album_id FOREIGN KEY (album_id) REFERENCES photo_albums (id) ON DELETE CASCADE ON UPDATE CASCADE +) AUTO_INCREMENT=1; ALTER TABLE photo_albums ADD CONSTRAINT photo_albums_preview_id FOREIGN KEY (preview_id) REFERENCES photos (id) ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/tests/db/pgsql-init.sql b/tests/db/pgsql-init.sql index 16e253ae..86d11696 100644 --- a/tests/db/pgsql-init.sql +++ b/tests/db/pgsql-init.sql @@ -120,6 +120,52 @@ CREATE TABLE "photos" CONSTRAINT "photos_album_id" FOREIGN KEY ("album_id") REFERENCES "photo_albums" ("id") ON DELETE CASCADE ON UPDATE CASCADE ); +CREATE TABLE "user_stats_x" ( + "user_id" int NOT NULL, + "date" TIMESTAMPTZ NOT NULL, + "value" int NOT NULL, + PRIMARY KEY("user_id", "date"), + CONSTRAINT "user_stats_x_user_id" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE CASCADE +); + + +CREATE TABLE "users_x_users" ( + "my_friends_id" int NOT NULL, + "friends_with_me_id" int NOT NULL, + PRIMARY KEY ("my_friends_id", "friends_with_me_id"), + CONSTRAINT "my_friends_key" FOREIGN KEY ("my_friends_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE CASCADE, + CONSTRAINT "friends_with_me_key" FOREIGN KEY ("friends_with_me_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE CASCADE +); + + +CREATE FUNCTION "book_collections_before"() RETURNS TRIGGER AS $BODY$BEGIN +NEW."updated_at" = NOW(); +return NEW; +END; +$BODY$ +LANGUAGE 'plpgsql' VOLATILE; + +CREATE TRIGGER "book_collections_before_insert_trigger" BEFORE INSERT ON "book_collections" +FOR EACH ROW EXECUTE PROCEDURE "book_collections_before"(); + +CREATE TRIGGER "book_collections_before_update_trigger" BEFORE UPDATE ON "book_collections" +FOR EACH ROW EXECUTE PROCEDURE "book_collections_before"(); + + +CREATE TABLE "photo_albums" ( + "id" serial4 NOT NULL, + "title" varchar(255) NOT NULL, + "preview_id" int NULL, + PRIMARY KEY ("id") +); + +CREATE TABLE "photos" ( + "id" serial4 NOT NULL, + "title" varchar(255) NOT NULL, + "album_id" int NOT NULL, + PRIMARY KEY ("id"), + CONSTRAINT "photos_album_id" FOREIGN KEY ("album_id") REFERENCES "photo_albums" ("id") ON DELETE CASCADE ON UPDATE CASCADE +); ALTER TABLE "photo_albums" ADD CONSTRAINT "photo_albums_preview_id" FOREIGN KEY ("preview_id") REFERENCES "photos" ("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/tests/inc/model/Model.php b/tests/inc/model/Model.php index cfe2f209..feeebf3e 100644 --- a/tests/inc/model/Model.php +++ b/tests/inc/model/Model.php @@ -21,6 +21,7 @@ * @property-read TagFollowersRepository $tagFollowers * @property-read UsersRepository $users * @property-read UserStatsRepository $userStats + * @property-read UserStatsXRepository $userStatsX */ class Model extends OrmModel { diff --git a/tests/inc/model/userStatX/UserStatX.php b/tests/inc/model/userStatX/UserStatX.php new file mode 100644 index 00000000..27166d64 --- /dev/null +++ b/tests/inc/model/userStatX/UserStatX.php @@ -0,0 +1,17 @@ +