We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Related records not retrieved

Hello!

I'm currently writing unit tests for my models, and I've ran into an issue where related records are no longer retrieved by the magical functionality from defining relations.

It has worked before, and I'm sure it is I who have done something to mess it up, but I have no idea as of how to troubleshoot. It is only two relations in my entire system that have stopped working.

Problem description Here's example code to show the problem. For the actual models, and how I've set it up, see below.

<?php

// Retrieve an UcCheck.
$ucCheck = UcCheck::findFirst();

// This DOES work.
$ucCheckIncomes = UcCheckIncome::find(
    array(
        "conditions" => "uc_check_id = :ucCheckId:",
        "bind" => array("ucCheckId" => $ucCheck->getId()
    )
);
var_dump(count($ucCheckIncomes)); // 2 (Correct - there are two related ones in the DB)

// This does NOT work:
$ucCheckIncomes = $ucCheck->getUcIncomes();
var_dump(count($ucCheckIncomes)); // 0 (..?)

Code for recreating the conditions Here is an example table of a credit check (it has been stripped of fields to make troubleshooting easier):

CREATE TABLE `uc_check` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `applicant_id` int(11) unsigned NOT NULL,
  `firstname` varchar(50) DEFAULT NULL,
  `lastname` varchar(50) DEFAULT NULL,
  `fullname` varchar(150) DEFAULT NULL,
  `address` varchar(50) DEFAULT NULL,
  `added_on` datetime NOT NULL,
  `deleted_on` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `applicant_id_2` (`applicant_id`,`deleted_on`),

  -- Ignore the Applicant for now - this relation works.
  -- KEY `applicant_id` (`applicant_id`),
  -- CONSTRAINT `uc_check_ibfk_1` FOREIGN KEY (`applicant_id`) REFERENCES `applicant` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8

And in the model for this table, aside from getters and setters, I have this:

<?php

namespace Models;

use \Phalcon\Mvc\Model;

class UcCheck extends Model {

    /**
     * Initialization.
     *
     * @return void
     */
    public function initialize() {
        parent::initialize();

        $this->belongsTo('applicant_id', '\Models\Applicant', 'id', array("alias" => "Applicant", "reusable" => true));
        $this->hasMany('id', '\Models\UcCheckIncome', 'uc_check_id', array("alias" => "UcIncomes", "reusable" => true));

        $this->addSoftDeleteBehavior();
    }

    /* GETTERS AND SETTERS FROM HERE */

}

The related records I'm trying to retrieve look like this (also stripped of some arbitrary values):

CREATE TABLE `uc_check_income` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `uc_check_id` int(11) unsigned NOT NULL,
  `year` int(8) NOT NULL,
  `final_income` int(8) NOT NULL,
  `added_on` datetime NOT NULL
  PRIMARY KEY (`id`),
  KEY `uc_check_id` (`uc_check_id`),
  CONSTRAINT `uc_check_income_ibfk_1` FOREIGN KEY (`uc_check_id`) REFERENCES `uc_check` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8
<?php

namespace Models;

use \Phalcon\Mvc\Model;

class UcCheckIncome extends Model {

    /**
     * Initialization.
     *
     * @return void
     */
    public function initialize() {
        parent::initialize();

        $this->belongsTo('uc_check_id', '\Models\UcCheck', 'id', array("alias" => "UcCheck", "reusable" => true));

        $this->addSoftDeleteBehavior();
    }

    /* GETTERS AND SETTERS FROM HERE */

}

Additional info I am using ModelMetaData, which I have been sure to delete and have re-generated before running the tests again.

The tests have worked before. I must have changed something, but what?

Thanks for your time.

// dimhoLt



22.6k

I've found the problem.

I had misunderstood what the "reusable" => true meant. I thought it would always make a request to the database, but cache the result. Apparently, it does not, but doesn't request it if they're not loaded with the object (I gotta read up on this). Removing all "reusable" => true from my relations solved the problems.