Hi there I have a little relationship. For example
class Invoice extends \Phalcon\Mvc\Model {
const COL_ID = 'id';
const COL_DATE = 'date';
const REL_ITEMS = 'relItems';
private $id;
private $date;
public function initialize() {
$this->setSource('invoices');
$this->hasMany(
self::COL_ID,
Item::class,
Item::COL_INVOICE_ID,
[
'alias' => self::REL_ITEMS,
'reusable' => true,
]
);
}
public function getItems(): Simple
{
return $this->{self::REL_ITEMS};
}
public function setItems(array $items = []) : self
{
$this->{self::REL_ITEMS} = $items;
return $this;
}
// setters getters
}
class Item extends \Phalcon\Mvc\Model {
const COL_ID = 'id';
const COL_NAME = 'name';
const COL_PRICE = 'price';
const COL_INVOICE_ID = 'invoiceId';
const REL_INVOICE = 'relInvoice';
private $id;
private $name;
private $price;
private $invoiceId;
public function initialize() {
$this->setSource('invoices_items');
$this->belongsTo(
self::COL_INVOICE_ID,
Invoice::class,
Invoice::COL_ID,
[
'alias' => self::REL_INVOICE,
'foreignKey' => [
'allowNulls' => false,
'message' => 'Hey! where is my invoice?',
],
'reusable' => true,
]
);
}
public function getInvoice(): ?Invoice
{
return $this->{self::REL_INVOICE} ?: null;
}
public function setInvoice(Invoice $invoice) : self
{
$this->{self::REL_INVOICE} = $invoice;
return $this;
}
// setters getters
}
$invoice = Invoice::findFirst(1234);
var_dump(spl_object_hash($invoice)); // ie: #000000003cc56d770000000007fa48c5
$item = $invoice->getItems()->getFirst();
var_dump(spl_object_hash($item->getInvoice())); // ie#000000003ccfffff0000000007fa48c5
Why I get a new instance of the same invoice?
Rgds