0

まず第一に、私を助けようとして読んでくれてありがとう。私はsymfonyが初めてです。

プロパティ FechaAlta (SingUpDate) を持つエンティティがあります。ユーザーのサインアップ日を保存したい

/**
 * @var date
 *
 * @ORM\Column(name="fechaAlta", type="datetime")
 */
private $fechaAlta;

/**
 * Set fechaAlta
 *
 * @return Promotor
 */
public function setFechaAlta()
{
    $this->fechaAlta = new \DateTime('now');

    return $this;
}

フォームに隠しフィールドを作成せずにこの日付を保存する最善の方法を知りたいです。

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('nombre')
        #->add('slug')
        ->add('fechaAlta')
    ;
}

フォームフィールド「fechaAlta」を削除しようとしましたが、次のエラーが発生し続けます

An exception occurred while executing 'INSERT INTO Promotor (nombre, slug, fechaAlta) VALUES (?, ?, ?)' with params {"1":"Prueba","2":"prueba","3":null}:

SQLSTATE [23000]: 整合性制約違反: 1048 列 'fechaAlta' を null にすることはできません

newAction() で、$promotor->setFechaAlta(); を呼び出します。現在の日付を保存する必要があります。

public function newAction()
{
    $promotor = new Promotor();

    $promotor->setFechaAlta();

    $form   = $this->createForm(new PromotorType(), $promotor);

    return $this->render('PromotorBundle:Promotor:new.html.twig', array(
        'entity' => $promotor,
        'form'   => $form->createView(),
    ));
}

どうもありがとう

4

2 に答える 2

0

デフォルトの symfony パターンは、newAction が createAction に投稿するフォームを作成することです。

createAction は、日付を設定する場所です。

public function createAction( Request $request, $id ) {
    $promotor = new Promotor();
    $promotor->setFechaAlta();

    $form   = $this->createForm(new PromotorType(), $promotor);
    $form->bind( $request );

    if ( $form->isValid() ) {
        $this->getDoctrine()->getManager()->persist( $promotor );
        $this->getDoctrine()->getManager()->flush();

    return $this->redirect( 
        $this->generateUrl( 'promotor_show', array( 'id' => $promotor->getId() ) ) 
        );
    }

    return $this->render( 'PromotorBundle:Promotor:new.html.twig'
        , array(
        )
    );
}
于 2013-04-22T17:57:12.080 に答える