Doctrine2エンティティを更新していくつかのフィールドを除外する必要がある状況です。ZF2 Doctrine2の更新からエンティティフィールドを除外
ZF2では、Zend \ Formと検証フィルタを使用して更新を処理するアクションがあります。特にDish
エンティティは、と呼ばれ、が必要なブロブ列を持っています。です。更新中は、新しいファイルが提供されている場合にのみ、写真を置き換えたいと思います。
エンティティのソースコードと、料理を更新するコントローラアクションがあります。
料理\エンティティ\ Dish.php
<?php
namespace Dishes\Entity;
use Doctrine\ORM\Mapping as ORM;
/** @ORM\Entity **/
class Dish
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
**/
protected $id;
/**
* @ORM\Column(type="string")
*/
protected $name;
/**
* @ORM\Column(type="text")
*/
protected $description;
/**
* @ORM\Column(type="integer")
*/
protected $time;
/**
* @ORM\Column(type="integer")
*/
protected $complexity;
/**
* @ORM\Column(type="blob")
*/
protected $photo;
/**
* Magic getter to expose protected properties.
*
* @param string $property
* @return mixed
*/
public function __get($property)
{
return $this->$property;
}
/**
* Magic setter to save protected properties.
*
* @param string $property
* @param mixed $value
*/
public function __set($property, $value)
{
$this->$property = $value;
}
}
料理\コントローラ\ AdminController.php
public function editDishAction()
{
//Params from url
$id = (int) $this->params()->fromRoute('id', 0);
$objectManager = $this->objectManager;
$hydrator = new DoctrineObject($objectManager, false);
$form = new DishForm();
$existingDish = $objectManager->find('Dishes\Entity\Dish', $id);
if ($existingDish === NULL)
$this->notFoundAction();
$request = $this->getRequest();
if ($request->isPost())
{
$filter = new DishFilter();
$filter->get('photo')->setRequired(false);
$form->setHydrator($hydrator)
->setObject($existingDish)
->setInputFilter($filter);
$post = array_merge_recursive(
$request->getPost()->toArray(),
$request->getFiles()->toArray()
);
//Backup photo stream
$imageData = $existingDish->photo;
$form->setData($post);
if ($form->isValid())
{
//If user upload a new image read it.
if(!empty($existingDish->photo['tmp_name']))
$imageData = file_get_contents($existingDish->photo['tmp_name']);
$existingDish->photo = $imageData;
$objectManager->flush();
$this->redirect()->toRoute('zfcadmin/dishes');
}
}
else
{
$data = $hydrator->extract($existingDish);
unset($data['photo']);
$form->setData($data);
}
return new ViewModel(array('form' => $form));
}
は、実際に私はNULL
に$dish->photo
プロパティを設定するが、これは違反しますDB NOT NULLという制約があります。
実行時に特定のエンティティフィールドを更新から除外するようにDoctrineに指示するにはどうすればよいですか?
ありがとうございました。 私のコードを実際の解決策に編集します。特に、写真の検証を無効にすることはありません。 Zend \ファイルが提供されていない場合は、空の配列に写真のプロパティを設定します。したがって、新しい画像がない場合は、doctrineによって提供されたストリームをバックアップして復元します。 – DocLM