Symfony1 Symfony 1.3:对该代码有何意见?可以更短或更好?

Symfony1 Symfony 1.3:对该代码有何意见?可以更短或更好?,symfony1,Symfony1,我需要你对下面这个代码的意见 我有一个消息列表:每条消息都有一个改变状态的链接 消息的名称(已读-未读) 在部分“_消息”中,我有以下内容: <div class="switching_link" id="switching_link_<?php echo $message ?>"> echo include_partial('link_switch_state', array('message' => $message)) </div&

我需要你对下面这个代码的意见

我有一个消息列表:每条消息都有一个改变状态的链接 消息的名称(已读-未读)

在部分“_消息”中,我有以下内容:

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

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

</div>
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');",

));
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]));

     }
在message/actions/actions.class.php中,我有以下内容:

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

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

</div>
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');",

));
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]));

     }

Mmmh不确定是否可以在Symonfy中将其写得更短,但可以去掉
if-else
语句(假设
状态可以是
0
1
):

\u消息中

$string_state_message = ($message->getState()) ? "non read" : "read";
0
“0”
计算为
false
,任何其他非空字符串计算为
true

消息/actions/actions.class.php
中:

$this->messages[0]->setState(!$this->messages[0]->getState());
这也是由于
0==false
1==true
这两个事实造成的