Symfony 1.3:对此代码有何看法?可能更短还是更好?

时间:2010-05-07 13:12:11

标签: symfony1

我需要您对以下代码的看法。

我有一个消息列表:每条消息都有一个更改状态的链接 消息(读 - 非读)。

在部分“_message”中我有这个:

<div class="switching_link" id="switching_link_<?php echo $message ?>">

         echo include_partial('link_switch_state', array('message' =>
$message))

</div>

在部分“_link_switch_state”中我有这个:

if((int)$message->getState() == 1) {

            $string_state_message="non read";

} else {

            $string_state_message="read";

}

echo link_to_remote('Mark as '.$string_state_message, array(

                    'url' => 'message/switchState?id='.$message->getId(),

                     'update' => 'switching_link_'.$message,

                     "complete" => "switchClassMessage('$message');",

));

在message / actions / actions.class.php中我有这个:

public function executeSwitchState(sfWebRequest $request) {

         // searching the message we want to change its state.
         $this->messages =
Doctrine::getTable('Message')->findById($request->getParameter('id'));

         // changing the state of the message.
         if($this->messages[0]->getState() == 1) {

             $this->messages[0]->setState(0);
         }
         else {

             $this->messages[0]->setState(1);
         }

         $this->messages[0]->save();

         // rendering the partial that shows the link ("Mark as read/non
read").
         return $this->renderPartial('mensaje/link_switch_state', array(
'message' => $this->messages[0]));

     }

1 个答案:

答案 0 :(得分:0)

嗯,不确定你是否可以在Symonfy中写得更短,但你可以摆脱if-else语句(假设state可以是0或{{1} }):

1

_message

$string_state_message = ($message->getState()) ? "non read" : "read"; 0评估为"0",其他任何非空字符串都为false

true

message/actions/actions.class.php

这再次归功于$this->messages[0]->setState(!$this->messages[0]->getState()); 0 == false