up
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.
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
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();
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
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)
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