Virtual fields and entity functions are not read

Hi,
something very strange is happening.
I use virtual fields often. I am familiar with them.
However, there is a specific Entity in my code, where I cannot retrieve the virtual fields.
Does anyone know what could be happening?
Important detail, if I create some function in the entity I get the error:

“all to undefined method Cake\ORM\Entity::test()”

It’s like nothing I did in the entity had any effect.
Here’s the entity code:

	class Receita extends Entity
	{

        protected function teste(){
            return 'ok';
        }

       
	    protected function _getField(){
	    	return 'test';
	    }
	}

Below how I retrieve the fields and call the function

        $receitaTable = TableRegistry::getTableLocator()->get('Receita');
        $test = $receitaTable->get('5301');
        Log::debug($test->id); // 5301....ok!
        Log::debug($test->field);//print empty
        Log::debug($test->teste());//error
        

I’m using cakePHP 3.5.

Thanks a lot!

As you can see in Entities - 3.10 all virtual field functions have the nameing convention _get<CamelCasedProperty>

So in your example if you have a property teste your virtual field function should look like

	    protected function _getTeste(){
	    	return 'test';
	    }

The function protected function teste(){ doesn’t do anything related to virtual fields.

You get an error on your Log::debug($test->teste());//error line because you can’t call protected methods outside of the class itself.

If you don’t know what public, protected and private properties/functions are see PHP: Visibility - Manual

Hi @KevinPfeifer ,
Thanks for your help.
But I think it’s not the problem.
I set the function visibility to public, but the error still persists. And theoretically the visibility would not affect the virtual field as well, and I would be able to access it. The problem is that I can only access database attributes, virtual fields and entity functions are not accessible.

The strange thing is that it’s only for that specific “Receita” type. The other entity types work normally, as per the cakePHP documentation.

I’m using nameing convention _get<CamelCasedProperty>, and I know that functions are not virtual fields, but I can’t access both.

The code

	class Receita extends Entity
	{

        public function teste(){
            return 'ok';
        }

       
	    protected function _getField(){
	    	return 'test';
	    }
	}
        $receitaTable = TableRegistry::getTableLocator()->get('Receita');
        $teste = $receitaTable->get('5301');
        //Log::debug($teste);
        $this->out($teste->id); // ok! print 5301
        $this->out($teste->field); //error: print ""
        $this->out($teste->teste()); // [Error] Call to undefined method Cake\ORM\Entity::teste()

Thanks again!

Its because you’re not following the cakephp naming convention.

“all to undefined method Cake\ORM\Entity::test()”

The Cake\ORM\Entity class is the generic class for entities, its not using your Receita entity

Do you have the ReceitaTable class?
In that class you can call setEntityClass with you class

public function initialize(array $config) {
    parent::initialize($config);
    $this->setEntityClass(\App\Model\Entity\Receita::class);
    // ....
}

TTHHHAAANNKKSSS A LOT @raul338 !!! You save my day.

You solve my problem!
I had the ReceitaTable class, and I added the code below, and it worked like a charm !!!

class ReceitaTable extends Table
{
    public function initialize(array $config)
    {
        $this->setEntityClass(\App\Model\Entity\Receita::class);
.........

Thanks a lot too @KevinPfeifer for the clarification.