我想在控制器中测试我的动作以上传文件。 但是该文件需要一个" farm"这是一个父实体。
所以我在使用WebTestCase调用请求之前创建了包含所有子实体的服务器场。
private function addFarm()
{
$farm = new Farm();
$farm->setName('test farm');
$farm->setDescription('test');
$farm->setContactName('test');
$farm->setActive(1);
$farm->setPhone('123');
$farm->setFax('123');
$path = sys_get_temp_dir().DIRECTORY_SEPARATOR.'farmlogo.jpg';
$image = imagecreate(100, 100);
imagejpeg($image, $path);
$farm->setLogo($path);
$country = new Country($this->uniqueTestValue('country test'), $this->uniqueTestValue('test'));
$state = new State($this->uniqueTestValue('state test'), $this->uniqueTestValue('test'), $country);
$city = new City($this->uniqueTestValue('test city'), $state);
$this->emSave($country);
$this->emSave($state);
$this->emSave($city);
$coordinates = new Coordinates(10, 10);
$location = new Location($city, $coordinates);
$address = new Address(
$this->uniqueTestValue('test'),
$this->uniqueTestValue('test'),
$this->uniqueTestValue('zip'),
$location
);
$addressEntity = new AddressEntity(
$this->client->getContainer()->get('location'),
$address
);
$addressEntity->getLocation()->setCity($city);
$farm->setAddress($addressEntity);
$farmManagement = new FarmManagement();
$farmManagement->setFarm($farm->getId());
$farmManagement->setUser($this->getUser());
$this->emSave($farmManagement);
$this->em->flush();
return $farm;
}
在调用flush(我需要获取新农场的ID)后,我得到:
1) [mybundlepath]\Tests\Controller\FarmManageControllerTest::testUploadPhotoAction
Undefined index: 000000002acb549d0000000036bcbc2f
/var/www/grocery/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:2865
/var/www/grocery/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php:665
/var/www/grocery/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php:714
/var/www/grocery/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php:269
/var/www/grocery/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:952
/var/www/grocery/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:335
/var/www/grocery/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:389
/var/www/grocery/src/[mybundlepath]Tests/Controller/FarmManageControllerTest.php:119
[mybundlepath]/Tests/Controller/FarmManageControllerTest.php:164
/usr/share/php/PHPUnit/TextUI/Command.php:192
/usr/share/php/PHPUnit/TextUI/Command.php:130
我一整天都在苦苦思索,调试教条源代码。它以某种方式查找City实体代理spl_object_hash(),但它无法找到。
我做错了什么?我只想将实体存储在数据库中并获取ID。
emSave()方法只执行entityManager-> persist($ entity)