X
X
xpeter2014-01-03 14:28:21
PHP
xpeter, 2014-01-03 14:28:21

Do I need another ORM - Query-Builder?

New Year holidays. It's time to write something useful. But there are many ideas, and not all can be good. Therefore, I want to consult with you.
For some reason, many popular ORMs have a syntax that is essentially sql queries, only in php expressions, such as:

$females = ORM::for_table('person')->where('gender', 'female')->find_many();

People who don't use ORM use PDO constructs, which are rather clumsy and tend to repeat themselves in the same form all the time.
What would you like? More elegant inner-noble things. But light and easy to understand.
1. I want beautiful native OOP-style selects:
For example, to select users over 30 years old and display them on the page, I would like to see something like:
foreach ($db->users->age->gt(30) as $user) {
  echo $user->title;
}

2. A simple call to count found rows - count, isEmpty methods
Are there any users over 28 years old? 3. Beautiful native Join's Very often the syntax for joins in the ORM is such that in fact this is an sql query, only in calls to php methods. I would like to see it in a more elegant form:
$db->users->age(28)->isEmpty();
$db->users
    ->extend($db->roles->id->title->userId($db->users->id))
    ->find();

4. Transparent type casting after query execution:
$db->users
    ->id->toInt()
    ->title->toString()
    ->find();

5. Batch multi-insert rows
For some reason, very often in ORM they miss such a nice thing and everyone writes another crutch with arrays and generation of the insert row based on it.
$db->users
    ->new()->title('John')
    ->new()->title('Jack')
    ->new()->title('Smith')
    ->save();

6. Conditional conditions
No matter how terrible it sounds, the same code is found in applications: if there is any parameter, then add it to the request and to the bind variables. I would like to see it in this form:
$db->users
    ->name('john')
    ->if($age)->age($age)
    ->find();

7. Transparent camelCase
In mysql tables and fields are named with underscores, in PHP camel-case is often used. The library could transparently convert these two writing formats:
$db->users->fullName(‘Jack Jones’);
8. One-liners
If you want to display the name of the current user on the page, it could look like this:
$db->users($id)->title
What do you think about this? Are you satisfied with the ORM and query-builder you use?

Answer the question

In order to leave comments, you need to log in

7 answer(s)
G
gro, 2014-01-03
@gro

Do you need another ORM with this syntax?
If you need it, do it and don't ask anyone.

S
Sergey, 2014-01-03
Protko @Fesor

I like your version less than Doctrine2's QueryBuilder implementation. And there is also DQL, which is also sometimes convenient. Yes, and I'm personally satisfied with it.

R
Ruslan Shashkov, 2014-01-03
@rshashkov

In fact yes. If it’s convenient for you, do it without asking, and there will always be like-minded people, the idea is good. I really like this approach too. Will wait :-)

A
Alexander A, 2014-01-15
@lordsc

I don't like the current approach in php orm. and I really like how in django.
I'm trying to do something similar https://github.com/buldezir/dja_orm
check it out, if it's not difficult, feedback is better on skype ( buldezir )

X
xAockd, 2014-01-03
@xAockd

I am using Eloquent (Laravel). Very simple, concise, convenient. I also worked with doctrine2, but Eloquent is enough for most of my tasks.

A
Alexander N++, 2014-01-03
@sanchezzzhak

but I didn't like it, it reminds me of the for_table kohana style.
Then it's better to just table In
laravel Eloquent'a quite a convenient ORM

R
Ruslan Shashkov, 2014-03-01
@rshashkov

Well, is there an ORM movement? :-)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question