CakePHP 3.0 在 index.ctp 而不是 id 上获取值

CakePHP 3.0 get values on index.ctp instead id

我有两个表,'applications' 和 'statuses',其中有关系 'applications' BelongsTo 'statuses'。 让我们从模型开始。

这是 ApplicationsTable 的样子:

public function initialize(array $config)
{
    $this->table('applications');
    $this->displayField('title');
    $this->primaryKey('id');
    $this->addBehavior('Timestamp');
    $this->belongsTo('Users', [
        'foreignKey' => 'user_id',
        'joinType' => 'INNER'
    ]);

    $this->hasOne('Statuses', 
        'foreignKey' => 'status_id',
        'joinType' => 'INNER'
    ]);

    $this->belongsToMany('Statuses', [
        'foreignKey' => 'application_id',
        'targetForeignKey' => 'status_id',
        'joinTable' => 'applications_statuses'
    ]);
}

StatusesTable 看起来像:

public function initialize(array $config)
{
    $this->table('statuses');
    $this->displayField('title');
    $this->primaryKey('id');
    $this->addBehavior('Timestamp');
    $this->hasMany('Applications', [
        'className' => 'Applications',
        'foreignKey' => 'status_id'
    ]);
}

索引的 ApplicationsController 看起来像这样:

 public function index()
{
    $this->paginate = [
        'contain' => 'Users', 'Statuses'
    ];
    $this->set('applications', $this->paginate($this->Applications));
    $this->set('_serialize', ['applications', 'statuses']);
}

我想显示状态的标题而不是 index.ctp

上的 ID

来自 Applications 的 index.ctp 看起来像:

<?php foreach ($applications as $application): ?>
    <tr>
        <td>
            <?= $application->has('user') ? $this->Html->link($application->user->name, ['controller' => 'Users', 'action' => 'view', $application->user->id]) : '' ?>
        </td>
        <td><?= $this->Html->link($application->title, ['action' => 'view', $application->id]) ?></td>
        <td><?= h($application->companyname) ?></td>
        <td><?= h($application->city) ?>, <?= h($application->country) ?></td>
        <td><?= h($application->status_id) ?></td> // **I want to get the titles of the status instead the id.**
        <td><?= h($application->modified) ?></td>
        <td class="actions">
            <?= $this->Html->link(__('Edit'), ['action' => 'edit', $application->id]) ?>
            <?= $this->Form->postLink(__('Delete'), ['action' => 'delete', $application->id], ['confirm' => __('Are you sure you want to delete # {0}?', $application->id)]) ?>
        </td>
    </tr>

但我总是得到 id 作为输出。我尝试了几种方法:

1. <?= h($application->status_title) ?> // It doesn't work but it works for user

2. 'contain' => ['Users', 'Statuses'] // Change in ApplicationsController
<?= $application->has('status') ? $this->Html->link($application->status->title, ['controller' => 'Statuses', 'action' => 'view', $application->status->id]) : '' ?>

3. <td><?= h($application->status->title); ?></td>
<?= h($application->status->title) ?>

PS:我建议在您的状态 object 上加上 __toString(),即 returns 标题。然后你可以做

<?= h($application->status) ?>

如果你有:

function __toString(){
   return $this->title;
}
  • 首先确保您的模型已配置。

    // Remove this part from your model
    $this->hasOne('Statuses', 
        'foreignKey' => 'status_id',
        'joinType' => 'INNER'
    ]);
    
    // Replace it with
    $this->belongsTo('Statuses', [
        'foreignKey' => 'status_id',
        'joinType' => 'INNER'
    ]);
    
  • 在视图中,您应该能够像这样加载数据:

    <td><?= h($application->status->title); ?></td>
    

另一个是其他解决方案:

  • 如果您想单独检索 $statuses 而不是对 $statuses 进行分页,请在您的控制器中尝试执行 find()

    $this->set('statuses', statuses->find());
    

然后您可以在视图中加载数据:

 <td><?= $statuses[$application->status_id][title]; ?></td>

首先你的关联应该使用唯一的名称,你不能使用 Statuses 作为 belongsTo 以及 belongsToMany 关联(同样适用于 Applications) .

然后注意你的语法,Statuses 字符串没有被传递给 contain 选项,而是被设置为 paginate 选项数组中的一个值

'contain' => 'Users', 'Statuses' // < it's on the same level as `contain`

应该是

'contain' => ['Users', 'Statuses']

然后这个

<?= $application->has('status') ? $this->Html->link($application->status->title, ['controller' => 'Statuses', 'action' => 'view', $application->status->id]) : '' ?>

belongsTo 协会应该可以正常工作。 belongsToMany 当然需要区别对待,因为 属性 会保存一个数组。