如何从实体获取财产

时间:2015-06-17 13:25:38

标签: php symfony

我有两个实体“状态”和“医生”。每位医生都可以在他的个人资料中添加状态。当医生想要添加状态时,由于选择字段,他可以为另一位医生添加状态。

这不符合逻辑,我希望每位医生都只添加他的身份,而不是其他医生。

我该如何解决?

这是状态表:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder

        ->add('medecin','entity',array('class'=>'DoctorBundle:medecin','property'=>'prenom','multiple'=>false))
        ->add('text')
        ->add('description')
        ->add('image',new ImageType())
    ;
}`

4 个答案:

答案 0 :(得分:1)

从Form中删除字段Doctor(medecin),然后在处理表单响应时在控制器中删除

    if ('POST' === $request->getMethod()) {
        $statusForm->handleRequest($request);
        if ($statusForm->isValid()) {
            $status = $statusForm->getData();

            $status->setDoctor($this->getUser());

            $statusManager->flush($status);
        }
    }

$ this-> getUser()如果您已经与医生一起登录,如果您不是,请告诉他,无论您是在做什么。

答案 1 :(得分:0)

好的,根据您的评论,我认为您有几种解决方法:

  1. 完全删除$status = ....; // Your entity $currentDoctor = ... // logic for getting myself :) $status->setMedecin($currentDoctor); $form = $this->createForm(StatusType(), $status); $form->handleRequest($request); if ( $form->isValid() ){ // The rest is the same } 表单字段,稍后再分配。例如:

    data
  2. 绑定medecin表单字段的currentDoctor属性。当您需要将自己(Root -CountryA -RegionA -producerA -RegionB -ProducerB -ProducerC -RegionC -ProducerD -CountryB . . Several countries with the same pattern )传递给表单类型时,这会使事情变得更加复杂。我肯定会选择上面的#1。

  3. 希望这会有所帮助......

答案 2 :(得分:0)

我在创建表单的状态控制器中有这段代码:

    private void ReadByteArrayFromFile(string Chosen_File)
    {

        byte[] bytesFromFile = File.ReadAllBytes(Chosen_File);
        try
        {
            BinaryFormatter bf = new BinaryFormatter();
            MemoryStream ms = new MemoryStream(bytesFromFile);
            MyCustomStrokes customStrokes = bf.Deserialize(ms) as MyCustomStrokes;
            for(int i = 0; i < customStrokes.StrokeCollection.Length; i++)
            {
                if(customStrokes.StrokeCollection[i] != null)
                {
                    StylusPointCollection stylusCollection = new
                      StylusPointCollection(customStrokes.StrokeCollection[i]);
                    Stroke stroke = new Stroke(stylusCollection);
                    StrokeCollection strokes = new StrokeCollection();
                    strokes.Add(stroke);
                    this.MyInkPresenter.Strokes.Add(strokes);
                }
            }

        }
        catch (Exception ex)
        {
            System.Windows.MessageBox.Show(ex.Message);
        }
    }

    private void DecryptByteArray(byte[] encryptedArray)
    {
    }


}
[Serializable]
public sealed class MyCustomStrokes
{
    public MyCustomStrokes() { }
    /// <SUMMARY>
    /// The first index is for the stroke no.
    /// The second index is for the keep the 2D point of the Stroke.
    /// </SUMMARY>
    public Point[][] StrokeCollection;
}

`

答案 3 :(得分:0)

我尝试使用选择字段可以使用此代码,但是Doctor-id取值为null,这是代码创建表单:$id= $entity->setMedecin(); $form = $this->createForm(new statutType(), $entity, array( 'action' => $this->generateUrl('statut_create'), 'method' => 'POST', ));