xPDO将整数编译为字符串

时间:2013-11-25 08:32:02

标签: modx modx-revolution modx-resources xpdo

$query = $this->modx->newQuery('modResource');
$query->leftJoin('modTemplateVarResource', "price", array("modResource.id = price.contentid", "price.tmplvarid = 2"));
$query->where(array("price:>=" => 6000));
$query->prepare();
echo $query->toSql();

返回值:

`modResource`.`context_key` AS `modResource_context_key`, `modResource`.`content_type` AS `modResource_content_type`, `modResource`.`uri` AS `modResource_uri`, `modResource`.`uri_override` AS `modResource_uri_override`, `modResource`.`hide_children_in_tree` AS `modResource_hide_children_in_tree`, `modResource`.`show_in_tree` AS `modResource_show_in_tree`, `modResource`.`properties` AS `modResource_properties`
FROM `modx_site_content` AS `modResource`
LEFT JOIN `modx_site_tmplvar_contentvalues` `price` 
    ON ( modResource.id = price.contentid 
        AND price.tmplvarid = 2 ) 
WHERE `modResource`.`price` >= '6000' 

为什么引用整数?它应该被视为一个int。

3 个答案:

答案 0 :(得分:2)

这可能不起作用,因为“price”是连接上使用的表别名;你需要针对实际字段运行where where条件 - price.value

$query = $this->modx->newQuery('modResource');
$query->leftJoin('modTemplateVarResource', "price", array("modResource.id = price.contentid", "price.tmplvarid = 2"));
$query->where(array("price.value:>=" => 6000));
$query->prepare();
echo $query->toSql();

我几乎100%肯定在这种情况下where条件会起作用,即使是字符串。但是,如果您仍遇到问题,可以尝试将“price.value”转换为整数(未经测试):

$query = $this->modx->newQuery('modResource');
$query->leftJoin('modTemplateVarResource', "price", array("modResource.id = price.contentid", "price.tmplvarid = 2"));
$query->where(array("CAST(price.value AS UNSIGNED INTEGER):>=" => 6000));
$query->prepare();
echo $query->toSql();

PS。浮动/小数通常是更合适的价格类型,但这取决于你;)

答案 1 :(得分:0)

似乎我找到了另一种解决方案 - 最简单的方法是

$ query-> where(“price.value> = 6000”);

okyanet解决方案

结合的最佳方式

$ query-> where(“CAST(price.value AS UNSIGNED INTEGER)> = 6000”);

所有这些都在xPDO

中正常工作

答案 2 :(得分:0)

正确的方法是使用

$whereArray = array('CAST(:price.value:as unsigned)>=' => 6000);
//then put it to
$query->where($whereArray);