【问题标题】:symfony propel : wrongly populated object with criteria addJoinsymfony 推进:错误填充的对象与标准 addJoin
【发布时间】:2012-08-30 15:17:14
【问题描述】:

这是我在这里的第一个问题,所以请耐心等待我:)

我偶然发现了填充对象的奇怪行为。

我开始将项目中使用的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
    (

...等等。

条件和查询对象之间可能存在一些我遗漏的关键区别。我在 Google、StackOverflow 上搜索过,谁知道在哪里,但我没有找到任何类似的解决方案。

这个guy/gal 有一个类似的问题,但我没有使用addSelectColumn 作为我的标准,所以这对我来说是另一个死胡同。

谁能指出我正确的方向?

【问题讨论】:

  • 什么?在 Propel 1.6 中构建查询的新方法比使用标准要容易得多。特别是当您编写 simple 查询时。顺便说一句,哪个版本的 Propel?
  • 我使用 symfony 1.4,并且已经将 propel 更新到 1.6。我不知道有什么更简单的方法,Jobeet 书中提到了这一方法,而且我还在学习,所以我使用我找到的方法。你能发布任何我应该使用的参考资料吗?
  • 关于 Propel,你应该看看documentation。但是你需要什么来转换这个查询?因为你读到ModelQuery 不好,标准更好?
  • 或多或少,是的,项目负责人告诉我要避免它。但我也想知道为什么它的工作方式不同。谢谢你的链接。
  • 好吧,我认为这是一种正常的行为,因为它们的处理方式不同。 find() 默认返回PropelObjectCollection(参见here),doSelect 返回一个PDOStatement(参见here),这似乎是一个结果数组。

标签: php symfony-1.4 criteria propel


【解决方案1】:

我发现了问题。那是我在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 参数中将self/itemPricePeeritemPeer 切换。愚蠢的我:-/无论如何,谢谢你的回复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多