这是我在这里的第一个问题,所以请耐心等待:)
我偶然发现了一种填充对象的奇怪行为。
我开始将项目中使用的objectQuery::create()-> ... ->find()
方法转换为$c = new Criteria(), $c-> ... objectPeer::doSelect($c)
,因为我被告知当条件可以时不应该使用查询。
我有一个函数,它返回商店里所有商品的价格。或者至少做到了。我想不通的是:
旧代码:
static public function shopGetPrices($id){
$prices = itemPriceQuery::create()->
addJoin(itemPricePeer::ITEM_ID, itemPeer::ID, Criteria::LEFT_JOIN)->
addJoin(itemPeer::CATEGORY_ID, categoryPeer::ID, Criteria::LEFT_JOIN)->
addJoin(categoryPeer::SHOP_ID, shopPeer::ID, Criteria::LEFT_JOIN)->
add(shopPeer::ID, $id)->find();
return $prices;
}
返回正确填充的PropelObjectCollection
对象,通过该对象我可以使用foreach,并获取/设置我需要的itemPrice对象和属性。
现在,新的代码:
static public function shopGetPrices($id){
$c = new Criteria();
$c->addJoin(itemPricePeer::ITEM_ID, itemPeer::ID, Criteria::LEFT_JOIN)->
addJoin(itemPeer::CATEGORY_ID, categoryPeer::ID, Criteria::LEFT_JOIN)->
addJoin(categoryPeer::SHOP_ID, shopPeer::ID, Criteria::LEFT_JOIN)->
add(shopPeer::ID, $id);
return self::DoSelect($c);
}
返回一个itemPrice
对象的数组,但它们通过联接填充了与itemPrice对象相关的项值。这意味着:当我调用print_r(self::DoSelect($c));
时,它会打印
Array
(
[0] => ItemPrice Object
(
[startCopy:protected] =>
[id:protected] => 47 <- id of joined item
[item_id:protected] => 9 <-foreign key to category object of joined item
[price:protected] => 0
[unit:protected] => Axe <- name of item, not unit (unit is like 'golden', 'iron', 'wood' or whatever )
[active:protected] =>
[collItemsOrder:protected] =>
[collItemsOrderPartial:protected] =>
[alreadyInSave:protected] =>
[alreadyInValidation:protected] =>
[polozkyObjednavkasScheduledForDeletion:protected] =>
[prisadyPolozkyObjednavkasScheduledForDeletion:protected] =>
[validationFailures:protected] => Array()
[_new:protected] =>
[_deleted:protected] =>
[modifiedColumns:protected] => Array()
[virtualColumns:protected] => Array()
)
[1] => ItemPrice Object
(
等等
在条件和查询对象之间可能存在一些关键的区别,这是我遗漏的。我在谷歌上搜索了StackOverflow,谁知道在哪里,但我没有找到任何类似的解决方案。
这个家伙/女孩也有类似的问题,但我的标准没有使用addSelectColumn
,所以这对我来说又是一条死胡同。
谁能告诉我正确的方向吗?
我发现了问题。是我在itemPricePeer
类中重写了方法do select
public static function doSelect(Criteria $criteria, PropelPDO $con = null){
$critcopy = clone $criteria;
$critcopy->add(self::ACTIVE, 1);
return self::populateObjects(itemPeer::doSelectStmt($critcopy, $con));
}
我在populateObjects
变元中用itemPeer
切换了self/itemPricePeer
。愚蠢的我://谢谢你的回复。