Closed jonyhayama closed 10 years ago
Hi,
you can do as follows Using Respect/Relational/Sql:
(https://github.com/Respect/Relational/blob/develop/library/Respect/Relational/Sql.php)
and $ = array ('name' => 'Alexander'); $ ou = array ('id =' => 1); $ sql = new Sql (); $ mapper-> $ table (and $) -> fetch ($ sql () -> or ($ or));
You can use the Sql object statically, but for "or" I'm hoping to correct a small bug where the internal use of the word or words is a key php: http://www.php.net/manual/en/reserved. keywords.php So it will have to instantiate the Sql object.
Hi Cristopher, thank you for the quick response, but I'm afraid this piece of code doesn't work.
I've worked my way to try and figure out what's wrong and here's what I found out:
I took the liberty to fix some minor issues (like changing "and $" to "$and" and adding try/catch instructions) and here's how it looks:
$and = array ('name LIKE' => 'Alexandre'); $or = array ('author.id =' => 1); $sql = new Sql(); try{ $mapper->sc_users($and)->fetch($sql->or($or)); } catch(Exception $e){ echo $e->getMessage(); echo '
'; echo $e->getTraceAsString(); echo ''; }
So, the response I've got was this: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens
I've checked the createStatement method on the Mapper class (where the problem seems be happening) and added a var_dump to the $statement and $query->getParams() variables.
Here's the response: object(PDOStatement)#16 (1) { ["queryString"]=> string(102) "SELECT author.* FROM author WHERE author.name LIKE :AuthorNameLike OR author.id = :AuthorId" }
array(1) { ["ScUsersNameLike"]=> string(7) "Alexander" }
Basically, the getParams() method does not return the "id" param the I've set on the $sql variable...
Is there any other way to achieve this? I noticed that the extra sql I've added in through the fetch method is treated as as string, maybe that's why the params are not being added...
Thank you again for your attention on this matter!
By the way, reviewing the message I realized that the "pre" inside my "echos" where processed as HTML...so that's why it looks a bit confusing, here is the code again:
$and = array ('name LIKE' => 'Alexandre'); $or = array ('author.id =' => 1); $sql = new Sql(); try{ $mapper->sc_users($and)->fetch($sql->or($or)); } catch(Exception $e){ echo $e->getMessage(); echo '<pre>'; echo $e->getTraceAsString(); echo '</pre>'; }
Hi again,
I can't believe I'm so Stupid. Your code worked like a charm, I just didn't realize I had to use the 'develop' version. I was using version 0.5.1 (through composer). Thank you again for your support and patience.
do not have that ..
Hi, I've just stumbled on a simple thing. I'm sorry if it is already answered somewhere, but I couldn't how to do a condition with the "OR" operator, instead of "AND".
I mean, basically, I'm searching for an author. The documentation shows this: $mapper->author(array("name"=>"Alexandre"))->fetch();
Which works fine, but let's say I'm not really sure of what the user may have typed on the search box (it could be the author's name or the author's id). I tried this: $mapper->author(array("name"=>"Alexandre", "id"=>1))->fetch();
I believe this generates the following SQL (or something like this): SELECT * FROM author WHERE name = Alexandre AND id = 1
I would actually like a way to generate the following (or something like it): SELECT * FROM author WHERE name = Alexandre OR id = 1
Is it possible? Thanks in advance!