-
Notifications
You must be signed in to change notification settings - Fork 398
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
Using addJoin causes where clauses to fail #1919
Comments
Oh, I think the preferred way to do that join or filter through another table is: BookQuery::create()
->useAuthorQuery()
->filterByFirstName('%Charles%', Criteria::LIKE)
->endUse()
->joinWithAuthor() // populates the author field in the returned books, omit it if you just want to filter
->find(); If for some reason you absolutely want to use $query = BookQuery::create();
$query->addJoin(BookTableMap::COL_AUTHOR_ID, AuthorTableMap::COL_ID, Criteria::INNER_JOIN);
$query->where("Author.First LIKE '%Charles%'"); |
I can't use the first way because there is no relation set up (no foreign key). My example may not be a good example, but just assume that there is no relation and I am trying to build a join on any arbitrary column. Maybe I want to find all books whose main character's last name is the same as the author's last name... It seems dangerous to add a where without using a prepared statement. Would it still be protected against sql injections? I wouldn't really feel comfortable with that without digging in a little more. My workaround is preferable. |
For further clarity, I'm actually running a query on an Archive table using the |
Ah, I see. Does something like this work: $authorQuery = AuthorQuery::create()->filterByFirstName('Neal%', Criteria::LIKE);
BookQuery::create()->whereExists($authorQuery)->find() Created query looks like this: SELECT *
FROM book
WHERE EXISTS (
SELECT 1
FROM author
WHERE author.first_name LIKE ?
) If you need to use a column from the outer query in the $authorQuery = AuthorQuery::create()->where('Book.Title LIKE CONCAT(\'%\', Author.FirstName,\'%\')');
BookQuery::create()->whereExists($authorQuery)->find()
Yes, same with |
That's an interesting solution and might work. Why would it be preferable to the workaround that I posted above? |
Oh, your workaround is fine, but it is a lot of work (kudos!) and code to maintain. I think you basically set up the join as Propel does for registered relations. |
Uff, just realized the query above obviously does not work, this SELECT *
FROM book
WHERE EXISTS (
SELECT 1
FROM author
WHERE author.first_name LIKE ?
) will give you all books, you always need to bind an $authorQuery = AuthorQuery::create()
->filterByFirstName('Neal%', Criteria::LIKE)
->where('Author.Id = Book.AuthorId');
BookQuery::create()->whereExists($authorQuery)->find(); sorry, tired, lol |
When using
addJoin
to join a table on column with no foreign key,where
clauses are unable to extract the join table name appropriately resulting in the following error:PHP Fatal error: Uncaught Propel\Runtime\Exception\PropelException: Cannot determine the column to bind to the parameter in clause "Author.First LIKE ?".
Example code is:
$query = BookQuery::create();
$query->addJoin(BookTableMap::COL_AUTHOR_ID, AuthorTableMap::COL_ID, Criteria::INNER_JOIN);
$query->where("Author.First LIKE ?", '%Charles%');
It seems that this error comes up because
ModelCriteria::getColumnFromName
is unable to find the join object because the join is not set up with a key. I am able to get around it by setting up the join object explicitly as aModelJoin
but it should be able to work using the simpleaddJoin
.Workaround:
$join = new ModelJoin();
$join->setJoinType(Criteria::INNER_JOIN);
$join->setTableMap(AuthorTableMap::getTableMap());
$join->setLeftTableName('Book');
$join->setRightTableName('Author');
$join->addCondition(BookTableMap::COL_AUTHOR_ID, AuthorTableMap::COL_ID);
$query = BookQuery::create();
$query->addJoinObject($join, 'Author');
$query->where("Author.First LIKE ?", "%Charles%");
I would suggest either setting the join key in the
addJoin
method with the value of the right table name or alias and/or adding a block to theModelCriteria::getColumnFromName
which correctly selects the join from the array of joins (similar to howgetModelJoinByTableName
but not limiting it toModelJoin
and selecting the join based on Alias).The text was updated successfully, but these errors were encountered: