0

我的想法是这样的。我在这样的模型中创建了一个函数。

public function status($id = null)
{
$conditions = array('Transaction.catalogue_id' => $id);
$status = $this->find('first',array('conditions' => $conditions, 'fields' => array('Transaction.status'),'order' => array('Transaction.id' => 'desc'))
  );
return $status['Transaction']['status'];
}

我如何从这个循环中发送 $id 的值?

<table cellspacing="0" cellpadding="0" border="0" class="all">
      <thead>
      <tr>
          <th>No</th>
          <th>Title</th>
          <th>Author</th>
          <th>ISBN</th>
          <th>Location</th>
          <th>Availability</th>
      </tr>
      </thead>
      <?php $count = 1;?>
      <tbody>  
      <?php foreach ($Catalogue as $Catalogue): ?>
      <tr>
          <td><?php echo $count++; ?></td>
          <td><?php echo $this->Html->link($Catalogue['Catalogue']['title'],array('controller' => 'transactions', 'action' => 'view',$Catalogue['Catalogue']['id']),array('escape' => false, 'class' => 'ajax')); ?></td>
          <td><?php echo $Catalogue['Catalogue']['author']; ?></td>
          <td><?php echo $Catalogue['Catalogue']['isbn']; ?></td>
          <td><?php echo $Catalogue['Location']['rack']; ?></td>
          <td><?php echo $this->getstatus($Catalogue['Catalogue']['id'])</td>
      </tr>
      <?php endforeach; ?>
      </tbody>
      <?php unset($Catalogue); ?>
  </table>

我们可以做 $this->getstatus($Catalogue['Catalogue']['id']) 吗?

4

3 回答 3

0

You should be using your Model in CakePHP to define the relationship between Catalogue and Transaction. Although what you are attempting is possible, it's a lot more effort than it needs to be. One or two lines of code (providing you have key'd your tables correctly) and Cake will do the rest for you. Pretty neat, yes? Yes.

In your Catalogue.php Model file, define a hasOne relationship with Transaction. Use ORDER to get the latest record...

class Catalogue extends AppModel {
    public $name = 'Catalogue';

    public $hasOne = array(
       'Transaction' => array(
           'className'    => 'Transaction'
           'order'        => 'Transaction.id DESC'
       )
    );
}

And then in your view, instead of

<td><?php echo $Catalogue['Catalogue']['id'];?></td>

just do

<td><?php echo $Catalogue['Transaction']['status'];?></td>

You can read more about relationships in CakePHP in the manual here.

于 2013-01-07T01:34:13.327 回答
0

误读问题道歉:是的,您可以使用 requestAction $this->requestAction('controllerName/actionName/'.$parameter); 从视图页面访问功能(控制器内部);

所以在你的情况下:

 $this->requestAction('status/' .$Catalogue['Catalogue']['id']);

===========================================

我会留下我原来的(错误的)答案,以防它在某种程度上有所帮助:)

您需要在控制器中设置值,以便可以在视图页面上调用它。

 $this->set('status', $status);

然后在您的视图页面上,您可以使用 echo $status

虽然您的示例中不需要它,但您也可以使用 CakePHP 的 copmact 函数,允许您创建多个变量。$this->set(compact('var1','var2','var5')); 在以下视图中都将变得可用:

 echo $var1.$var2.$var3;
于 2013-01-07T01:04:59.863 回答
0

您可以将模型作为变量传递给视图,如下所示:

public function action() {
    ...
    $this->set('Catalogue', $this->Catalogue);
} 

然后在您看来,您可以执行以下操作:

<td><?php echo $Catalogue->getstatus($catalogue['Catalogue']['id'])?></td>

但是你应该在模型上这样做。试图获取整个项目列表及其状态。

于 2013-01-07T15:29:53.990 回答