【问题标题】:CakePhp multiple table data fetch on single pageCakePhp 在单页上获取多表数据
【发布时间】:2015-08-01 12:23:48
【问题描述】:

我是 cakePHP 的新手,尝试创建一个博客站点,用户在选择类别后添加博客,我有一个类别表:字段:category_id、名称和帖子表:字段:id、category_id、标题、正文。 我想将所有类别提取到下拉列表中。当用户添加新帖子时,他们必须先选择类别,然后他才能发布任何内容..

我的帖子控制器:

<?php 


class PostsController extends AppController{



    public $helpers = array('Html', 'Form', 'Session');
    public $components = array('Session','Paginator');

    public function index(){

        $this->Paginator->setting = array(
            'limit' =>'10',
            'order' => array('Post.id' =>'Desc')
            /*'conditions' => array(
                'Post.user_id' => AuthComponent::user(id)
                )*/
            ); 

        $arrPosts = $this->Paginator->paginate('Post');

        $this->set('posts', $arrPosts);

    }

    public function view($id = null){

        if(!$id){

            throw new NotFoundException(__("Error Processing Request ID"));

        }
        $post =$this->Post->findById($id);
        if(!$post){

            throw new NotFoundException(__("Error Processing Request POST"));

        }
        $this->set('post',$post);   
    }

    public function add(){

         // HERE I want to fetch all categoryies from categories table and want to send to view

        if($this->request->is('post')){

            $this->Post->create();
            if($this->Post->save($this->request->data)){
                $this->Session->setFlash(__('Blog Posted Sucessfully'));
                return $this->redirect(array('action' => 'index'));
            }else{
                $this->Session->setFlash(__('Unable to Post Blog '));
            }
        }


    }
}
?>

我想以添加形式显示我的类别:

请帮帮我...

【问题讨论】:

    标签: php mysql cakephp cakephp-2.0


    【解决方案1】:

    在您的控制器操作中,您需要使用$this-&gt;set() 来设置视图变量。只要您在 Post 模型中正确设置了关联,您就应该能够使用:-

    $this->set('categories', $this->Post->Category->find('list'));
    

    Cake 的 FormHelper 应该自动知道 Post.category_id 表单字段希望成为一个选择输入,并以 $categories 作为选项。

    还有一点,最好在处理完表单后设置视图变量,因为如果保存正确,您将不需要它们,这样可以将数据库查询的数量减少 1。

    如果您使用find('all') 检索类别,您需要将其转换为find('list') 使用的格式,这可以使用Hash::combine() 轻松完成:-

    $categories = $this->Post->Category->find('all');
    $this->set(
        'categories', 
        Hash::combine($categories, '{n}.Category.id', '{n}.Category.name')
    );
    

    这样做的唯一真正价值是如果您需要$categories 来做其他事情。

    【讨论】:

    • 但我想用 find('all'); ** $this->set('categories', $this->Post->Category->find('all'));**
    • 如果您使用的是 find('all'),您将无法为表单助手获取正确格式的数据。我更新了我的答案,向您展示如何使用 find('all') 并更正格式。
    • @RahulSaxena 想要以 one 的方式做某事,并以 正确 的方式做某事,通常是相互排斥的,就像在这种情况下,有没有理由使用all 查找,从查看您的问题来看,这绝对没有意义。
    【解决方案2】:

    公共函数add(){

      /*Here get the category list & set the view (ctp file)*/
     $this->set('categories', $this->Post->Category->find('list'));
    
        if($this->request->is('post')){
    
            $this->Post->create();
            if($this->Post->save($this->request->data)){
                $this->Session->setFlash(__('Blog Posted Sucessfully'));
                return $this->redirect(array('action' => 'index'));
            }else{
                $this->Session->setFlash(__('Unable to Post Blog '));
            }
        }
    
    }
    

    //设置ctp文件中的下拉列表
    $this->Form->input('category_id', array('type'=>'select', 'options'=>'categories'));

    【讨论】:

    • 我正在使用 find('all') 因此它返回像 array( (int) 0 => array( 'Category' => array( 'category_id' => '1', 'name ' => 'Php' ) ), (int) 1 => array( 'Category' => array( 'category_id' => '3', 'name' => 'Ajax' ) } 怎么打印?
    猜你喜欢
    • 2018-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-16
    • 1970-01-01
    相关资源
    最近更新 更多