0

This is my first question here, so please try to be patient with me :)

I've stumbled upon a weird behavior populating an object.

I started to convert the objectQuery::create()-> ... ->find() methods used in my project to $c = new Criteria(), $c-> ... objectPeer::doSelect($c) since I've been told Queries shouldn't be used when criteria can be.

I have a function, that returns all the prices of items from the shop. Or at least did. The thing that I cannot figure out is this:

the old code:

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;
}

returns correctly populated PropelObjectCollection object, through which i can go with foreach, and get/set the itemPrice objects and attributes i need.

now, the new code:

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);
}

returns an array of itemPrice objects, but they are populated with item values related to itemPrice objects through join. that means : when I call print_r(self::DoSelect($c)); it prints

 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
    (

...and so on.

There is probably some crucial difference between criteria and query object, that I'm missing. I searched on Google, StackOverflow, and who knows where, but I didn't find anything resembling a solution to this.

This guy/gal had a vaguely similar problem, but I didn't use addSelectColumn with my criteria, so it's been another dead end for me.

Can anyone please point me in the right direction?

Kuro
  • 878
  • 9
  • 28
  • What ? The new way to build query in Propel 1.6 is really much easier than using criteria. Specially when you write *simple* query. Which version of Propel btw? – j0k Aug 30 '12 at 15:30
  • I use symfony 1.4, and have propel updated to 1.6. I didn't know about any easier way, Jobeet book speaks of this one, and I'm still learning, so I use what i find. could you post any references i should be working with? – Kuro Aug 30 '12 at 15:50
  • Regarding Propel, you should take a look at the [documentation](http://www.propelorm.org/reference/model-criteria.html). But what do you need to convert this query? Because you read that `ModelQuery` is bad and criteria is better ? – j0k Aug 30 '12 at 15:58
  • More or less, yes, I've been told to avoid it by head of the project. But I'd also like to know why it works differently. Thank you for the link btw. – Kuro Aug 30 '12 at 16:03
  • Well I think this a normal behavior because they aren't handle in the same way. `find()` returns `PropelObjectCollection` by default (see [here](http://api.propelorm.org/1.6.0/runtime/propel-runtime-query/ModelCriteria.html#methodfind)) and `doSelect` returns a `PDOStatement` (see [here](http://api.propelorm.org/1.5.0/runtime/propel-runtime-util/BasePeer.html#methoddoSelect)) which seems to be an array of result. – j0k Aug 30 '12 at 16:39
  • so how do you use join with criteria, if you need to filter ones based on value in related table? can it be even done? i changed it back to itemPriceQuery, and simplyfied it using leftJoin() from documentation, but i'm still curious. – Kuro Aug 30 '12 at 17:54
  • If you want to filter on a value in a related table, you should use `filterByXXX` and `useXXQuery`, take a look at [this answer](http://stackoverflow.com/a/9738970/569101). – j0k Aug 30 '12 at 21:14

1 Answers1

0

I found the problem. It was that I had overriden method do select in itemPricePeer class

public static function doSelect(Criteria $criteria, PropelPDO $con = null){

    $critcopy = clone $criteria;
    $critcopy->add(self::ACTIVE, 1);

    return self::populateObjects(itemPeer::doSelectStmt($critcopy, $con));

}

I switched self/itemPricePeer with itemPeer in populateObjects arguments. silly me :-/ Thanks for your responses anyway j0k.

j0k
  • 22,600
  • 28
  • 79
  • 90
Kuro
  • 878
  • 9
  • 28