Symfony Doctrine 刷新实体。

3

我有一个叫做SourceElanceProfileImport的实体,我在动作中创建了这个实体并设置了一些数据,但是当我执行flush操作时出现了错误:

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

有一个问题,为什么我发现像这样的团队后,我就有代理对象?

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

我努力编写了一个id,但是没有实体Teams

$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;

当我在实体团队(Teams)中添加__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 中有一个 Teams 对象,但是当我刷新时仍然出现错误:

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

$teamId 需要实体 Team,而 $team 则需要一个 Id,这正常吗? - BENARD Patrick
可能是正常的,因为对于另一个实体有效。我只是为另一个实体添加了一个团队字段,并没有为实体团队创建inversedBy字段。 - shuba.ivan
我已经使用Symphony有一段时间了,我发现它不像PHP本身那样自由类型。在你刚刚进行的编辑中,当你搜索一个实际上是整数字段的字段时,我相信你不能让它成为一个字符串,即硬编码$ id。 - Glubus
但是当我执行这个代码时,'$id = '2';$team = $em->getRepository('ArtelProfileBundle:Teams')->findOneById($id);// $team entity Teams, not Proxies',它是正确的。 - shuba.ivan
2个回答

0

我解决了,但问题出在持久化实体和配置fos_elastic上。我仍然有代理团队实体,但在操作中。

        $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(团队代理对象),这是与实体团队之间的关系,而在 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: ~

我在我的数据库和Elastic数据库中有实体


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方法?可能它没有返回一个字符串。


函数 getCompanyByEmail 是自定义查询构建器。 - shuba.ivan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接