Alberto Ruffo Sun Aug 01 15:46:16 -0400 2010

Subject: Nested save()

Hello everybody,
i've written this:

$item = Product::find('first', array('conditions' => array('ID=?', array($importProduct->ID))));
if ($item) {

$item->code = $importProduct->Code;
$item->save();

$item->searchproduct->description = $importProduct->Description;
$item->searchproduct->save();
}

But, it throws this error:
Fatal error: Call to undefined method stdClass::save() (i.e. $item->searchproduct->save)

This is a 'one-to-one' relationship:
-Product has one SearchProduct
-SearchProduct belongs to Product

Thank you very much.

Regards,
Alberto


Alberto Ruffo Tue Aug 03 12:03:57 -0400 2010

up

Kien La Thu Aug 05 20:50:04 -0400 2010

Your error seems to indicate $item->searchproduct is an object and not a Model instance. Could you paste the results of print_r($item->searchproduct)? Also please show your has_one definition of searchproduct in the Product model.

Alberto Ruffo Fri Aug 06 10:18:52 -0400 2010

Yes,

 class Product extends ActiveRecord\Model {
    static $has_one = array(
     array('searchproduct', 'class_name' => 'SearchProduct')
    );
}

 class SearchProduct extends ActiveRecord\Model {
   static $belongs_to = array(
     array('product','class_name' => 'Product')
   );

}

PRINT_R AFTER $item->save(); prints nothing

$item->save();
 print_r($item->searchproduct);

PRINT_R AFTER $item->searchproduct->description = $importProduct->Description;
AND BEFORE $item->searchproduct->save();

$item->searchproduct->description = $importProduct->Description;
 print_r($item->searchproduct); 
$item->serchproduct->save();

prints:
stdClass Object
(

    [description] => SimpleXMLElement Object
        (
            [0] => PC 3.0Ghz.
        )
)

Then, there is this error message:
Undefined property: Product->serchproduct in /var/www/fedom3/lib/php-activerecord/lib/Model.php on line 524

Thank you

Regards,
Alberto

Jacques Fuentes Sun Aug 08 12:37:21 -0400 2010

You have a typo on your last save() here:
$item->serchproduct->save();

You are missing an 'a' and that's you are getting Undefined property exception.

I think it should be $item->searchproduct->save();

So the following should work

1 $item->searchproduct->description = 'something';
2 $item->searchproduct->save();

Alberto Ruffo Mon Aug 09 10:55:01 -0400 2010

Hello Jacques,
now i get this error:
Fatal error: Call to undefined method stdClass::save() in line:

$item->searchproduct->save();

Thank you very much.

Alberto

Jacques Fuentes Sat Aug 14 13:40:48 -0400 2010

It looks like this searchproduct you have is not an ActiveRecord model and is an XML object? You can't directly save that object to the DB, you would need to serialize it and store it in a particular value on a model. Also, your searchproduct object doesn't have a save() method and so you cannot invoke that method, nor would you be able to call $item->save() because it won't save your associated object even if it were an ActiveRecord model.

I think you may need to review PHP OOP and whatever libraries you are using.

(1-6/6)