PHP Classes

Yii2 Query Relation Manager: Run queries to model objects using filters

Recommend this page to a friend!
  Info   View files Example   View files View files (25)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Last Updated Ratings Unique User Downloads Download Rankings
2023-03-06 (7 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 28 All time: 10,920 This week: 108Up
Version License PHP version Categories
yii2-query-relation- 1.0MIT/X Consortium ...7.2Databases, Libraries, Design Patterns, P...
Description 

Author

This package can run queries to model objects using filters.

It provides a trait that can be used with YII2 with model classes that use the ActiveRecord design pattern to add a query relation manager.

The package allows developers to compose and execute queries to model class objects.

The queries can use relations between model classes and filters to define conditions for the objects that the execution of the queries returns as results.

Innovation Award
PHP Programming Innovation award nominee
March 2023
Number 3
ActiveRecord is a popular design pattern implemented by many frameworks based on the Model-View-Controller (MVC) design pattern to store and retrieve objects stored, for instance, in SQL databases.

Applications that use the MVC design pattern usually need to perform queries to retrieve certain model class objects that match certain conditions.

This package uses a query relation manager trait to add the ability to model classes to elaborate complex queries on model objects that match conditions that may include filters on the model object properties and relations with other model class objects.

Manuel Lemos
Picture of Smoren  Freelight
  Performance   Level  
Name: Smoren Freelight <contact>
Classes: 29 packages by
Country: Russian Federation Russian Federation
Age: 34
All time rank: 325988 in Russian Federation Russian Federation
Week rank: 34 Up2 in Russian Federation Russian Federation Up
Innovation award
Innovation award
Nominee: 14x

Example

<?php

$config
= require __DIR__.'/../unit/Config/db.php';

$dbh = new PDO(
   
$config['dsn'],
   
$config['username'],
   
$config['password'],
    [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
);

echo
"Connected. Creating tables...\n";

$dbh->query(
   
'CREATE TABLE `city` (
        `id` int NOT NULL AUTO_INCREMENT,
        `name` varchar(255) NOT NULL,
        PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb3;'
);

$dbh->query(
   
'CREATE TABLE `address` (
        `id` int NOT NULL AUTO_INCREMENT,
        `city_id` int NOT NULL,
        `name` varchar(255) NOT NULL,
        PRIMARY KEY (`id`),
        KEY `idx-address-city_id` (`city_id`),
        CONSTRAINT `fk-address-city_id-city-id` FOREIGN KEY (`city_id`) REFERENCES `city` (`id`) ON DELETE CASCADE
    ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb3;'
);

$dbh->query(
   
'CREATE TABLE `place` (
        `id` int NOT NULL AUTO_INCREMENT,
        `address_id` int NOT NULL,
        `name` varchar(255) NOT NULL,
        PRIMARY KEY (`id`),
        KEY `idx-place-address_id` (`address_id`),
        CONSTRAINT `fk-place-address_id-address-id` FOREIGN KEY (`address_id`) REFERENCES `address` (`id`) ON DELETE CASCADE
    ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb3;'
);

$dbh->query(
   
'CREATE TABLE `comment` (
        `id` int NOT NULL AUTO_INCREMENT,
        `place_id` int NOT NULL,
        `username` varchar(255) NOT NULL,
        `mark` tinyint NOT NULL,
        `text` text NOT NULL,
        PRIMARY KEY (`id`),
        KEY `idx-comment-place_id` (`place_id`),
        CONSTRAINT `fk-comment-place_id-place-id` FOREIGN KEY (`place_id`) REFERENCES `place` (`id`) ON DELETE CASCADE
    ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb3;'
);

echo
"Inserting data...\n";

$dbh->query(
   
"INSERT INTO city (id,name) VALUES
         (1,'Moscow'),
         (2,'St. Petersburg'),
         (3,'Samara'),
         (4,'Barnaul'),
         (5,'Ivanovo');"
);

$dbh->query(
   
"INSERT INTO address (id,city_id,name) VALUES
         (1,1,'Tverskaya st., 7'),
         (2,1,'Schipok st., 1'),
         (3,2,'Mayakovskogo st., 12'),
         (4,2,'Galernaya st., 3');"
);

$dbh->query(
   
"INSERT INTO place (id,address_id,name) VALUES
         (1,1,'TC Tverskoy'),
         (2,1,'Tverskaya cafe'),
         (3,2,'Stasova music school'),
         (4,3,'Hostel on Mayakovskaya'),
         (5,3,'Mayakovskiy Store'),
         (6,4,'Cafe on Galernaya');"
);

$dbh->query(
   
"INSERT INTO comment (id,place_id,username,mark,`text`) VALUES
         (1,1,'Ivan Mustafaevich',3,'Not bad, not good'),
         (2,1,'Peter',5,'Good place'),
         (3,1,'Mark',1,'Bad place'),
         (4,3,'Ann',5,'The best music school!'),
         (5,5,'Stas',4,'Rather good place'),
         (6,6,'Stas',3,'Small menu, long wait');"
);

echo
"Migration complete!\n";


  Files folder image Files  
File Role Description
Files folder image.github (1 directory)
Files folder imagesrc (4 files)
Files folder imagetests (3 files, 3 directories)
Accessible without login Plain text file codeception.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Read me

  Files folder image Files  /  .github  
File Role Description
Files folder imageworkflows (1 file)

  Files folder image Files  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file test_master.yml Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
  Plain text file ActiveRecordTrait.php Class Class source
  Plain text file QueryRelationDataProvider.php Class Class source
  Plain text file QueryRelationManager.php Class Class source
  Plain text file QueryWrapper.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imagescripts (1 file)
Files folder imageunit (4 files, 2 directories)
Files folder image_support (1 file)
  Accessible without login Plain text file coding_standard.xml Data Auxiliary data
  Accessible without login Plain text file unit.suite.yml Data Auxiliary data
  Accessible without login Plain text file _bootstrap.php Aux. Auxiliary script

  Files folder image Files  /  tests  /  scripts  
File Role Description
  Accessible without login Plain text file migrate.php Example Example script

  Files folder image Files  /  tests  /  unit  
File Role Description
Files folder imageConfig (2 files)
Files folder imageModels (4 files, 1 directory)
  Plain text file CommonUsageTest.php Class Class source
  Plain text file DataProviderTest.php Class Class source
  Plain text file ErrorsTest.php Class Class source
  Plain text file WithSyntaxTest.php Class Class source

  Files folder image Files  /  tests  /  unit  /  Config  
File Role Description
  Accessible without login Plain text file db.php Aux. Auxiliary script
  Accessible without login Plain text file test.php Aux. Auxiliary script

  Files folder image Files  /  tests  /  unit  /  Models  
File Role Description
Files folder imageBad (1 file)
  Plain text file Address.php Class Class source
  Plain text file City.php Class Class source
  Plain text file Comment.php Class Class source
  Plain text file Place.php Class Class source

  Files folder image Files  /  tests  /  unit  /  Models  /  Bad  
File Role Description
  Plain text file NonActiveRecordClass.php Class Class source

  Files folder image Files  /  tests  /  _support  
File Role Description
  Plain text file UnitTester.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:28
This week:0
All time:10,920
This week:108Up