【问题标题】:Grocery Crud - Can't differentiate attribute in returning query tableGrocery Crud - 无法区分返回查询表中的属性
【发布时间】:2014-04-09 14:51:37
【问题描述】:

我一直在使用Grocery Crud 开发一个简单的本地应用程序,该应用程序允许用户注册自己、喜欢乐队并给他们评分并选择他们认识的也在应用程序中注册的人。

实体:
人员(person_id、person_URL、全名、家乡)
Band(band_id,band_URL,band_name,country,genre)

关系:
喜欢(person_id,band_id,rate)
知道(person_id,known_person_id)

我的问题是:

1) 我想返回一个人员和已知人员的表格,如下所示:

KNOWS person_id | fullname | known_person_id | known_fullname

但我不能使用 *set_relation_n_n* 函数,因为关系是 (Person -> Likes -> Person),所以它给了我错误。我想出的另一个解决方案是制作一个自定义表,进行查询以返回我想要的值并将其显示在表中(下面的代码)。返回的自定义表是正确的,但是当我将它渲染到我的 Grocery Crud 表时,我需要指定 $crud->columns('person_id', 'fullname', 'known_person_id', 'fullname'),它无法区分全名该人的姓名和已知人的全名。为了能够以这种方式显示表格,我将如何做到这一点?

2) 我在另一个表中有同样的问题,但可以使用函数 *set_relation_n_n* 来解决这个问题,因为它是一个关系(人 -> 喜欢 -> 乐队),所以因为它是 2 个不同的它没有给我返回错误的实体。问题是查询(下面的代码)返回给我整个表,我只想要每页 25 条记录。当我尝试在查询中使用“LIMIT 25”时,它只返回 25 条记录,并且“下一页”按钮不起作用。有什么解决办法吗?

以下,所有信息:

问题 1 的代码:

function knows_management()
    {
    $crud = new grocery_CRUD();
    $crud->set_model('model_socialnetwork');
    $crud->set_table('knows');
    $crud->set_subject('Known');
    $crud->basic_model->set_query_str('SELECT tb1.person_id, tb1.fullname, tb1.known_person_id, person.fullname FROM (SELECT person.person_id, person.fullname, knows.known_person_id FROM person INNER JOIN knows ON person.person_id = knows.person_id) tb1 INNER JOIN person ON tb1.known_person_id = person.person_id');<br>
    $crud->columns('person_id','fullname','known_person_id','fullname');
    $output = $crud->render();
    $this->_socialnetwork_output($output);
    }

问题 2 的代码:

function likes_management()
    {
        $crud = new grocery_CRUD();
        $crud->set_model('model_socialnetwork');
        $crud->set_table('likes');
        $crud->set_subject('Like');
        $crud->columns('person_id','fullname','band_id','band_name', 'rate');
        $crud->basic_model->set_query_str('SELECT tb2.person_id, tb2.fullname, tb2.band_id, band.band_name, tb2.rate FROM(SELECT tb1.person_id, person.fullname, tb1.band_id, tb1.rate FROM(SELECT person.person_id, likes.band_id, likes.rate FROM person INNER JOIN likes ON person.person_id = likes.person_id) tb1 INNER JOIN person ON tb1.person_id = person.person_id) tb2 INNER JOIN band ON tb2.band_id = band.band_id');
        $output = $crud->render();
        $this->_socialnetwork_output($output);
    }

【问题讨论】:

    标签: php mysql sql entity-framework grocery-crud


    【解决方案1】:

    问题 1) 例如,如果您在查询中使用别名会怎样

    选择 tb1.person_id,tb1.fullname 作为 Tb1fullName,tb1.known_person_id,person.fullname 作为 PersonFullName

    问题 2) 我不建议您在查询中直接/手动添加 LIMIT。在文件 application/config/grocery_crud.php 中,您有两个与分页直接相关的选项

    您应该正确使用和配置它们

    // The default per page when a user firstly see a list page
    $config['grocery_crud_default_per_page']    = 25;
    
    ....
    
        //Having some options at the list paging. This is the default one that all the websites are using.
        //Make sure that the number of grocery_crud_default_per_page variable is included to this array.
        $config['grocery_crud_paging_options'] = array('10','25','50','100');
    

    【讨论】:

    • 感谢马塞洛的回答,工作顺利。我设法将所有值插入到一个临时表中,并使用 callback_before_insert 我使用 $this->db->insert 将这些值添加到另一个表中。
    猜你喜欢
    • 1970-01-01
    • 2013-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多