2016-12-18 7 views
1

今週(私のインターンシップに必要な)CakePHPの使用を開始しました。 とにかく、私は以下の助けが必要です:CakePHP 3、フィールドが空の場合はレコードの全行を表示し、そうでない場合はそれを表示しない

空のフィールドがある場合にのみ、レコードを表示させる方法はありますか?上記画像に基づい

Sample reference

他を隠しながら、私は、空のタイトル/日付の行が表示されるようにしたいです。

他のページでは同じレコードを表示したいが、今回は完全に塗りつぶしたフィールドのみを表示し、空のフィールドを隠すようにします。

EDIT:

ビュー:

<?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']); 
} 

これは、私が質問を投稿、そうしてください午前初めてです私に必要があるかどうか私に知らせてください。incluそれ以上の細かいことなどはありません。 ありがとうございます。

+1

モデルレイヤーでは、特定のフィールドのNULL値の行のみを取得するようにデータベースに問い合わせます。 mysqlをデータベースとして使用している場合は、mtsqlのクエリ文字列を確認してください。 –

+0

ああ、私は参照してください:D sqliteを使用するように要求されました。cakephp 3で使用されるsqliteクエリの例を表示することは可能でしょうか? どうもありがとうございました! コメントに投票するにはどうすればよいですか? :D –

+0

モデルとコントローラコードを追加してください –

答えて

0

このコードでは、nameフィールドがnullのユーザーを選択します。

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

ありがとうございました! :D –

関連する問題