【问题标题】:CakePHP 3, display entire record row if a field is empty, otherwise do not display itCakePHP 3,如果字段为空则显示整个记录行,否则不显示
【发布时间】:2017-05-03 19:50:38
【问题描述】:

我这周刚刚开始使用 CakePHP(我的实习所必需的),而且我一开始对 PHP 并不是那么好。 无论如何,我需要以下帮助:

如何让一个记录只有在它有一个空字段时才显示?

Sample reference

根据上图,我只希望显示标题/日期为空的行,而隐藏另一行。

我希望在不同的页面上显示相同的记录,但这次只显示字段完全填满的记录,而隐藏字段为空的记录。

编辑:

查看:

<?php foreach ($users as $user): ?>
        <tr>
            <td><?= $this->Number->format($user->id) ?></td>
            <td><?= h($user->username) ?></td>
            <td><?= h($user->name) ?></td>
            <td><?= h($user->phone) ?></td>
            <td><?= h($user->email) ?></td>
            <td><?= h($user->role) ?></td>
            <td><?= $this->Number->format($user->status) ?></td>
            <td class="actions">
                <?= $this->Html->link(__('View'), ['action' => 'view', $user->id]) ?>
                <?= $this->Html->link(__('Edit'), ['action' => 'edit', $user->id]) ?>
                <?= $this->Form->postLink(__('Delete'), ['action' => 'delete', $user->id], ['confirm' => __('Are you sure you want to delete # {0}?', $user->id)]) ?>
            </td>
        </tr>
        <?php endforeach; ?>

型号:

public function initialize(array $config)
{
    parent::initialize($config);

    $this->table('users');
    $this->displayField('name');
    $this->primaryKey('id');
}

public function validationDefault(Validator $validator)
{
    $validator
        ->integer('id')
        ->allowEmpty('id', 'create');

    $validator
        ->notEmpty('username', 'A username is required');

    $validator
        ->notEmpty('password', 'A password is required');

    $validator
        ->allowEmpty('name');

    $validator
        ->allowEmpty('phone');

    $validator
        ->email('email')
        ->allowEmpty('email');

    $validator
        ->add('role', 'inList', [
            'rule' => ['inList', ['admin', 'editor', 'sales_user', 'field_user']],
            'message' => 'Please select a role']);

    $validator
        ->add('status', 'inList', [
            'rule' => ['inList', ['1', '2', '3']],
            'message' => 'Please select a status']);

    return $validator;
}

控制器:

public function index()
{
    $users = $this->paginate($this->Users);

    $this->set(compact('users'));
    $this->set('_serialize', ['users']);
}

这是我第一次发布问题,所以如果我需要提供任何进一步的详细信息等,请告诉我。 谢谢。

【问题讨论】:

  • 在模型层,查询数据库只获取特定字段的空值行。如果您使用 mysql 作为数据库,请检查 mtsql 查询字符串。
  • 哦,我明白了:D 我被要求使用 sqlite,是否可以向我展示一个在 cakephp 3 中使用的 sqlite 查询的示例?不管怎样,非常感谢!我如何投票赞成您的评论? :D
  • 请添加您的型号和控制器代码
  • 完成,您可以使用任何字段作为示例。

标签: cakephp cakephp-3.0


【解决方案1】:

此代码选择名称字段为空的用户。

public function index() {
  $users = $this->users->find('all', array("conditions" => array('name' => '')));
  $this->set("users", $users);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-28
    • 1970-01-01
    • 2016-05-22
    • 2013-05-27
    • 2021-09-02
    • 2022-08-18
    • 1970-01-01
    相关资源
    最近更新 更多