Php 在Symfony中持久化更新的对象

Php 在Symfony中持久化更新的对象,php,symfony,doctrine-orm,doctrine-odm,symfony-2.8,Php,Symfony,Doctrine Orm,Doctrine Odm,Symfony 2.8,我有一个symfony应用程序,试图使用setter更新数据库中的一个实体。但是,当我更新实体并调用$this->em->flush()时,实体不会持久化到数据库中 这是我的服务: <?php namespace AppBundle\Service; use AppBundle\Exceptions\UserNotFoundException; use Doctrine\ORM\EntityManager; use AppBundle\Entity\User; /** * Clas

我有一个symfony应用程序,试图使用setter更新数据库中的一个实体。但是,当我更新实体并调用
$this->em->flush()
时,实体不会持久化到数据库中

这是我的服务:

<?php

namespace AppBundle\Service;

use AppBundle\Exceptions\UserNotFoundException;
use Doctrine\ORM\EntityManager;
use AppBundle\Entity\User;

/**
 * Class MyService
 * @package AppBundle\Service
 */
class MyService extends BaseService {

    /**
     * @var EntityManager
     */
    protected $em;

    /**
     * @var User
     */
    protected $user;


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

    /**
     * See if a user email exists
     * @param string $email
     * @return bool
     */
    public function checkEmailExists($email){
        $this->user = $this->em
            ->getRepository('AppBundle:User')
            ->findOneBy(['email' => $email]);

        return !(is_null($this->user));
    }

    /**
     * add credit to a users account
     * @param User $user
     * @param integer $credit
     */
    public function addCredit(User $user, $credit){
        $user->addCredit($credit);

        $this->em->flush();
    }

    /**
     * add a credit to a users account
     * @param $email
     * @param $credit
     */
    public function addCreditByEmail($email, $credit){
        if(!($this->checkEmailExists($email))){
            throw new UserNotFoundException(sprintf('User with email %s not found.', $email));
        }
        $this->addCredit($this->user, $credit);
    }
}
我发现了这个问题

在测试用例中,我只需通过执行
$this->getEntityManager()->refresh($user)
(在原始$user实体上)来刷新实体。谢谢

我发现了这个问题


在测试用例中,我只需通过执行
$this->getEntityManager()->refresh($user)
(在原始$user实体上)来刷新实体。谢谢

您的PHPUnit测试是否针对真实数据库运行?这个问题是否也发生在“实际使用”中,或者可能是您在测试期间如何使用条令的一个方面?是的,它们是针对sqlite数据库@Darien运行的。我不这么认为。我首先要验证
$this->getUser()
应该始终为您提供与假定等效的
findOneBy(…)
调用相同的预先存在的用户实体。如果
$this->getUser()
正在创建一个新的,请确保在刷新之前正在执行
->persist()
调用。如果同一电子邮件中有多行,
findOneBy()
可能返回了“错误”的一行。您是否正在调用
$this->em->persist($this->user)$this->em->flush()
之前,我同意@Confidence。。。你必须持久化然后刷新。你的PHPUnit测试是针对真实的数据库运行的吗?这个问题是否也发生在“实际使用”中,或者可能是您在测试期间如何使用条令的一个方面?是的,它们是针对sqlite数据库@Darien运行的。我不这么认为。我首先要验证
$this->getUser()
应该始终为您提供与假定等效的
findOneBy(…)
调用相同的预先存在的用户实体。如果
$this->getUser()
正在创建一个新的,请确保在刷新之前正在执行
->persist()
调用。如果同一电子邮件中有多行,
findOneBy()
可能返回了“错误”的一行。您是否正在调用
$this->em->persist($this->user)$this->em->flush()
之前,我同意@Confidence。。。你必须坚持,然后冲水。
<?php

namespace AppBundle\Tests\Service;

use AppBundle\DataFixtures\ORM\PurchaseFixture;
use AppBundle\Entity\Vendor;
use AppBundle\Repository\OfferRepository;
use AppBundle\Tests\TestCase;
use AppBundle\Entity\Offer;
use AppBundle\DataFixtures\ORM\OfferFixture;
use AppBundle\DataFixtures\ORM\PaymentSystemFixture;

/**
 * Class UserOfferServiceTest
 * @package AppBundle\Tests\Service
 */
class MyServiceTest extends TestCase implements ServiceTestInterface
{


    function __construct($name = null, array $data = [], $dataName = '')
    {
        $this->setFixtures([
            'AppBundle\DataFixtures\ORM\CityFixture',
            'AppBundle\DataFixtures\ORM\CountryFixture',
            'AppBundle\DataFixtures\ORM\PaymentSystemFixture',
            'AppBundle\DAtaFixtures\ORM\UserFixture',

        ]);
        parent::__construct($name, $data, $dataName);
    }

    /**
     * test the checkEmailExists() of app.vendor
     */
    public function testCheckEmailExists(){
        $myService = $this->getService();


        $this->assertTrue($myService->checkEmailExists('user1@user1.com'));
        $this->assertFalse($myService->checkEmailExists($this->fake()->safeEmail));
    }

    /**
     * test the addCredit functionality
     */
    public function testAddCredit(){
        $myService = $this->getService();

        $user = $this->getUser();
        $this->assertEquals(0, $user->getCredit());

        $toAdd = $this->fake()->numberBetween(1, 500);
        $myService->addCredit($user, $toAdd);

        $this->assertEquals($toAdd, $user->getCredit());
    }

    /**
     * test the addCreditByEmail functionality
     */
    public function testAddCreditByEmail(){
        $myService = $this->getService();

        $user = $this->getUser();
        $email = $this->getUser()->getEmail();

        $this->assertEquals(0, $user->getCredit());

        $toAdd = $this->fake()->numberBetween(1, 500);
        $myService->addCreditByEmail($email, $toAdd);

        $updatedUser =  $this->getEntityManager()
            ->getRepository('AppBundle:User')
            ->findOneBy(['email' => $email]);

        $this->assertEquals($toAdd, $updatedUser->getCredit());
    }


    /**
     * @return \AppBundle\Service\VendorService|object
     */
    public function getService(){
        $this->seedDatabase();
        $this->client = $this->createClient();

        return $this->client->getContainer()->get('app.admin_kiosk');
    }


}