【问题标题】:Symfony Doctrine Flush EntitySymfony Doctrine Flush 实体
【发布时间】:2015-10-06 16:07:25
【问题描述】:

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

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;

当我将 __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 

【问题讨论】:

  • $teamId 需要 Entity Team 而 $team 需要 Id 是否正常?
  • 也许是常态,因为对于另一个实体来说工作正常。我只是为另一个实体 Teams 添加字段,而不是为实体 Teams 创建 inversedBy 字段
  • 我使用 symphony 已经有一段时间了,我体验到它不像 php 本身那样无类型。在您刚刚对 $id 进行硬编码的编辑中,我敢肯定,当您搜索实际上是整数的字段时,您不能让它成为字符串。
  • 但是当我这样做时它是真的 '$id = '2';$team = $em->getRepository('ArtelProfileBundle:Teams')->findOneById($id);// $团队实体团队,而不是代理的

标签: php entity-framework symfony doctrine-orm


【解决方案1】:

我解决了,但问题在于持久实体和配置 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 Teams(代理对象 Teams) 和 teamId 这是与实体 Teams 的关系,在 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: ~

我的数据库和弹性数据库中有实体

【讨论】:

    【解决方案2】:

    首先,每次实体未完全填充时,您都会有代理。代理用于返回部分实体。 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(重新启动网络服务器)以强制缓存重新生成它。如果出现莫名其妙的错误,可能应该重新启动 APC。

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

    【讨论】:

    • function getCompanyByEmail tjis 是自定义查询生成器
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    • 2016-04-13
    • 2018-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-05
    相关资源
    最近更新 更多