1
votes

I have this entity:

class Brand
{
    ...

    /**
     * @var Company
     * @ORM\ManyToOne(targetEntity="Company", inversedBy="brands")
     * @ORM\JoinColumn(name="companies_id", referencedColumnName="id", nullable=true)
     */
    protected $company;

    ...

    /**
     * Set company.
     *
     * @param Company $company
     *
     * @return Brands
     */
    public function setCompany(Company $company)
    {
        $this->company = $company;
        return $this;
    }
}

If I remove the Company type-hint from the method signature, I get this error:

Catchable Fatal Error: Argument 1 passed to AppBundle\Entity\Brand::setCompany() must be an instance of AppBundleEntity\Company, null given, called in /var/www/html/backend/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php on line 410 and defined

I set the property to nullable but I can't remove the object from the data, why? How this can be fixed?

2
This sounds like an issue with caching. The error is on the method you said you modified. If you're using something like Zend OPCache, try restarting/disabling it. Seems like the old class definition is used. Note: you can also do setCompany(Company $company=null) to preserve type safety and allow nulls. - Anonymous
@Anonymous that works, I just restarted my web server and clear the cache (I was cleared previously) and all is good, thanks, BTW, how do I know if I am using any kind of cache? - ReynierPM
I'll write a full answer is a minute. - Anonymous

2 Answers

4
votes

This sounds like an issue with caching. The error is on the method you said you modified. If you're using something like Zend OPCache, try restarting/disabling it. Seems like the old class definition is used.

Note: you can also do setCompany(Company $company=null) to preserve type safety and allow nulls (PHP 5.1+).


The easiest way to check what you have enabled is to check your phpinfo() output. If you're using OPCache, you'll see it in there. Here is the configuration for my dev environment (in php.ini):

[OPCACHE]
zend_extension="php_opcache.dll"
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=0
opcache.fast_shutdown=1
opcache.enable_cli=1
opcache.validate_timestamps=1

The last option is the important one - it re-reads any files that were updated. In production you would want to set it to zero (and opcache.revalidate_freq to zero) to reduce disk access. See more settings here.

While things like APC still work, the built-in Zend OPCache is the way to go on PHP 5.5+, so I recommend switching if you can.

0
votes

Just modify setCompany method as given below

public function setCompany(Company $company=null)
{
    $this->company = $company;
    return $this;
}