Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用cakephp对联接表进行分页_Php_Mysql_Cakephp - Fatal编程技术网

使用cakephp对联接表进行分页

使用cakephp对联接表进行分页,php,mysql,cakephp,Php,Mysql,Cakephp,我有几个操作都可以正确分页,但是我不知道这个“连接”查询到底发生了什么。它显示了我表中的所有帖子,而不是像我指定的那样一次显示3篇 public function index() { $this->paginate = array( 'joins' => array( array( 'table' => 'Users', 'type' => 'inner', 'li

我有几个操作都可以正确分页,但是我不知道这个“连接”查询到底发生了什么。它显示了我表中的所有帖子,而不是像我指定的那样一次显示3篇

 public function index() { 
    $this->paginate = array(
    'joins' => array(
        array(
            'table' => 'Users',
            'type' => 'inner',
            'limit' => 3,
            'fields' => array('User.username'),
            'conditions' => array('Users.id = Post.user_id')
            )
        ));
    $posts = $this->paginate('Post');
    $this->set(compact('posts'));
}
在此编辑下面的内容

table Users
id | username | password

table Posts
id | body | user_id | created
此函数可以工作,但不分页

public function index() { 
  $options['joins'] = array(
    array('table' => 'Users',
        'type' => 'inner',
        'fields' => array('User.username'),
        'conditions' => array('Users.id = Post.user_id')
        )
    );
    $post = $this->Post->find('all', $options);
    $this->set('posts', $post);
}
请阅读以下内容:-

分页不喜欢加入

除了定义常规分页值外,您还可以在控制器中定义多组分页默认值,您只需在要配置的模型后命名数组的键:

<?php
class PostsController extends AppController {

    public $paginate = array(
        'Post' => array (...),
        'Author' => array (...)
    );
}
试试这个:

Your Model class should have relationship like :

Post Model :
public $belongsTo = array('User');

Post Controller :

$this->Post->recursive = 1;
$this->set('posts', $this->paginate());

Index View :
<table >
    <thead>
    <tr>
            <th><?php echo $this->Paginator->sort('id'); ?></th>
            <th><?php echo $this->Paginator->sort('body'); ?></th>
            <th><?php echo $this->Paginator->sort('User.username'); ?></th>
        <tr>
<?php foreach ($posts as $post){ ?>
    <tr>
    <td><?php echo h($post['Post']['id']); ?>&nbsp;</td>
        <td><?php echo h($post['Post']['body']); ?>&nbsp;</td>
        <td><?php echo h($post['User']['username']); ?>&nbsp;</td>
    </tr>
<?php } ?>    
您的模型类应该具有如下关系:
后模型:
public$belongsTo=array('User');
邮政总监:
$this->Post->recursive=1;
$this->set('posts',$this->paginate());
索引视图:

以下是实现这一魔术的3个步骤

1) 在第一个模型中,只需添加第一个与第二个的$hasMany关系

public $hasMany = array(
                ‘Second’ => array(
                     ‘className’ => ‘Second’,
                     ‘foreignKey’ => ‘first_id’,
                     ‘dependent’ => false,
                     ‘conditions’ => ”,
                     ‘fields’ => ”,
                     ‘order’ => ”,
                     ‘limit’ => ”,
                     ‘offset’ => ”,
                     ‘exclusive’ => ”,
                     ‘finderQuery’ => ”,
                     ‘counterQuery’ => ”
     )
);
2) 在第一个控制器中,添加第二个模型用法,如下所示:

public $uses = array(‘First’,'Second’);
3) 最后打印$this->paginate(),您将获得所需的数组

NTB:在第一个模型的$hasMany数组中,添加所需数量的辅助表


给我以下错误“error:SQLSTATE[42000]:语法错误或访问冲突:1066非唯一表/别名:“User”。这是否意味着我的模型有问题?请提供您的用户表名和模型类名我看到了您的表名请提供模型类名或者您可以在模型中使用用户或用户类是用户和POST我已经改进了我的答案请查看并尝试。让我知道如果一件事情不起作用,那么为了模拟正常的蛋糕行为,将“别名”添加到您的连接数组中会很有帮助。然后,您的数据结果将被正确索引(例如$data['alias'])。
public $uses = array(‘First’,'Second’);