A
A
Alexander2016-04-19 21:17:23
PHP
Alexander, 2016-04-19 21:17:23

Bicycle "ORM" for automation. Should it continue and in what direction? Is it interesting?

A class for working with pdo has been created (for internal needs).
There is the following:
An abstract class for creating
SQL models query builder
Mini-caching of repeated queries within a connection (including checking for changed data).
Abstract Methods:
where():
where() - array
[
"user" => "10",
["%and", "status/!=|number" => "rhfghdfgh$%^456456"],
],
['%and',"date/>=" => "//now()//"
user = :user and status <> :status and (type = :type or type = :type1 or (st = :st or st = :st2)) and date >= now() or status = :status3
data:
( [ user] => 10 [status] => 456456 [type] => admin [type1] => advert [st] => 2 [st2] => 3 [
status3 ] => 1 ) a readable template for pdo (including parentheses, handling free requests like "now()" and filtering data) and an array with a list of keys and values ​​for this template.
selectBy() - array
Example:
selectBy(array("count(*)","id"))
Result:
select count(*), id
Allows you to retrieve certain values ​​from the database
+ grouping of results for easy unloading and working with several tables at the same time
selectBy(array("count(*) as count<-user","id<-user"))
Result:

Array (

[0] => stdClass Object
    (
        [user] => stdClass Object
            (
                [count] => 10
                [id] => 17
            )
     )

)

orderBy() - array
Sort
Example:
orderBy(array("date"=>"asc"))
Result:
Ipattern + order by date asc
limit() - array
Example:
limit(array("1"=>"100") )
Result:
LIMIT 1,100
data() - array
(for add and update methods)
Example:
data(array("id"=>"100","status"=>"2"))
Result:
id='100', status='2'
find() - Find data
Model::selectBy("COUNT(id)")->where(array("id"=>2))->find();
Model::selectBy("COUNT(id)")->find(array("id"=>2));

Model::where(array("id"=>2))->update(array("status"=>2));
delete() - Delete data
Model::where(array("id"=>2))->dalete();
add() - Add data
Model::data(array("status"=>2,"name"=>"name"))->add();
Model::add(array("status"=>2,"name"=>"name"));
Builder environment "where": [[[],[[],[[],[...]]]]]
Levels of investments are divided brackets "()"
Attachment may contain:
array("%and/%or ","key"=>"val")
Key may contain:
"key/rule|filter"
Example:
"id/>=|number"=>"1dfsg0456hfgh##$%"
Rules:
=, !=, >=, <=, >, <, like, in, not in
More about groupings:
$table = CollectTables::selectBy(
["users.login<-user","userGroups.name as groupName<-group","userGroups.permissions<-group"]
)->
table(["users", "userGroups"])->
find(
array("users->group" => "//userGroups.id //","users->id/<"=>1)
);
print_r($table->rows);
result:
Array (

[0] => stdClass Object

    (

        [user] => stdClass Object

            (

                [login] => Demo User

            )

        [group] => stdClass Object

            (

                [groupName] => VIP

                [permissions] => {"createFlow":"1"}

            )

    )

)

1 - Will it be useful to at least someone besides me?
2 - In what direction to move and what are the jambs of this way of working with the base?
3 - Is it interesting for you and should it be published for everyone?
4 - Would anyone like to help develop this library and test it for themselves?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
D', 2016-04-19
@Denormalization

Some brain-crushing syntax for where/select/order. What is the legacy of brainfuck?
I would be afraid to use this. The extra parenthesis is not there, and Messrs.

T
trevoga_su, 2016-04-20
@trevoga_su

1 - Will it be useful to at least someone besides me?
no, but for you it can be quite an acceptable tool if you hone it to a shine.
2 - In what direction to move and what are the jambs of this way of working with the base?
your cant is that you write yourself without understanding what and call it all very big names, such as ORM
, while you mixed everything together and I don’t see any ORM here,
I see an attempt to write 1001 an unnecessary query builder
ORM should work based on patterns, such as ActiveRecord or DataMapper, get model objects, and not just stdClass and broadcast these objects back to the base:
all these jerks
Model::data(array("status"=>2))->where(array("id"=>2))->update();
nobody needs them. because real queries are much more complex than selecting a single where clause. There are requests that simply cannot be put into builders. you focus on builders, but you should focus on ORM. Here's an example of a data mapper class . for its performance, a simple builder is used, but complex sql is written by hand. and almost always, not just objects are returned, but model objects associated with this data mapper. Note that almost every method calls
return parent::result2objects(...);
return parent::findModelListByParams($params);
return parent::findModelByParams($params);

those. The ORM always returns real domain objects, not just stdClass.

D
dmitriy, 2016-04-20
@dmitriylanets

unfortunately

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question