Frage

I've been using Grocery Crud to develop a simple local application that allows users to register themselves and like bands and rate them and select people they know that are also registered in the application.

Entities:
Person(person_id,person_URL, fullname, hometown)
Band(band_id,band_URL,band_name,country,genre)

Relationships:
Likes(person_id,band_id,rate)
Knows(person_id,known_person_id)

My questions are:

1) I want to return a table of person and known person like below:

KNOWS person_id | fullname | known_person_id | known_fullname

but I can't use *set_relation_n_n* function 'cause the relationship is (Person -> Likes -> Person), so it's giving me error. The other solution I came up with is making a custom table making a query to return the values I want and show it in the table (code below). The custom table returned is correct but when I render it to my Grocery Crud table, I need to specify $crud->columns('person_id', 'fullname', 'known_person_id', 'fullname'), and it cannot differentiate the fullname of the person and the fullname of the known person. How would I make it in order to be able to show the table that way?

2) I have the same issue in another table but could manage that using the function *set_relation_n_n* 'cause it's a relationship (Person -> Likes -> Band), so since it's 2 different entities it didn't return me a error. The problem is that the query (code below) returns me the whole table and I want only 25 records per page. When I try to use "LIMIT 25" in the query, it returns me ONLY 25 records and the "next page" button doesn't work. Any solutions?

Below, all the information:

CODE for question 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);
    }

CODE for question 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);
    }
War es hilfreich?

Lösung

Question 1) What if you use an alias name in your query, for example

SELECT tb1.person_id, tb1.fullname as Tb1fullName, tb1.known_person_id, person.fullname as PersonFullName

Question 2) I would not recommend you to add LIMIT directly / manually in your query. In the file application/config/grocery_crud.php, you have two options directly related to pagination

You should use and configure them properly

// 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');
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top