Respect / Respect/Relational

Bug when conditions are applied to more than one table

Open
#47 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
237
Forks
32
PR merge metrics
No merged PRs in 30d

Description

Hello all of you who develop Respect. First, let me thank you and say you're doing a really good job! I'm using Respect in a project of mine and today I was not able to perform a query based on the examples in your documentation.

The project I'm working on is a CMS, and I want to fetch all the contents that belongs to the category of ID equal to 30, which ID is different from 60. According to your documentation, I thought of this command:

$aContent = $mapper->content(array('id !=' => 60))->category[30]->fetchAll();

It was not bringing me the contents I expected, so I dove into the code and inserted an echo where you generate the query to see what the framework was looking for (release 0.5.1, file Mapper.php, line 223):

    protected function createStatement(
        Collection $collection, $withExtra = null
    ) {
        $query = $this->generateQuery($collection);

        if ($withExtra instanceof Sql) {
            $query->appendQuery($withExtra);
        }
        echo $query; exit;

The generated query was:

SELECT content.*, category.* FROM content INNER JOIN category ON content.category_id = category.id WHERE category.id = ?

I decided to replace my command by your example:

<?php $mapper->comment(array("created_at >"=>strtotime('today')))
             ->post(array("created_at >"=>strtotime('7 days ago')))
             ->author[7]
             ->fetchAll();

And the generated query was actually:

SELECT comment.*, post.*, author.* FROM comment INNER JOIN post ON comment.post_id = post.id INNER JOIN author ON post.author_id = author.id WHERE author.id = ?

It seems it's only taking into account the last condition from the join.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with library/Respect/Relational/Mapper.php around line 223 and reproduce the documented multi-table examples. Trace how the query is generated for conditions on content, comment, and post alongside the final joined-table condition. Done means predicates from every specified table are represented in the generated SQL, rather than only the last join condition.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.