Search code examples
phpsymfony-1.4criteriapropel

symfony propel : wrongly populated object with criteria addJoin


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?


Solution

  • 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.