symfony教义协会 [英] symfony doctrine associations

查看:82
本文介绍了symfony教义协会的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我设法让这个工作与用户和工作之间的关系



我遵循完全相同的过程,检查文档,我失去了



我正在设置的关系是工作工作



1分配了许多工作项的作业

../ Entity / Job.php

  ** 
* @ ORM\OneToMany(targetEntity =Work,mappedBy =workJob)
*
* /
protected $ jobToWork;
/ **
*添加jobToWork
*
* @param \Laelaps\InvoiceBundle\Entity\Work $ jobToWork
* @return作业
* /
public function addJobToWork(\Laelaps\InvoiceBundle\Entity\Work $ jobToWork)
{

$ this-> jobToWork [] = $ jobToWork;

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

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

../ Entity / Work.php

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

/ **
*设置workJob
*
* @param integer $ workJob
* @return工作
* /
public function setWorkJob($ workJob)
{
$ workJob-> addJobToWork($ this);
$ this-> workJob = $ workJob;

return $ this;
}

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

我做了一个应用程序/控制台原则:generate:crud



我知道为我的用户工作的工作关系设置完全一样。



当我尝试去创建一个新的工作项目时,我得到的错误是
类.. \Entity\Job的对象不能转换为字符串



任何帮助,一如既往的非常赞赏



更新 / p>

如答案中所定义

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

当我使用FOSUserBundle和我的用户类扩展FOSUser类时,这意味着__toString()函数在FOSUser中定义,因此在向用户创建关联时没有问题。

解决方案

您可能会尝试将作业显示为字符串(例如在选择框或列表中)。只需向Job实体添加一个__toString()方法,如下所示:

  public function __toString()
{
return $ this-> name; //例如
}


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

../Entity/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

/**
 * @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

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

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.

解决方案

You probably try to display your Job as a string (inside a select box or a list for example). Just add a __toString() method to your Job entity like this :

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

这篇关于symfony教义协会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆