Comments (14)
You are right, thanks for the ideas. What do you think about ->where("id", $array)->or("attr", $scalar)
? It doesn't cover all possible cases but it seems most natural to me. I would probably also add ->and()
as an alias to ->where()
. The only problem is that or
and and
are keywords...
from notorm.
I like the idea, so it will be possible to do the code below naturally, which is very readable with the cost of having both and
& or
as keywords.
$db->user()
->where('id', 1)
->and(array(
'id' => '2',
'active' => true
))
->or(array(
'id' => '2',
'active' => true
));
By seeing the code, I believe we can turn this powerful recursive call to do several layer of conditions, by having a constant to alternate the OR/AND in between conditions.
CODE:
$db->user()
->where('id', 1)
->and(array(
'id' => '2',
new NotORM_Where_And('active', true)
))
->or(array(
'id' => '2',
new NotORM_Where_Or('active', true),
array(
'id' => 3,
new NotORM_Where_Or('active', true)
)
));
OUTPUT:
SELECT *
FROM user
WHERE (id = 1)
AND (id = 2 AND active != 0)
OR (id = 2 OR active != 0 OR (
id = 3
OR 'active' != 0
))
It will be powerful and unfortunately a bit ugly. Maybe there is a better idea to this?
from notorm.
It could be done using the __call
method.
from notorm.
Basically, this will work as well.
$db->user()->where(new NotORM_Where_Or('active', true));
$db->user()->or('active', true); // Equal to above
from notorm.
I was actually refering to the keyword problem. It's not possible to name a method 'or' or 'and' but you can implement them using __call.
from notorm.
Re vrana: I like your idea of passing arguments directly to and() and or().
I'm not sure about where() always being equal to and() because of the semantic meaning of the word "where" in SQL. The good thing about where() = and() would be sort of a backward compatibility.
Re smasty/__call(): Good idea. Tested in PHP 5.3.5 and 5.1.6.
I realize now that we may still need a support for a multidimensional array. We couldn't otherwise build a queries like this:
a] ...WHERE (id=1) OR (id = 2) AND (admin = true) AND (active = true)
which I believe is the same thing as
WHERE ((id=1) OR (id = 2)) AND (admin = true) AND (active = true)
b] ...WHERE (id=1) OR ((id = 2) AND (admin = true) AND (active = true))
c] ...WHERE ((id=1) OR ((id = 2) AND (admin = true))) AND (active = true)
To produce a] I could simply call
a] $my_table->where('id', 1)->or('id', 2)->and('admin', true)->and('active', true)
There's no way however, to create b] or c].
It would be great if we could nest calls to or()/where()/and() but I'm not sure how to go about it. Kind of like:
b] $my_table->where('id', 1)->or(
new NotORM_self('id', 2)->and('admin', true)->and('active', true)
)
c] $my_table->or(
new NotORM_self('id', 1)->or(
new NotORM_self('id', 2)->and('admin', true)
)
)->and('active', true))
This isn't very easy to read though. The multidimensional array looks better to me.
from notorm.
I don't know NotORM well but I think this could work:
b] $my_table->where('id', 1)->or('id = ? AND admin = ? AND active = ?', 1, true, true)
To the where() = and()
and also the __call
.
I've been using it this way for a while now in my database layer and there are no problems with where()=and()
. You can use where()->where()->where()
as well as where()->and()->and()
. The only difference is the second one looks more SQL-like and thus more natural.
from notorm.
Hey Smasty, I think I also don't understand what Jakub means with AND and OR as keyword. Still waiting for his reply on this matter and my other issue.
from notorm.
@wendynovianto I know what is he refering to. AND
and OR
are reserved PHP keywords and thus cannot be used as names of a real method/function. However, this can be bypassed using the PHP magic __call()
method. But it's not that ideal, since such methods are not suggested by IDE when typing and they are bit slower than regular methods.
The IDE support can be bypassed using the @method
annotation on the class (works at least in NetBeans IDE).
The performance issue will still remain, however, since NotORM uses many magic methods I don't see this as a big problem.
Anyhow, I think if Jakub thinks it's a good feature he is probably implementing it as we speek (or he is at work ;-)).
(P.S. to mention someone here on github, you can use the 'at' notation the same way as you would on Twitter: @wendynovianto.)
from notorm.
Hey @smasty, it's a very useful info there on both php keywords and twitter mention!
from notorm.
I've implemented a very simple OR support here: https://github.com/rixbeck/notorm
It's just evaluates AND OR in order, unfortunately doesn't support nesting in bracket.
from notorm.
I need this query with notORM for a search system.
$query = "SELECT * FROM users WHERE (name LIKE '%".$q."%') OR (identification LIKE '%".$q."%') OR (lastame LIKE '%".$q."%')";
I've tried
$result['usuarios'] = $db->users()->where('name = ? OR lastname = ?', '$q' , '$q');
But not work, helpme please
from notorm.
This is now implemented:
$db->table()->where("a", 1)->or("b", 2);
$db->table()->where("a", 1)->and("b", 2);
$db->table()->where("(a", 1)->or("b", 2)->where(")")->and("(a", 2)->or("b", 1)->where(")");
The parentheses syntax is kind of hacky but it's not necessary to use very often.
from notorm.
@cristiamdiaz, you can use this:
$like = "%$q%";
$db->users()->where('name LIKE ? OR identification LIKE ? OR lastmame LIKE ?', $like, $like, $like);
from notorm.
Related Issues (20)
- Should use apcu_* instead of apc_* if available HOT 1
- How to avoid TO_CHAR(date, format) being interpreted as join?
- Can't insert if date fields are empty
- output rows as array HOT 1
- how to know sql query HOT 6
- Cannot set fetch mode - always set to PDO::FETCH_ASSOC HOT 1
- add support for mongodb
- Please support another lang fields.
- Tablename with dash HOT 1
- Set NotORM to PHP7 defaults. HOT 2
- No maintenance? HOT 1
- DELETE All QUERY HOT 1
- "table" in comment is parsed HOT 1
- Is this project dead already ? HOT 5
- Join Question
- fetch() always returns "Call to a member function fetch() on a non-object" HOT 1
- Your letsencrypt SSL certificate has expired on https://www.notorm.com HOT 1
- How to get last inserted id HOT 2
- warning: adding embedded git repository: vendor/vrana/notorm
- PHP 8 compatibility HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from notorm.