Symfony Doctrine Flush Entity

时间:2015-10-06 16:07:25

标签: php entity-framework symfony doctrine-orm

我有实体SourceElanceProfileImport,我在操作中创建了这个实体并设置了一些数据,当我刷新时有错误:

Catchable Fatal Error: Object of class Proxies\__CG__\Artel\ProfileBundle\Entity\Teams could not be converted to string

并提出疑问,为什么我找到这样的团队我有Proxies对象

$team = $em->getRepository('ArtelProfileBundle:Teams')->findOneById($id);

我用硬编码写id并且没有实体团队

$id = '2';
$team = $em->getRepository('ArtelProfileBundle:Teams')->findOneById($id);
// $team entity Teams, not Proxies

这次行动发生了什么事?

动作:

   public function elanceProfileAction(Request $request)
{
    $em = $this->getDoctrine()->getManager();
    $url = $request->get('url');
    $email = $request->get('email');
    $firstName = $request->get('firstName');

    $user = $em->getRepository('ArtelProfileBundle:Users')->getCompanyByEmail($request->get('email'));
    $id = $user[0]->getTeams()->getId();
    $team = $em->getRepository('ArtelProfileBundle:Teams')->findOneById($id);
    if (!empty($user)) {
        $elance_import = new SourceElanceProfileImport();

        $hepler = $this->container->get('artel.profile.additional_function');
        $pass = $hepler->generatePassword();
        $elance_import
            ->setEmail($email)
            ->setSecurityHash(sha1($pass))
            ->setElanceUrl($url)
            ->setTeamId($team)
            ;
        $em->persist($elance_import);
        $em->flush();

和实体:

class SourceElanceProfileImport
{
/**
 * @var \Teams
 *
 * @ORM\ManyToOne(targetEntity="Teams")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="team_id", referencedColumnName="id", onDelete="CASCADE")
 * })
 */
private $teamId;

/**
 * @var integer
 *
 * @ORM\Column(name="team_id", type="integer")
 */
public $team;

当我将__toString添加到我拥有的实体团队时:

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

Error in one or more bulk request actions:

index: /aog/sourceelanceprofileimport/5 caused MapperParsingException[failed to parse [team_id]]; nested: NumberFormatException[For input string: "Gutkowski LLC"]; 

为什么在另一个实体工作正常,我不知道什么是错的((

更新 我解决了,但我认为这解决得不好,这是我不删除这个问题 我添加了用户实体:

    private function _load()
{
    // lazy loading code
}

/**
 * Get teams
 *
 * @return \Artel\ProfileBundle\Entity\Teams
 */
public function getLoadTeams()
{
    $this->_load();
    return parent::getId();
}

并在我的行动中

        $user = $em->getRepository('ArtelProfileBundle:Users')->getCompanyByEmail($request->get('email'));
    $id = $user[0]->getLoadTeams();
    $team = $em->getRepository('ArtelProfileBundle:Teams')->findOneById($id);

比我的对象团队变量$ team,当我冲洗时仍然有错误:

Catchable Fatal Error: Object of class Artel\ProfileBundle\Entity\Teams could not be converted to string 

2 个答案:

答案 0 :(得分:0)

我解决了,但问题出现在持久化实体和配置fos_elastic中 我仍然有Proxies团队实体但在行动中

        $user = $em->getRepository('ArtelProfileBundle:Users')->getCompanyByEmail($request->get('email'));
    $id = $user[0]->getTeams()->getId();
    $team = $em->getRepository('ArtelProfileBundle:Teams')->findOneById($id);

    if (!empty($user)) {
        $elance_import = new SourceElanceProfileImport();

        $hepler = $this->container->get('artel.profile.additional_function');
        $pass = $hepler->generatePassword();
        $elance_import
            ->setEmail($email)
            ->setSecurityHash(sha1($pass))
            ->setElanceUrl($url);
        $em->persist($elance_import);
        $elance_import->setTeamId($team);

        $em->flush();

并刷新精细实体创建并拥有id团队(Proxies对象团队) 和teamId这是与实体团队的关系,在fos_elastica需要这样写:

                sourceelanceprofileimport:
                mappings:
                    id:
                      type: integer
                    elance_url:
                      type: string
                    full_name:
                      type: string
                    status:
                      type: string
                    created:
                      type: date
                    approved_at:
                      type: date
                    imported_at:
                      type: date
                    team_id:
                        type: "nested"
                        properties:
                            id: ~
                persistence:
                    # the driver can be orm, mongodb or propel
                    # listener and finder are not supported by
                    # propel and should be removed
                    driver: orm
                    model: Artel\ProfileBundle\Entity\SourceElanceProfileImport
                    provider: ~
                    listener: ~
                    finder: ~

我的DB和Elastic DB中有实体

答案 1 :(得分:0)

首先,每次实体未完全填充时,您将拥有代理。代理用于返回部分实体。 http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/partial-objects.html

查看代码我注意到了这一点:

$team = $em->getRepository('ArtelProfileBundle:Teams')->findOneById($id);

但你可以用这种方式做同样的事情:

$team = $em->find('ArtelProfileBundle:Teams', $id); // EntityManager::find has 2 arguments: entity and id.

在下面你这样做:

$user = $em->getRepository('ArtelProfileBundle:Users')->getCompanyByEmail($request->get('email'));

您是否在getCompanyByEmail上实现了一些自定义代码?如果没有,您可以使用Doctrine的方法:

$user = $em->getRepository('ArtelProfileBundle:Users')->findOneByEmail($request->get('email'));

这些是神奇的方法:findOneBy或findBy。一个返回单个对象或NULL,另一个返回带有结果的数组。 你也可以这样做:

$user = $em->getRepository('ArtelProfileBundle:Users')->findOneBy(['email' => $request->get('email')]); // you can add more key/value filters on the array

现在只是为了确定,你是否将APC实现为字节码缓存?因为当我对我的实体进行更改时,我通常需要刷新APC(重新启动Web服务器)以强制缓存重新生成它。如果发生了大量错误,可能应重新启动APC。

现在,关于刷新错误,你是否在任何实体上实现了__toString方法?可能它没有返回一个字符串。