fetch related data from the model

hello. i have two tables:

invoices: invoices

invoicesEvents: invoicesEvents

also set relations:

    // models/invoices.php
    public function initialize()
    {
        // Определяет n-1 отношения
        $this->belongsTo("userId", "users", "id");

        // Определяет 1-n отношения
        $this->hasMany("id", "invoicesEvents", "invoiceId");
    }

and

    // models/invoicesEvents.php
        public function initialize()
        {
            $this->setSource('invoicesEvents');

            // Определяет n-1 отношения
            $this->belongsTo("invoiceId", "invoices", "id");

        }

the following code

    // controllers/ReportsController.php
    $exportInvoices = invoices::find(array (
        "offerDate BETWEEN '{$cleanedPostData['startDate']}' AND '{$cleanedPostData['endDate']}'",
        'order' => 'offerDate ASC',
    ));
    $exportInvoicesEvents = $exportInvoices->getInvoicesEvents(['order' => 'id ASC']); //<----- ERROR LINE
    var_dump($exportInvoicesEvents->toarray());

, which would seem to work, an error:

Fatal error: Call to undefined method Phalcon\Mvc\Model\Resultset\Simple::getInvoicesEvents() in _\app\controllers\ReportsController.php on line 325

Fatal error: Cannot use the memory manager when the request is shutting down in Unknown on line 0

different tried - does not want to run fetch. tell me, what are the options?

What version are you using?



4.2k

i try: $exportInvoices->invoicesEvents();

but this results in the same error



4.2k

What version are you using? phalcon 1.3.4



4.2k

when I replace find on findFirst function works, but then get a recording from invoicesEvents only one entry of invoices

Hi,

in $exportInvoices variable, you have Phalcon\Mvc\Model\Resultset\Simple not Phalcon\Mvc\Model. That mean, its "array of models" not one model.

Try:

$exportInvoices = invoices::findFirst(array (
        "offerDate BETWEEN '{$cleanedPostData['startDate']}' AND '{$cleanedPostData['endDate']}'",
        'order' => 'offerDate ASC',
    ));
    $exportInvoicesEvents = $exportInvoices->getInvoicesEvents(['order' => 'id ASC']); //<----- ERROR LINE
    var_dump($exportInvoicesEvents->toarray());

or something like this (iam dont check syntax, take it like pseudocode):

$exportInvoices = invoices::find(array (
        "offerDate BETWEEN '{$cleanedPostData['startDate']}' AND '{$cleanedPostData['endDate']}'",
        'order' => 'offerDate ASC',
    ));
    $exportInvoicesEvents = array();
    foreach($exportInvoices as $exportInvoice) {
        $exportInvoiceEvents = $exportInvoice->getInvoicesEvents(['order' => 'id ASC']);
        $exportInvoicesEvents[] = $exportInvoiceEvents->toarray();
    }
    var_dump($exportInvoicesEvents);


4.2k

Hi,

in $exportInvoices variable, you have Phalcon\Mvc\Model\Resultset\Simple not Phalcon\Mvc\Model. That mean, its "array of models" not one model.

Try:

```php $exportInvoices = invoices::findFirst(array ( "offerDate BETWEEN '{$cleanedPostData['startDate']}' AND '{$cleanedPostData['endDate']}'", 'order' => 'offerDate ASC', )); $exportInvoicesEvents = $exportInvoices->getInvoicesEvents(['order' => 'id ASC']); //<----- ERROR LINE var_dump($exportInvoicesEvents->toarray()); ```

or something like this (iam dont check syntax, take it like pseudocode):

```php $exportInvoices = invoices::find(array ( "offerDate BETWEEN '{$cleanedPostData['startDate']}' AND '{$cleanedPostData['endDate']}'", 'order' => 'offerDate ASC', )); $exportInvoicesEvents = array(); foreach($exportInvoices as $exportInvoice) { $exportInvoiceEvents = $exportInvoice->getInvoicesEvents(['order' => 'id ASC']); $exportInvoicesEvents[] = $exportInvoiceEvents->toarray(); } var_dump($exportInvoicesEvents); ```

Thank you. I understand your option to bypass an array $exportInvoices.

But this is what will be the load on the server when they create a separate queries for each $ exportInvoice, if, for example, 100 or 1,000 units?

Or is it all combined into one query to the ORM?

For optimized request to database is better use phql and create custom sql query: http://docs.phalconphp.com/en/latest/reference/phql.html

Or just create pure sql query for get data in format you need. Using phalcon models is good practice, but not for all cases.



4.2k

clear. and I was going to do. I wanted to solve a trivial task using ORM ..