[SOLVED] Virtual property exposed in entity => recursive limit while processing

Hello,

I want to expose a virtual field “deletable” which is set to true when several conditions match.
One of this condition needs to query the position of the current entity.
Here is how I am trying to do this:

namespace App\Model\Entity;

use Cake\ORM\Entity;
use Cake\ORM\TableRegistry;
use Cake\Log\Log;

class Notice extends Entity

{

protected function _getDeletable() {

        $notices = TableRegistry::get('Notices');
        $query     = $notices->find()
            ->where(['lease_id' => $this->_properties['lease_id']])
            ->order([
                'name'          => 'DESC',
                'receipt_date2' => 'DESC'
            ]);
        $result = $query->first();
    }
    

    return $result;
}

protected $_virtual = ['deletable']; 

}

I got an error: 256 recursive limit. I believe this is due to the fact that the deletable virtual field is also exposed during my query …
I tried to use ->hiddenProperties([‘deletable’]) without success.
How can I hide the virtual property only while calculation?

Children should not know about the parent class. It’s not good practice to put TableRegistry staticcalls in an entity (which are deprecated) . Virtual fields should use existing properties (returned by the database) to return a calculated value. Like:

protected function _getName()
{
    return $this->_properties['first_name'] . ' ' . $this->_properties['last_name'];
}

I would rethink your design and place this method on a table class, modify the table to have a flag, or use another class that may receive as an argument the entity in question.

Hello xaphalanx,

Got it (and learned that it was deprecated).
I moved it in my Model and use a method ->isDeletable.

Thanx