Respect / Relational

A fluent, intuitive ORM for any relational database engine
http://respect.github.io/Relational
Other
243 stars 32 forks source link

Conditions #50

Closed jonyhayama closed 10 years ago

jonyhayama commented 10 years ago

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!

cristopher-rodrigues commented 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.

jonyhayama commented 10 years ago

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

0 [PATH]\vendor\respect\relational\library\Respect\Relational\Mapper.php(237): PDOStatement->execute(Array)

1 [PATH]vendor\respect\data\library\Respect\Data\AbstractMapper.php(65): Respect\Relational\Mapper->createStatement(Object(Respect\Data\Collections\Collection), Object(Respect\Relational\Sql))

2 [PATH]\vendor\respect\data\library\Respect\Data\Collections\Collection.php(115): Respect\Data\AbstractMapper->fetch(Object(Respect\Data\Collections\Collection), Object(Respect\Relational\Sql))

3 [PATH]\index.php(31): Respect\Data\Collections\Collection->fetch(Object(Respect\Relational\Sql))

4 {main}

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!

jonyhayama commented 10 years ago

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>';
}
jonyhayama commented 10 years ago

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.

cristopher-rodrigues commented 10 years ago

do not have that ..