Hi all,
I've got a model, Transaction, that has hasMany relationships with an Extra, and an Item model.
My controller is creating a list of new Extra models and Item models to be associated with a new Transaction model - essentially it's converting $_POST data into models.
In my controller I have this code:
if(isset($_POST['extra'])){
$extra = [];
foreach($this->request->getPost('extra') as $name=>$value){
$Extra = new \Pay\Model\Extra();
$Extra->name = $name;
$Extra->value = $value;
$extra[] = $Extra;
}
$Transaction->extra = $extra;
}
if(isset($_POST['items'])){
$items = [];
foreach($this->request->getPost('items') as $key=>$item){
$Item = new \Pay\Model\Item();
$Item->name = (isset($item['name'])) ? $item['name'] : '';
$Item->price = (isset($item['price'])) ? $item['price'] : '';
$Item->tax = (isset($item['tax'])) ? $item['tax'] : '';
$Item->count = (isset($item['count'])) ? $item['count'] : '';
$Item->speedcode = (isset($item['speedcode'])) ? $item['speedcode'] : NULL;
$Item->accountcode = (isset($item['accountcode'])) ? $item['accountcode'] : NULL;
$items[] = $Item;
}
$Transaction->items = $items;
}
As you can see, the process for populating $Transaction->extra and $Transaction->items is pretty much identical.
However, I have 2 problems: 1) When doing my validation (not with \Phalcon validation, but my own code), $Transaction->items is set, but calling $Transaction->countItems() returns 0. Therefore I can't loop through $Transaction->items to see if they're all valid. For all intents and purposes, $Transaction->items is essentially not set.
If I put calls to count() in:
echo count($items);
$Transaction->items = $items;
echo count($Transaction->items);
I get "20" output - "2" for the count of $items, and 0 for the count $Transaction->items.
What the hell is happening to $items?
2) While $Transaction->extra is exhibiting all the same problems, the models do get saved in the database properly. Why, when the Transaction->Extra relationship is exactly the same as the Transaction->Item relationship, does one get saved but not the other?
Also, I'm using v1.3.0