Propel2
Propel2 copied to clipboard
Using addJoin causes where clauses to fail
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 a ModelJoin
but it should be able to work using the simple addJoin
.
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 the ModelCriteria::getColumnFromName
which correctly selects the join from the array of joins (similar to how getModelJoinByTableName
but not limiting it to ModelJoin
and selecting the join based on Alias).