我实际上遇到了Doctrine_Query的问题。 这是一个Symfony 1项目(1.4.20)。
这是最后的错误消息:
SQLSTATE [HY093]:参数号无效:绑定变量数 与令牌数量不匹配
以下是构建查询的代码:
public function search ($keywords_article, $lang, $keywords_produit = '', $limit = 0, $offset = 0)
{
$q = $this->createQuery('a')
->innerJoin('a.FlatSearchArticles fa')
->innerJoin('a.Translation t')
->where('t.lang = ?', $lang)
->addWhere('fa.lang = ?', $lang)
->addWhere('fa.reference LIKE "%?%" Or fa.nom LIKE "%?%"', array($keywords_article, $keywords_article));
if ('' != $keywords_produit)
$q->innerJoin('fa.Produits fp')
->addWhere('fp.lang = ?', $lang)
->addWhere('fp.reference LIKE "%?%" Or fp.nom LIKE "%?%"', array($keywords_produit, $keywords_produit));
if (0 != $limit)
$q->limit($limit);
if (0 != $offset)
$q->offset($offset);
return $q->execute();
}
堆栈跟踪:
1. at ()
in SF_SYMFONY_LIB_DIR/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Connection.php line 1082 ...
2. at Doctrine_Connection->rethrowException(object('PDOException'), object('Doctrine_Connection_Statement'))
in SF_SYMFONY_LIB_DIR/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Connection/Statement.php line 269 ...
3. at Doctrine_Connection_Statement->execute(array('fr', 'fr', 'ALLUM', 'ALLUM', 'fr', 'IDRA', 'IDRA'))
in SF_SYMFONY_LIB_DIR/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Connection.php line 1006 ...
4. at Doctrine_Connection->execute('SELECT i.id AS i__id, i.reference AS i__reference, i.art_code AS i__art_code, i.commentaire AS i__commentaire, i.image AS i__image, i.marque_id AS i__marque_id, i.article_type_id AS i__article_type_id, i.reference_fournisseur AS i__reference_fournisseur, i.historique AS i__historique, i.article_id AS i__article_id, i.reference_client AS i__reference_client, i.domaine_id AS i__domaine_id, i.migration_id AS i__migration_id, i.date_validite AS i__date_validite, i.list_pays AS i__list_pays, i.type AS i__type, i.created_at AS i__created_at, i.updated_at AS i__updated_at, f.id AS f__id, f.reference AS f__reference, f.nom AS f__nom, f.lang AS f__lang, f.article_id AS f__article_id, i2.id AS i2__id, i2.nom AS i2__nom, i2.lang AS i2__lang, f2.id AS f2__id, f2.reference AS f2__reference, f2.nom AS f2__nom, f2.lang AS f2__lang, f2.produit_id AS f2__produit_id, f2.domaine_id AS f2__domaine_id, f2.marque_id AS f2__marque_id FROM item i INNER JOIN flat_search_article f ON i.id = f.article_id INNER JOIN item_translation i2 ON i.id = i2.id INNER JOIN flat_search_article_produit f3 ON (f.id = f3.article_id) INNER JOIN flat_search_produit f2 ON f2.id = f3.produit_id WHERE (i2.lang = ? AND f.lang = ? AND (f.reference LIKE "%?%" OR f.nom LIKE "%?%") AND f2.lang = ? AND (f2.reference LIKE "%?%" OR f2.nom LIKE "%?%") AND (i.type = 1))', array('fr', 'fr', 'ALLUM', 'ALLUM', 'fr', 'IDRA', 'IDRA'))
in SF_SYMFONY_LIB_DIR/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Query/Abstract.php line 983 ...
5. at Doctrine_Query_Abstract->_execute(array())
in SF_SYMFONY_LIB_DIR/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Query/Abstract.php line 1033 ...
6. at Doctrine_Query_Abstract->execute()
in SF_ROOT_DIR/lib/model/doctrine/ArticleTable.class.php line 90 ...
当我在mysql控制台客户端中尝试相同的查询时,它可以工作:
mysql> SELECT i.id AS i__id, i.reference AS i__reference, i.art_code AS i__art_code, i.commentaire AS i__commentaire, i.image AS i__image, i.marque_id AS i__marque_id, i.article_type_id AS i__article_type_id, i.reference_fournisseur AS i__reference_fournisseur, i.historique AS i__historique, i.article_id AS i__article_id, i.reference_client AS i__reference_client, i.domaine_id AS i__domaine_id, i.migration_id AS i__migration_id, i.date_validite AS i__date_validite, i.list_pays AS i__list_pays, i.type AS i__type, i.created_at AS i__created_at, i.updated_at AS i__updated_at, f.id AS f__id, f.reference AS f__reference, f.nom AS f__nom, f.lang AS f__lang, f.article_id AS f__article_id, i2.id AS i2__id, i2.nom AS i2__nom, i2.lang AS i2__lang, f2.id AS f2__id, f2.reference AS f2__reference, f2.nom AS f2__nom, f2.lang AS f2__lang, f2.produit_id AS f2__produit_id, f2.domaine_id AS f2__domaine_id, f2.marque_id AS f2__marque_id FROM item i INNER JOIN flat_search_article f ON i.id = f.article_id INNER JOIN item_translation i2 ON i.id = i2.id INNER JOIN flat_search_article_produit f3 ON (f.id = f3.article_id) INNER JOIN flat_search_produit f2 ON f2.id = f3.produit_id WHERE (i2.lang = 'fr' AND f.lang = 'fr' AND (f.reference LIKE "%ALLUM%" OR f.nom LIKE "%ALLUM%") AND f2.lang = 'fr' AND (f2.reference LIKE "%IDRA%" OR f2.nom LIKE "%IDRA%") AND (i.type = 1));
***/***
35 rows in set (0.00 sec)
我不明白为什么Symfony1 / Doctrine / PHP Pdo不会"喜欢"我的询问。
感谢您的支持, 大卫。
答案 0 :(得分:1)
?
不是此标记:
->addWhere('fa.reference LIKE "%?%" Or fa.nom LIKE "%?%"', array($keywords_article, $keywords_article));
相反传递方式:
->addWhere('fa.reference LIKE ? Or fa.nom LIKE ?', array('%' . $keywords_article . '%', '%' . $keywords_article . '%'));
答案 1 :(得分:0)
我在我身边找到的另一个解决方案:
public function search ($keywords_article, $lang, $keywords_produit = '', $limit = 0, $offset = 0)
{
$q = $this->createQuery('a')
->innerJoin('a.FlatSearchArticles fa')
->innerJoin('a.Translation t')
->where('t.lang = :lang')
->addWhere('fa.lang = :lang')
->addWhere('fa.reference LIKE "%:kwa%" OR fa.nom LIKE "%:kwa%"');
if ('' != $keywords_produit)
$q->innerJoin('fa.FlatSearchProduits fp')
->addWhere('fp.lang = :lang')
->addWhere('fp.reference LIKE "%:kwp%" OR fp.nom LIKE "%:kwp%"');
if (0 != $limit)
$q->limit($limit);
if (0 != $offset)
$q->offset($offset);
return $q->execute(array(
':lang' => $lang,
':kwa' => $keywords_article,
':kwp' => $keywords_produit,
));
}
我最终更喜欢使用":key"。
感谢Marek这个答案也很好。
再见 大卫。