【问题标题】:Symfony, Doctrine update not working as expectedSymfony,Doctrine 更新未按预期工作
【发布时间】:2016-01-26 11:24:45
【问题描述】:

嘿,我有这个无法克服的有线错误。我正在使用 Symfony Framweork 和 Doctrine 进行数据库交互。我正在尝试开发一个简单的 CRUD API 来掌握一些概念。

实际的问题是,当我尝试更新数据库中的项目时,它仅在项目的 ID 在数据库中时才有效,否则我会收到此错误:

Error: Call to a member function setTitle() on a non-object 

看看我的存储库:

<?php

namespace BooksApi\BookBundle\Repositories;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Query\QueryException;

class UpdateBookRepository
{
    /**
     * @var EntityManager
     */
    public $em;

    /**
     * @param EntityManager $entityManager
     */
    public function __construct(
        EntityManager $entityManager
    ){
        $this->em = $entityManager;
    }

    public function updateBook($id, $update)
    {
        try {
            $book = $this->em->getRepository('BooksApiBookBundle:BooksEntity')
                ->find($id);

            $book->setTitle($update);
            $this->em->flush();
        } catch (\Exception $em) {
            throw new QueryException('003', 502);
        }

        return $book;
    }
} 

还有我的工厂:

<?php

namespace BooksApi\BookBundle\Repositories;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Query\QueryException;

class UpdateBookRepository
{
    /**
     * @var EntityManager
     */
    public $em;

    /**
     * @param EntityManager $entityManager
     */
    public function __construct(
        EntityManager $entityManager
    ){
        $this->em = $entityManager;
    }

    public function updateBook($id, $update)
    {

        $book = $this->em->getRepository('BooksApiBookBundle:BooksEntity')
            ->find($id);

        if ($book)
        {
            try {
                $book->setTitle($update);
                $this->em->flush();
            } catch (\Exception $em) {
                throw new QueryException('003', 502);
            }

            return $book;
        } else {
            return false;
        }
    }
} 

工厂处理响应真或假,因此,如果我将尝试更新 ID 不在 DB 中的项目,工厂应以 false, 'Unable to Update Book' 响应,而不是我收到上述错误,知道为什么伙计们.. ?

【问题讨论】:

  • 显然,$book 对象为空。你检查过你的数据库中是否有那条记录吗?
  • 如果对象存在于数据库中,则更新对象,否则我会收到上述错误。但是,如果您查看我的工厂,我为它做了一个案例......所以看到错误消息,我希望看到 {flase, Unable to Update Book } 响应。
  • 原因是你没有检查find()返回的内容(如果书不存在则为null)。
  • 考虑在调用$book-&gt;setTitle($update);之前添加一些查找结果检查:$book instanceof Book,或is_object($book)
  • 你确定你的 request->get('id') 返回什么?

标签: php symfony doctrine


【解决方案1】:

@Tomazi,您可以通过在调用“setTitle”方法之前检查您的对象是否存在来避免错误。

public function updateBook($id, $update)
{
    $book = $this->em->getRepository('BooksApiBookBundle:BooksEntity')->find($id);

    if ($book) {
        $book->setTitle($update);
        $this->em->flush();

        return $book;
    }

    return null;
}

【讨论】:

  • 不幸的是,你的方法没有奏效,但它让我感到轻松,并稍微改变了你的建议,现在感谢你弯曲路径。
【解决方案2】:

在继续执行代码之前,请确保 $book 对象不为空。该错误表明 $book 为空/空。 另外,在使用 flush 之前persist您的对象。

$this->em->persist($book);
$this->em->flush();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-28
    • 2019-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-18
    • 2018-02-14
    • 2016-11-17
    相关资源
    最近更新 更多