2016-05-09 6 views
0

エンティティ定義の定数を関連するテキスト値に変換したいと思っています。 私は問題の原因に関係しないコードの大部分を削除しました。Symfony 3の実体定数から小枝のテキストへ

藤堂エンティティ

/** 
* Todo 
* 
* @ORM\Table(name="todo") 
* @ORM\Entity(repositoryClass="TodoBundle\Repository\TodoRepository") 
* @ORM\HasLifecycleCallbacks 
*/ 
class Todo 
{ 
    const PRIORITY_HIGH = 2; 
    const PRIORITY_NORMAL = 1; 
    const PRIORITY_LOW = 0; 

    /** 
    * @var int 
    * 
    * @Assert\NotBlank(groups={"new", "edit"}) 
    * @Assert\Choice(callback="getPriorities") 
    * 
    * @ORM\Column(name="priority", type="integer") 
    */ 
    private $priority; 

    /** 
    * Set priority 
    * 
    * @param integer $priority 
    * 
    * @return Todo 
    */ 
    public function setPriority($priority) 
    { 
     $this->priority = $priority; 

     return $this; 
    } 

    /** 
    * Get priority 
    * 
    * @return int 
    */ 
    public function getPriority() 
    { 
     return $this->priority; 
    } 

    /** 
    * Get all priorities 
    * 
    * @return int 
    */ 
    public static function getPriorities() 
    { 
     return array(
      'High'  => self::PRIORITY_HIGH, 
      'Normal' => self::PRIORITY_NORMAL, 
      'Low'  => self::PRIORITY_LOW 
     ); 
    } 
} 

小枝概要

{% for todo in todos %} 
    <tr> 
     <td>{{ todo.title }}</td> 
     <td>{{ todo.content }}</td> 
     <td>{{ todo.priority }}</td> *Change this to output High, Normal or Low according to its set priority* 
     <td>{{ todo.duedate|date('d/m/Y') }}</td> 
     <td> 
      <div class="btn-group" role="group"> 
       <a href="{{ path('todo.show', {'id': todo.id }) }}" class="btn btn-success">View</a> 
       <a href="{{ path('todo.edit', {'id': todo.id }) }}" class="btn btn-warning">Edit</a> 
       <a href="{{ path('todo.delete', {'id': todo.id }) }}" class="btn btn-danger">Delete</a> 
      </div> 
     </td> 
    </tr> 
{% endfor %} 

FormBuilderを使用してフォームを作成するときに、私は

->add('priority', ChoiceType::class, array(
    'choices' => Todo::getPriorities() 
)) 
コードのこの部分を呼び出すことができていますこの

<select id="new_priority" name="new[priority]" class="form-control"> 
    <option value="2">High</option> 
    <option value="1" selected="selected">Normal</option> 
    <option value="0">Low</option> 
</select> 

のように記入テキストと値dorpdownフィールドを作成するには、

私は後でスタジアムでの翻訳を実現するために有用であろう、これは別の方法で行うか、達成することが可能であると思います。

答えて

3

は一例では、つまり、逆の方法で連想配列を返す別の方法を使用して、それを試してみてください。そして、

public static function getPrioritiesForSelect() 
{ 
    return array(
     self::PRIORITY_HIGH  => 'High', 
     self::PRIORITY_NORMAL => 'Normal', 
     self::PRIORITY_LOW  => 'Low' 
    ); 
} 

、あなたは「高」、「ノーマル」のような文字列を翻訳することができるようになります、

解決策が気に入らない場合は、翻訳ID文字列に基づいて翻訳構造を使用している場合は、接頭辞と定数で構成される連結文字列を返す別の方法を再度作成できますそれから、それを翻訳してください。このような何か:私は実際に戻ってテキスト表現に変換することで、道differenと使用twigextensionsをやってしまった

string: 
    priority: 
     2: 'High' 
     1: 'Normal' 
     0: 'Low' 
+0

public static function getPrioritiesForSelect() { return array( 'High' => 'string.priority.' . self::PRIORITY_HIGH, 'Normal' => 'string.priority.' . self::PRIORITY_NORMAL, 'Low' => 'string.priority.' . self::PRIORITY_LOW ); } 

あなたの翻訳ファイルでは、あなたがこのようなものを持っています。しかし、あなたの作品はあまりにも簡単なimprementationです! –