简体   繁体   English

symfony 学说协会

[英]symfony doctrine associations

Hi so I managed to get this working for a relationship between users and a Job嗨,所以我设法让这个工作用于用户和工作之间的关系

I followed exactly the same procedure, checked the documentation and I am at a loss我遵循完全相同的程序,检查了文档,我不知所措

The relationship i'm trying to set up is Job to work我试图建立的关系是工作

1 Job with many work items assigned to it 1 个分配有许多工作项的作业

../Entity/Job.php ../实体/Job.php

 /**
 * @ORM\OneToMany(targetEntity="Work",mappedBy="workJob")
 *
 */
protected $jobToWork;
/**
 * Add jobToWork
 *
 * @param \Laelaps\InvoiceBundle\Entity\Work $jobToWork
 * @return Job
 */
public function addJobToWork(\Laelaps\InvoiceBundle\Entity\Work $jobToWork)
{

    $this->jobToWork[] = $jobToWork;

    return $this;
}
/**
 * Remove jobToWork
 *
 * @param \Laelaps\InvoiceBundle\Entity\Work $jobToWork
 */
public function removeJobToWork(\Laelaps\InvoiceBundle\Entity\Work $jobToWork)
{
    $this->jobToWork->removeElement($jobToWork);
}

/**
 * Get jobToWork
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getJobToWork()
{
    return $this->jobToWork;
}

../Entity/Work.php ../实体/Work.php

/**
 * @ORM\ManyToOne(targetEntity="Job",inversedBy="jobToWork")
 * @ORM\JoinColumn(name="work_job", referencedColumnName="id")
 */
protected $workJob;

/**
 * Set workJob
 *
 * @param integer $workJob
 * @return Work
 */
public function setWorkJob($workJob)
{
    $workJob->addJobToWork($this);
    $this->workJob = $workJob;

    return $this;
}

/**
 * Get workJob
 *
 * @return integer
 */
public function getWorkJob()
{
    return $this->workJob;
}

I did a app/console doctrine:generate:crud我做了一个应用程序/控制台学说:generate:crud

Which I know worked for my user to job relationship which was set up exactly the same way.我知道我的用户与工作关系的设置方式完全相同。

the error I get when I try to go to create a new "work" item is Object of class ..\\Entity\\Job could not be converted to string当我尝试去创建一个新的“工作”项目时得到的错误是 Object of class ..\\Entity\\Job could not be convert to string

Any help, as always greatly appreciated任何帮助,一如既往地非常感谢

UPDATE更新

As defined in the answer如答案中所定义

public function __toString()
{
    return (string) $this->getJobName();
}

As I was using the FOSUserBundle and my user class extended the FOSUser class this meant that the __toString() function was defined in the FOSUser, hence there was no problem when creating assocations to user.当我使用 FOSUserBundle 并且我的用户类扩展了 FOSUser 类时,这意味着 __toString() 函数是在 FOSUser 中定义的,因此在为用户创建关联时没有问题。

You probably try to display your Job as a string (inside a select box or a list for example).您可能尝试将您的 Job 显示为字符串(例如在选择框或列表中)。 Just add a __toString() method to your Job entity like this :只需将 __toString() 方法添加到您的 Job 实体,如下所示:

public function __toString()
{
    return $this->name; // For example
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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