Cakephp申请表

时间:2019-01-04 11:05:26

标签: cakephp

我使用cakephp 2创建了一份申请表。

现在我想知道,用户如何使用用户ID仅查看其应用程序详细信息。这是表单控制器,应用程序表单和用于显示表单的表

//Form controller
    public function index() {
            $this->set('posts', $this->Post->find('all'));
        }
       public function view($id = null) {
            if (!$id) {
                throw new NotFoundException(__('Invalid post'));
            }

            $post = $this->Post->findById($id);
            if (!$post) {
                throw new NotFoundException(__('Invalid post'));
            }
            $this->set('post', $post);
        }

        public function add() {
            if ($this->request->is('post')) {
                $this->Post->create();
                if ($this->Post->save($this->request->data)) {
                    $this->Flash->success(__('Your post has been saved.'));
                    return $this->redirect(array('action' => 'index'));
                }
                $this->Flash->error(__('Unable to add your post.'));
            }
        }

    //Create form
        echo $this->Form->create('Post');
        echo $this->Form->input('esta',['label'=>'New or Estabilished']);
        echo $this->Form->end('Save Post');

    //Form display

      <table>
            <tr>
                <th>Id</th>
                <th>Title</th>
                <th>Created</th>
            </tr>
            <?php foreach ($posts as $post): ?>
            <tr>
                <td><?php echo $post['Post']['id']; ?></td>
                <td>
                    <?php echo $this->Html->link($post['Post']['describe_idea'],
        array('controller' => 'posts', 'action' => 'view', $post['Post']['id'])); ?>
                </td>
                <td><?php echo $post['Post']['created']; ?></td>
            </tr>
            <?php endforeach; ?>
            <?php unset($post); ?>
        </table>

2 个答案:

答案 0 :(得分:1)

您说您正在使用 cakephp 2.x ,请在下面的代码中查找记录

单记录

$posts = $this->Post->find('first', array(
    'conditions' => array('id' => 1)
));

多个记录

$posts = $this->Post->find('all', array(
    'conditions' => array('id' => 1)
));

答案 1 :(得分:0)

在您的action

中添加过滤器

CakePHP 3.x

$posts = $this->Posts->find()
    ->where([
        'Posts.user_id' => $this->Auth->user('id')
    ]);

CakePHP 2.x

$posts = $this->Posts->find('all', [
     'user_id' => $this->Auth->user('id')
]);

注意:请确保登录用户以设置Auth数据。