Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NFR] ReusableInterface #155

Open
emiliodeg opened this issue Aug 28, 2018 · 4 comments
Open

[NFR] ReusableInterface #155

emiliodeg opened this issue Aug 28, 2018 · 4 comments
Assignees

Comments

@emiliodeg
Copy link

Context

Days ago I was working on a problem with the new instances returned by methods that call related elements in models in this publication

I have found a simple way to solve this problem by implementing a simple interface in the classes of models that we want to reuse globally in our applications

My Propose

  1. Change _reusable protected property to static in Model Manager _reusable property must remain as it is
  2. Create a new interface ReusableInterface
namespace Phalcon\Mvc\Model;

interface ReusableInterface {
    public function getUniqueKey();
}
  1. Modify how the model manager obtains the unique key of the models. Source
if reusable {
    if referencedModel instanceof ReusableInterface {
       let uniqueKey = referencedModel->getUniqueKey();
    } else {
       let uniqueKey = unique_key(referencedModel, arguments);
    }

    let records = this->getReusableRecords(referencedModel, uniqueKey);
    if typeof records == "array" || typeof records == "object" {
        return records;
    }
}

Usage

class Invoice extends \Phalcon\Mvc\Model implements \Phalcon\Mvc\Model\ReusableInterface {
    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 getUniqueKey() {
        return __CLASS__ . ':' . self::COL_ID . ':' . $this->getId();
    }

    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 implements \Phalcon\Mvc\Model\ReusableInterface {
    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 getUniqueKey() {
        return __CLASS__ . ':' . self::COL_ID . ':' . $this->getId();
    }

    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
}

// After
$invoice = (new Invoice())->findFirst(1234);
var_dump(spl_object_hash($invoice)); // 00000000546e8820000000005fdc6415

var_dump(spl_object_hash($invoice->getItems()->getFirst()->getInvoice())); // 00000000546efff0000000005fdc6415


// Before
$invoice = (new Invoice())->findFirst(1234);
var_dump(spl_object_hash($invoice)); // 00000000546e8820000000005fdc6415

var_dump(spl_object_hash($invoice->getItems()->getFirst()->getInvoice())); // 00000000546e8820000000005fdc6415

Well I hope your comments on the matter. I hope I have been explicit enough in my idea

Rgds

@phalcon phalcon deleted a comment from stale bot Nov 27, 2018
@niden
Copy link
Sponsor Member

niden commented Feb 23, 2019

Closing in favor of phalcon/cphalcon#13855. Will revisit if the community votes for it, or in later versions.

@niden niden closed this as completed Feb 23, 2019
@emiliodeg
Copy link
Author

@niden can you assign me this?

@niden niden reopened this May 16, 2019
@niden
Copy link
Sponsor Member

niden commented May 16, 2019

@emiliodeg all yours

@ruudboon
Copy link
Member

@emiliodeg I see this was never assigned to you. I will assign it now. Hopefully you have some time to look into this. Thnx

@phalcon phalcon deleted a comment from stale bot Dec 23, 2019
@niden niden transferred this issue from phalcon/cphalcon Oct 1, 2021
@stale stale bot added the stale Stale issue scheduled to be deleted label Apr 20, 2022
@phalcon phalcon deleted a comment from stale bot Apr 20, 2022
@stale stale bot removed the stale Stale issue scheduled to be deleted label Apr 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Backlog
Phalcon Roadmap
  
Backlog
Development

No branches or pull requests

3 participants