简体   繁体   中英

Can't add fields to my user class (Fos_UserBundle)

I am trying to modify my user class (FOS_UserBundle). I have a separate table with companies, and when a user is created, I want it tied to one of those companies. Eventually I will have to discover how to automatically create a user when creating an entry in the company table, but for now I am just trying to feed test data.

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
    * @var object BizTV\BackendBundle\Entity\company
    *  
    * @ORM\ManyToOne(targetEntity="BizTV\BackendBundle\Entity\company")
    * @ORM\JoinColumn(name="company", referencedColumnName="id")
    */
    protected $company; 

In my construct I tried to set

$this->company = 1;

But that wouldn't work since it wants an object, so I instantiated the company object and filled it with dummy data...

$dummy = new company;
$dummy->setActive(true);
$dummy->setCompanyName('myCompany');
$dummy->setId(1);

$this->company = $dummy;

Then I get an error message about a new entity being found...

A new entity was found through the relationship 'BizTV\\UserBundle\\Entity\\User#company' that was not configured to cascade persist operations for entity: BizTV\\BackendBundle\\Entity\\company@0000000018a5279000000000049c7b23. Explicitly persist the new entity or configure cascading persist operations on the relationship. If you cannot find out which entity causes the problem implement 'BizTV\\BackendBundle\\Entity\\company#__toString()' to get a clue.

Add the cascade persist option to the User::$company property annotation:

/**
 * @ORM\ManyToOne(targetEntity="BizTV\BackendBundle\Entity\company", cascade={"persist"})
 * @ORM\JoinColumn(name="company", referencedColumnName="id")
 */
protected $company;

You can find the related documentation here : http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-associations.html#transitive-persistence-cascade-operations

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM