Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple where conditions giving SQL error #4

Open
DavidOliver opened this issue Jul 8, 2012 · 4 comments
Open

Multiple where conditions giving SQL error #4

DavidOliver opened this issue Jul 8, 2012 · 4 comments
Labels

Comments

@DavidOliver
Copy link

Version: 0.6.2 (Symphony 2.2.5)

I have the following in an event:

$products_query = new SymQLQuery();
$products_query
    ->select('name, brand, instances')
    ->from('products')
    ->perPage(999);
foreach ($_SESSION['sym-cart'] as $key => $item) {
    $products_query->where('instances', $key);
}
$products = SymQL::run($products_query);

The instances field is a SBL+ field.

When there is more than one product in the cart, meaning ->where is called more than once, and the event is triggered:

Symphony Fatal Database Error
Not unique table/alias: 't137'
An error occurred while attempting to execute the following query
SELECT SQL_CACHE e.id, e.section_id, e.author_id, UNIX_TIMESTAMP(e.creation_date) AS creation_date FROM sym_entries AS e LEFT JOIN sym_entries_data_137 AS t137 ON (e.id = t137.entry_id) LEFT JOIN sym_entries_data_137 AS t137 ON (e.id = t137.entry_id) WHERE 1 AND e.section_id = '1' AND t137.relation_id IN ('1723') AND t137.relation_id IN ('3096') ORDER BY e.idDESC LIMIT 0, 999

The t137 alias is created more than once.

When ->where is called multiple times using system:id, this error doesn't occur.

@nickdunn
Copy link
Contributor

nickdunn commented Jul 8, 2012

Hmm yes, this is most likely more of a Symphony bug, I think. It will try to join the SBL table multiple times, which is unnecessary. You should create an array of the keys you want to filter on, and set the where once:

$keys = array();
foreach ($_SESSION['sym-cart'] as $key => $item) {
    $keys[] = $key;
}
$products_query->where('instances', implode(', ', $keys));

I think this will work as Symphony will take the comma delimited string of IDs and converts it to an array.

@DavidOliver
Copy link
Author

Nick, I see - thanks.

I'm now getting product entry objects, but they're the wrong ones. Instead of the two products that contain the instances given, I'm getting three products that have nothing to do with the instances. (My "instances" are versions of products.)

I've checked that the implode is giving the right instance ids.

@DavidOliver
Copy link
Author

Fix from ChriZ?

@nitriques
Copy link
Member

@DavidOliver Pull Request ? :P

@nitriques nitriques added the bug label Apr 19, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants